欢迎各位兄弟 发布技术文章
这里的技术是共享的
bash#!/bin/bash # 获取当前WiFi的SSID SSID=$(/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport -I | awk -F: '/ SSID/{print $2}' | xargs) # AppleScript代码创建弹窗 osascript <<EOF tell application "System Events" activate set theDialog to display dialog "WiFi登录信息" with title "网络认证" ¬ default answer "" ¬ buttons {"取消", "确定"} default button 2 ¬ giving up after 60 ¬ with icon note ¬ with text {"当前连接网络: $SSID", "请输入您的用户名和密码"} ¬ -- 添加用户名输入框 set userNameField to text returned of theDialog -- 添加密码输入框 set theDialog to display dialog "请输入密码" with title "密码" ¬ default answer "" ¬ buttons {"取消", "确定"} default button 2 ¬ with hidden answer set passWordField to text returned of theDialog -- 返回结果 return {userNameField, passWordField} end tell EOF
这个脚本的工作原理:
首先使用airport
工具获取当前连接的WiFi SSID
然后通过AppleScript创建两个弹窗:
第一个显示SSID并获取用户名
第二个获取密码(以隐藏形式显示)
最后将用户名和密码作为结果返回
如果你想要更美观的单窗口解决方案,可以使用更复杂的AppleScript:
applescripttell application "System Events" activate set theSSID to do shell script "/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport -I | awk -F: '/ SSID/{print \$2}' | xargs" set theDialog to display dialog "WiFi登录认证" & return & "当前网络: " & theSSID & return & return & "请输入您的凭据:" with title "网络认证" ¬ buttons {"取消", "登录"} default button 2 ¬ with icon note ¬ default answer "" ¬ with prompt "用户名:" ¬ set theUsername to text returned of theDialog set theDialog to display dialog "请输入 " & theSSID & " 的密码:" with title "密码" ¬ buttons {"取消", "确定"} default button 2 ¬ default answer "" ¬ with hidden answer ¬ with icon note set thePassword to text returned of theDialog return {theUsername, thePassword} end tell
将上述代码保存为.scpt
文件或直接在脚本编辑器中运行。
注意:为了获取SSID,脚本需要"完全磁盘访问"权限(在macOS Mojave及更高版本中)。