编写AppleScript快速切换网络位置

一直想自动切换网络位置,就琢磨了一下。最普通的切换方法我想大家应该都知道了,就是进入网络设置(通过Airport的图标下拉也好,dock上保存系统偏好设置也行),然后切换、应用,如果进一步需要控制Airport无线网卡,那么还需要多点击一次。我这个人懒,能点一次的何必要费四五次点击呢?

于是有了这个方法: 打开应用程序——实用工具——AppleScript编辑器,然后将下面的代码贴进去(后面我来讲解含义,请注意每行之间没有空行),按照后面的图示保存为只运行的应用程序,以后你双击这个程序就会运行脚本了。

tell application “System Events”
tell network preferences
do shell script “scselect ‘Home'”
do shell script “sudo networksetup -setairportpower AirPort on” user name “用户名” password “密码” with administrator privileges
end tell
end tell

tell application “System Events”

tell network preferences

do shell script “scselect ‘Home'”

do shell script “sudo networksetup -setairportpower AirPort on” user name “用户名” password “密码” with administrator privileges

end tell

end tell

讲解下代码含义,其中第三行scselect其实是一个指令,你可以放到终端里运行,将列出所有的网络位置,其中加“*”的是当前正在使用的。

阅读全文