<?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>Wise on Tech &#187; AppleScript</title>
	<atom:link href="http://www.wiseontech.com/tag/applescript/feed" rel="self" type="application/rss+xml" />
	<link>http://www.wiseontech.com</link>
	<description>Hacks, scripts and ideas for the refined geek.</description>
	<lastBuildDate>Thu, 22 Jul 2010 14:48:15 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Using AppleScript and a Shell Script to Restart an AppleTV remotely</title>
		<link>http://www.wiseontech.com/hack/using-applescript-and-a-shell-script-to-restart-an-appletv-remotely</link>
		<comments>http://www.wiseontech.com/hack/using-applescript-and-a-shell-script-to-restart-an-appletv-remotely#comments</comments>
		<pubDate>Mon, 21 Jul 2008 23:43:46 +0000</pubDate>
		<dc:creator>Jonathan Wise</dc:creator>
				<category><![CDATA[Hacks]]></category>
		<category><![CDATA[AppleScript]]></category>
		<category><![CDATA[AppleTV]]></category>
		<category><![CDATA[SSH]]></category>

		<guid isPermaLink="false">http://software.jonandnic.com/?p=110</guid>
		<description><![CDATA[Both my media serving devices (a NAS, and a Mac Mini) are using TwonkyVision Media Server to share media using uPnP. It works great, but as I mentioned earlier, I needed a script to make it start back up in the morning. Unfortunately, I've found that my AppleTV freaks out a bit when this happens, [...]]]></description>
			<content:encoded><![CDATA[<p><img class="alignright alignnone size-full wp-image-111" style="float: right;" title="appletv" src="http://www.wiseontech.com/wp-content/uploads/2008/07/appletv.jpg" alt="" width="225" height="125" /></p>
<p>Both my media serving devices (a NAS, and a Mac Mini) are using TwonkyVision Media Server to share media using uPnP. It works great, but as I <a href="http://www.wiseontech.com/wp-content/plugins/feed-statistics.php?url=aHR0cDovL3d3dy53aXNlb250ZWNoLmNvbS9oYWNrL3Jlc3RhcnQtdHdvbmt5bWVkaWEtYWZ0ZXItc2NoZWR1bGVkLXNsZWVw">mentioned earlier</a>, I needed a script to make it start back up in the morning.</p>
<p>Unfortunately, I've found that my AppleTV freaks out a bit when this happens, and as a result, needs its own reboot. Restarting the Finder helps, but streaming still gets weird, so a full, daily reboot is in order. Using the same iCal-alarm-firing-an-AppleScript trick, I updated my Twonky restart script to also tell the AppleTV to restart. But it wasn't that easy.</p>
<ul>
<li>First of all, you need to <a href="http://www.wiseontech.com/wp-content/plugins/feed-statistics.php?url=aHR0cDovL3d3dy53aXNlb250ZWNoLmNvbS9oYWNrL2dlbmVyYXRpbmctcnNhLWtleXBhaXJzLW9uLW1hYy1vcy14LWZvci1hLW5peC13ZWItc2VydmVy">exchange keypairs</a> with the AppleTV so that you can login over ssh without a password. If you're using an older patchstick, the process is a little different, since it may only support ssh1. This wiki page explains the process, and the <a href="http://www.wiseontech.com/wp-content/plugins/feed-statistics.php?url=aHR0cDovL3dpa2kuYXdrd2FyZHR2Lm9yZy93aWtpL2luZGV4LnBocD90aXRsZT1JbnN0YWxsX1NTSCNVc2luZ19Qcm90b2NvbF8x">slight tweak to it for ssh1</a>.</li>
</ul>
<ul>
<li>Second, you need to set the AppleTV up to allow you to sudo without a password. This is harder than it sounds, and requires modifying the /etc/sudoers file on the AppleTV. The only way I found to do that was to <code>sudo cat /etc/sudoers &gt; ~/sudoers</code> the file (which <a href="http://www.wiseontech.com/wp-content/plugins/feed-statistics.php?url=aHR0cDovL3d3dy5yZWRoYXQuY29tL2RvY3MvbWFudWFscy9saW51eC9SSEwtOS1NYW51YWwvZ2V0dGluZy1zdGFydGVkLWd1aWRlL3MxLW5hdmlnYXRpbmctdXNpbmdjYXQuaHRtbA==">dumps the contents of one file to another</a>, which you can access more easily), copy it over to my Mac, edit it, copy it back, set the permissions, and sudo mv it back over-top the original. You'll need to add this line to the bottom of the sudoers file:<code><br />
frontrow ALL=(ALL) NOPASSWD: ALL<br />
</code></li>
<li><strong>Make sure you change the permissions (chmod 0440) and ownership (chown root:wheel) on your new sudoers</strong> <strong><em>before </em>you replace the original, or you'll screw yourself out of sudo!</strong></li>
</ul>
<p>To test those things, you could go to Terminal on your Mac and try something like:<br />
<code>ssh -1 frontrow@appletv.local 'sudo ls-l'</code></p>
<p><em>(drop the -1 for newer Patchsticks)</em></p>
<p>If you've setup everything right, you shouldn't get prompted for a password to login OR to get a directory listing. Once that works, the AppleScript is easy and looks like this:</p>
<p><code>do shell script "ssh -1 frontrow@appletv.local 'sudo reboot' &amp;&gt; /dev/null &amp;"</code></p>
<p><em>(drop the -1 for newer Patchsticks)</em></p>
<p>At some point, I'm also going to attach this to a PHP script (somehow) so I can use Safari on my iPhone to do a reboot from the couch if needed. I've only found two other ways to reboot the AppleTV -- yanking the power cord, or using the remote to put it in recovery mode. Neither seems as elegant as my solution.</p>
 <img src="http://www.wiseontech.com/wp-content/plugins/feed-statistics.php?view=1&post_id=110" width="1" height="1" style="display: none;" /><img src="http://www.wiseontech.com/?ak_action=api_record_view&id=110&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.wiseontech.com/hack/using-applescript-and-a-shell-script-to-restart-an-appletv-remotely/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Restart TwonkyMedia after scheduled sleep</title>
		<link>http://www.wiseontech.com/hack/restart-twonkymedia-after-scheduled-sleep</link>
		<comments>http://www.wiseontech.com/hack/restart-twonkymedia-after-scheduled-sleep#comments</comments>
		<pubDate>Wed, 09 Jul 2008 16:03:42 +0000</pubDate>
		<dc:creator>Jonathan Wise</dc:creator>
				<category><![CDATA[Hacks]]></category>
		<category><![CDATA[AppleScript]]></category>
		<category><![CDATA[AppleTV]]></category>

		<guid isPermaLink="false">http://software.jonandnic.com/?p=106</guid>
		<description><![CDATA[In our house, a Mac Mini serves at the content library host. It runs iTunes and shares other media files. At night it runs a number of scheduled tasks to keep things orderly and fresh, and when its done (at about 4:00am) it goes to sleep until 8:00am when my wife will want to use [...]]]></description>
			<content:encoded><![CDATA[<p>In our house, a Mac Mini serves at the content library host. It runs iTunes and shares other media files. At night it runs a number of scheduled tasks to keep things orderly and fresh, and when its done (at about 4:00am) it goes to sleep until 8:00am when my wife will want to use it again.</p>
<p>TwonkyMedia server, which is delivering our non-iTunes content onto the AppleTV (via the MediaCloud uPnP client) has a problem with this. It doesn't recover well from sleep.</p>
<p>The solution is to run an AppleScript every morning at 8:01 to re-start TwonkyMedia. This little AppleScript will access Twonky's built-in shell script to shut down any running instances, and start-up a fresh one:</p>
<p><code>do shell script "/Applications/MediaServer.app/Contents/MacOS/twonkymedia.sh &amp;&gt; /dev/null &amp;"</code></p>
<p>The "&amp;&gt; /dev/null &amp;" sends the shell script's output to oblivion so that AppleScript doesn't hang waiting for the server to start-up.</p>
<p>The only annoying part about this is that every time TwonkyMedia starts up, it wants to take you to its home page in your browser. Follow these directions to <a href="http://www.wiseontech.com/wp-content/plugins/feed-statistics.php?url=aHR0cDovL3d3dy50d29ua3lmb3J1bS5jb20vdmlld3RvcGljLnBocD9mPTcmYW1wO3Q9NDkyMg==" target=\"_blank\">make TwonkyMedia start-up silently</a> (last post on the page.)</p>
 <img src="http://www.wiseontech.com/wp-content/plugins/feed-statistics.php?view=1&post_id=106" width="1" height="1" style="display: none;" /><img src="http://www.wiseontech.com/?ak_action=api_record_view&id=106&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.wiseontech.com/hack/restart-twonkymedia-after-scheduled-sleep/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Using Mail.app with multiple users — using AppleScript: Part 2</title>
		<link>http://www.wiseontech.com/hack/using-mailapp-with-multiple-users-%e2%80%94-using-applescript-part-2</link>
		<comments>http://www.wiseontech.com/hack/using-mailapp-with-multiple-users-%e2%80%94-using-applescript-part-2#comments</comments>
		<pubDate>Mon, 28 Jan 2008 00:21:04 +0000</pubDate>
		<dc:creator>Jonathan Wise</dc:creator>
				<category><![CDATA[Hacks]]></category>
		<category><![CDATA[AppleScript]]></category>
		<category><![CDATA[Mail]]></category>

		<guid isPermaLink="false">http://software.jonandnic.com/hack/using-mailapp-with-multiple-users-%e2%80%94-using-applescript-part-2</guid>
		<description><![CDATA[The previous post, about hacking multi-user support into Mail.app, seems to have struck a chord, and there have been lots of great questions about how to extend the script, or customize its behavior or appearance. Things tend to get lost in the comments, so I figured I'd create a part 2 to answer some of [...]]]></description>
			<content:encoded><![CDATA[<p>The previous post, about <a href="http://www.wiseontech.com/wp-content/plugins/feed-statistics.php?url=aHR0cDovL3d3dy53aXNlb250ZWNoLmNvbS9oYWNrL3VzaW5nLW1haWxhcHAtd2l0aC1tdWx0aXBsZS11c2Vycy11c2luZy1hcHBsZXNjcmlwdA==">hacking multi-user support into Mail.app</a>, seems to have struck a chord, and there have been lots of great questions about how to extend the script, or customize its behavior or appearance. Things tend to get lost in the comments, so I figured I'd create a part 2 to answer some of the questions that have popped up. If you haven't already, <a href="http://www.wiseontech.com/wp-content/plugins/feed-statistics.php?url=aHR0cDovL3d3dy53aXNlb250ZWNoLmNvbS9oYWNrL3VzaW5nLW1haWxhcHAtd2l0aC1tdWx0aXBsZS11c2Vycy11c2luZy1hcHBsZXNjcmlwdA==">check out part 1</a> to get caught up.</p>
<p>Some of these might seem a little basic to long-time users, but lets remember that not everyone has been using a Mac since OS 7 (or earlier!) and cut the newly Mac-faithful some slack...</p>
<p><strong>Evan asks: Absolutely perfect, exactly what I was looking for. Well, almost. How about adding a 3rd account?</strong></p>
<div style="border-top: 1px solid #c0c0c0; padding-top: 8px; margin-bottom: 12px; margin-left: 6px;">A third account is done very easily by modifying the script slightly. (<strong>Update</strong>: If you'd like to do more than 3 accounts, see the solution in the comments which changes our user interface to a listbox)</p>
<p>The first thing you'll want to do is update our crude little user interface to ask about the third account. This line here decides what options show up in the dialog box:</p>
<p><code>display dialog "Choose the Mail account to use" buttons {"Jonathan", "Nicole"} default button 1 with icon note</code></p>
<p>You can add <span style="text-decoration: line-through;">as many</span> up to 3 options <span style="text-decoration: line-through;">as you want</span>, just by comma seperating them, so instead of {"Jonathan", "Elizabeth"} you could have {"Jonathan", "Elizabeth", "David"}<br />
Then you need to modify the "if" statement to support each individual referred to. In the original we said:</p>
<p><code>if the button returned of the result is "Jonathan" then<br />
-- do stuff to change account to Jonathan<br />
else<br />
-- do stuff to change account to Elizabeth<br />
end if</code></p>
<p>The <em>else</em> is a problem now because it assumes only two conditions. Instead of an <em>else</em>, we can do an <em>else if</em> -- one for each individual we want to switch between:</p>
<p><code>if the button returned of the result is "Jonathan" then<br />
-- do stuff to change account to Jonathan<br />
else if the button returned of the result is "Elizabeth" then<br />
-- do stuff to change account to Elizabeth<br />
else if the button returned of the result is "David" then<br />
-- do stuff to change account to David<br />
end if</code></p>
<p>Inside each condition you'll need to disable all the other accounts (<code>set enabled to account X to false</code>) and then enable the account they've chosen.</div>
<p><strong>Eric asks: I would like to attach to the script the mail icon or a similar icon. Then when I added it to the dock, it is more obvious to click it to run.</strong></p>
<div style="border-top: 1px solid #c0c0c0; padding-top: 8px; margin-bottom: 12px; margin-left: 6px;">This is one of those tricks that long time Mac users will know well, but is not very obvious for newer converts. You can copy and paste any icon from any application/document/folder to any other in the finder. Just click on the icon you want to "borrow" from and hit Apple + I for "Get Info." In the info dialog, click on the icon and hit Apple + C to copy the icon to the clipboard. Now find your target icon (such as the script you just made) and "Get Info" on it, click the icon in the dialog and this time hit Apple + P to paste. Now you have a pretty icon!</p>
<p>Note: This will be limited by permissions, so if your user doesn't have permission to 'write' to the target object, you won't be able to paste.</p></div>
<p><strong>Eric, having figured out the above on his own, then asks: My 2 problems are: 1, I can not add the script to the dock. 2, when I click the script icon, it take me to the editor where I then have to click RUN.</strong></p>
<div style="border-top: 1px solid #c0c0c0; padding-top: 8px; margin-bottom: 12px; margin-left: 6px;">A script, by itself is just a document. In order to make it into a runable application, you have to save it as such in Script Editor. From the File / Save As... dialog <a href="http://www.wiseontech.com/wp-content/plugins/feed-statistics.php?url=aHR0cDovL3d3dy53aXNlb250ZWNoLmNvbS93cC1jb250ZW50L3VwbG9hZHMvMjAwOC8wMS9zYXZlYXMucG5n" target=\"_blank\">change the File Format</a> to "application."</div>
<p>Under most circumstances you won't want to check the box for 'Run Only' because once you do, you cannot edit it in Script Editor again. Also, uncheck the box for 'Startup Screen' to make it run a little more gracefully.</p>
<p><img src="http://www.wiseontech.com/wp-content/uploads/2008/01/scriptmenu.png" alt="" align="right" />You may also find that the Dock is not the best place for your script -- since it will essentially give you two Mail icons. What I did instead was to enable the AppleScript menu and use that for all my common scripts.</p>
<p>If you want to do this, open the Application <a href="http://www.wiseontech.com/wp-content/plugins/feed-statistics.php?url=aHR0cDovL3d3dy53aXNlb250ZWNoLmNvbS93cC1jb250ZW50L3VwbG9hZHMvMjAwOC8wMS9zY3JpcHR1dGlsaXR5LnBuZw==" target=\"_blank\">AppleScript Utility</a> in the AppleScript folder and check the box for 'Show Script Menu in the menu bar.' You may also want to uncheck the box for "Show Library scripts" to hide the example scripts Apple includes to make your menu shorter.</p>
<p>Note: For scripts to show up in this menu you'll have to save them where your Mac expects your scripts to be: In your <em>user's</em> Library folder you'll find a folder called "Scripts." Put them, or an alias to them, in there.</p>
 <img src="http://www.wiseontech.com/wp-content/plugins/feed-statistics.php?view=1&post_id=37" width="1" height="1" style="display: none;" /><img src="http://www.wiseontech.com/?ak_action=api_record_view&id=37&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.wiseontech.com/hack/using-mailapp-with-multiple-users-%e2%80%94-using-applescript-part-2/feed</wfw:commentRss>
		<slash:comments>18</slash:comments>
		</item>
		<item>
		<title>Using Mail.app with multiple users &#8212; using AppleScript!</title>
		<link>http://www.wiseontech.com/hack/using-mailapp-with-multiple-users-using-applescript</link>
		<comments>http://www.wiseontech.com/hack/using-mailapp-with-multiple-users-using-applescript#comments</comments>
		<pubDate>Sun, 06 Jan 2008 19:15:07 +0000</pubDate>
		<dc:creator>Jonathan Wise</dc:creator>
				<category><![CDATA[Code Snippets]]></category>
		<category><![CDATA[Hacks]]></category>
		<category><![CDATA[AppleScript]]></category>
		<category><![CDATA[Mail]]></category>

		<guid isPermaLink="false">http://software.jonandnic.com/hack/using-mailapp-with-multiple-users-using-applescript</guid>
		<description><![CDATA[OK, this is pretty brilliant -- and SO simple. Here's the setup: my wife and I share a Mac at home. For memory reasons, among others, I don't want to use Fast User Switching, and because of my automated tasks, I don't want the primary account to ever be logged out. So all many of [...]]]></description>
			<content:encoded><![CDATA[<p>OK, this is pretty brilliant -- and SO simple.</p>
<p>Here's the setup: my wife and I share a Mac at home. For memory reasons, among others, I don't want to use Fast User Switching, and because of my automated tasks, I don't want the primary account to ever be logged out. So all many of our programs need to be set-up for two different users. Firefox has user profiles, that once configured, works perfect. Mail.app has no such thing. What I decided to then, was write an AppleScript that would switch Mail.app between users for us. This example is for two users, but it could be edited for more. Here's how to use it:</p>
<ul>
<li>Setup Mail accounts for each user</li>
<li>Modify the script to prompt for each user you have, and reference their account name</li>
<li>Replace your Mail.app dock icon with a link to your AppleScript (you can even give it the Mail.app icon)</li>
<li>Whenever you launch Mail, you'll be asked which user you want to use</li>
<li>Even better than that, you can switch users just by clicking the Mail icon in your dock again. You don't even have to close down Mail!</li>
</ul>
<p>The result looks like this whenever you invoke the script, and launches/reconfigures Mail within two seconds. Set the delay to longer if Mail.app takes longer to start on your Mac.</p>
<p><img class="size-medium wp-image-268" title="mailprompt" src="http://www.wiseontech.com/wp-content/uploads/2008/01/mailprompt1-300x94.png" alt="" /></p>
<p>The code is dead simple, and took me only moments to put together. Note that the delays and the order in which things are done is important so that it doesn't hang waiting for Mail to start if its not already open.<br />
<code><br />
-- Mail Account Chooser, by Jonathan Wise<br />
-- Add user profiles to Mail.app<br />
display dialog "Choose the Mail account to use" buttons {"Jonathan", "Elizabeth"} default button 1 with icon note<br />
if the button returned of the result is "Jonathan" then<br />
  tell application "Mail"<br />
    activate<br />
    delay 2<br />
    set enabled of account "Elizabeth Home" to false<br />
    set enabled of account "Jonathan Home" to true<br />
  end tell<br />
else<br />
  tell application "Mail"<br />
    activate<br />
    delay 2<br />
    set enabled of account "Jonathan Home" to false<br />
    set enabled of account "Elizabeth Home" to true<br />
  end tell<br />
end if<br />
</code></p>
 <img src="http://www.wiseontech.com/wp-content/plugins/feed-statistics.php?view=1&post_id=32" width="1" height="1" style="display: none;" /><img src="http://www.wiseontech.com/?ak_action=api_record_view&id=32&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.wiseontech.com/hack/using-mailapp-with-multiple-users-using-applescript/feed</wfw:commentRss>
		<slash:comments>32</slash:comments>
		</item>
	</channel>
</rss>
