<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Read ERN &#187; AppleScript</title>
	<atom:link href="http://www.readern.com/tag/applescript/feed" rel="self" type="application/rss+xml" />
	<link>http://www.readern.com</link>
	<description>生命不息折腾不止</description>
	<lastBuildDate>Sat, 31 Dec 2011 16:18:48 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>编写AppleScript快速切换网络位置</title>
		<link>http://www.readern.com/fast-switch-network-location.html</link>
		<comments>http://www.readern.com/fast-switch-network-location.html#comments</comments>
		<pubDate>Tue, 27 Oct 2009 14:49:45 +0000</pubDate>
		<dc:creator>ern</dc:creator>
				<category><![CDATA[咬口苹果]]></category>
		<category><![CDATA[网络]]></category>
		<category><![CDATA[AppleScript]]></category>
		<category><![CDATA[MAC OS X]]></category>

		<guid isPermaLink="false">http://www.readern.com/?p=180</guid>
		<description><![CDATA[一直想自动切换网络位置，就琢磨了一下。最普通的切换方法我想大家应该都知道了，就是进入网络设置（通过Airport的图标下拉也好，dock上保存系统偏好设置也行），然后切换、应用，如果进一步需要控制Airport无线网卡，那么还需要多点击一次。我这个人懒，能点一次的何必要费四五次点击呢？ 于是有了这个方法： 打开应用程序——实用工具——AppleScript编辑器，然后将下面的代码贴进去（后面我来讲解含义，请注意每行之间没有空行），按照后面的图示保存为只运行的应用程序，以后你双击这个程序就会运行脚本了。 tell application “System Events” tell network preferences do shell script “scselect &#8216;Home&#8217;” 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 &#8216;Home&#8217;” do shell script “sudo networksetup -setairportpower AirPort on” [...]]]></description>
			<content:encoded><![CDATA[<p>一直想自动切换网络位置，就琢磨了一下。最普通的切换方法我想大家应该都知道了，就是进入网络设置（通过Airport的图标下拉也好，dock上保存系统偏好设置也行），然后切换、应用，如果进一步需要控制Airport无线网卡，那么还需要多点击一次。我这个人懒，能点一次的何必要费四五次点击呢？</p>
<p>于是有了这个方法： 打开应用程序——实用工具——AppleScript编辑器，然后将下面的代码贴进去（后面我来讲解含义，请注意每行之间没有空行），按照后面的图示保存为只运行的应用程序，以后你双击这个程序就会运行脚本了。<br />
<img src="http://images.weiphone.com/attachments/Day_091027/68_91849_413dfbf147e2c6f.jpg" alt="" /></p>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 56px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">tell application “System Events”</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 56px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;"><span style="white-space: pre;"> </span>tell network preferences</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 56px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;"><span style="white-space: pre;"> </span>do shell script “scselect &#8216;Home&#8217;”</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 56px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;"><span style="white-space: pre;"> </span>do shell script “sudo networksetup -setairportpower AirPort on” user name “用户名” password “密码” with administrator privileges</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 56px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;"><span style="white-space: pre;"> </span>end tell</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 56px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">end tell</div>
<blockquote><p>tell application “System Events”</p>
<p><span style="white-space:pre"> </span>tell network preferences</p>
<p><span style="white-space:pre"> </span>do shell script “scselect &#8216;Home&#8217;”</p>
<p><span style="white-space:pre"> </span>do shell script “sudo networksetup -setairportpower AirPort on” user name “用户名” password “密码” with administrator privileges</p>
<p><span style="white-space:pre"> </span>end tell</p>
<p>end tell</p></blockquote>
<p>讲解下代码含义，其中第三行scselect其实是一个指令，你可以放到终端里运行，将列出所有的网络位置，其中加“*”的是当前正在使用的。<br />
<img src="http://images.weiphone.com/attachments/Day_091027/68_91849_5fc32ce05be1eba.jpg" alt="" /></p>
<p><span id="more-180"></span></p>
<blockquote><p><span style="font-family: Arial, Arial, sans-serif; line-height: normal; white-space: pre-wrap;">scselect 位置名</span></p></blockquote>
<p>就是切换到这个网络位置，所以第三行后面的Home你可以换成你自己设置的位置名。</p>
<p>第四行是切换Airport，由于使用了sudo，因此需要提供管理帐户和密码，也就是用户名和密码那里填的内容（注意前后的双引号还是要保持的）。中间的on是打开，改成off后就是关闭，按需修改。 改好适合你的文本以后，将其贴入AppleScript编辑器，点击“编译”，可以检查你是否有语法出错。报错的话请仔细核对，我的代码是我测试过的，没有问题。</p>
<p>附注：一般而言，你的MAC电脑主账户是具有管理权限的，也就是管理账户，当你没有设置密码，是无法使用sudo获得root权限的。因此，要使用sudo的话，请设置账户密码。至于直接启用root账户，那请搜索一下吧。提醒一句，sudo或者启用root后，你的权限是非常高的，可以执行任何操作，意味着你可以随意破坏系统（事实上你可以把当前的系统都删除），所以务必注意安全。这些是另一个故事了，不展开了。</p>
<hr />
<p><small>© ern for <a href="http://www.readern.com">Read ERN</a>, 2009. |
<a href="http://www.readern.com/fast-switch-network-location.html">Permalink</a> |
<a href="http://www.readern.com/fast-switch-network-location.html#comments">One comment</a> |
Add to
<a href="http://del.icio.us/post?url=http://www.readern.com/fast-switch-network-location.html&title=编写AppleScript快速切换网络位置">del.icio.us</a>
<br/>
Post tags: <a href="http://www.readern.com/tag/applescript" rel="tag">AppleScript</a>, <a href="http://www.readern.com/tag/mac-os-x" rel="tag">MAC OS X</a><br/>
</small></p>
<p><small>Feed enhanced by <a href='http://planetozh.com/blog/my-projects/wordpress-plugin-better-feed-rss/'>Better Feed</a> from  <a href='http://planetozh.com/blog/'>Ozh</a></small></p>
]]></content:encoded>
			<wfw:commentRss>http://www.readern.com/fast-switch-network-location.html/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

