Jul 21 2008

Using AppleScript and a Shell Script to Restart an AppleTV remotely

Published by Jonathan Wise under Hacks

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, 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.

  • First of all, you need to exchange keypairs with the AppleTV so that you can login over ssh without a password. However, the process is a little different, since the AppleTV only supports ssh1. This wiki page explains the process, and the slight tweak to it for ssh1.
  • 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 sudo cat > ~/sudoers the file (which dumps the contents of one file to another, 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:
    frontrow ALL=(ALL) NOPASSWD: ALL
  • Make sure you change the permissions (0440) and ownership (root:wheel) on your new sudoers before you replace the original, or you’ll screw yourself out of sudo!

To test those things, you could go to Terminal on your Mac and try something like:
ssh -1 frontrow@appletv.local 'sudo ls-l'
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:

do shell script "ssh -1 frontrow@appletv.local 'sudo reboot' &> /dev/null &"

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.

No responses yet

Jul 09 2008

Restart TwonkyMedia after scheduled sleep

Published by Jonathan Wise under Hacks

In our house, a Mac Mini serves at the content library and retreival machine. It runs a Torrent client, iTunes, and TVShows to collect and organize all our media. 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.

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.

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:

do shell script "/Applications/MediaServer.app/Contents/MacOS/twonkymedia.sh &> /dev/null &"

The “&> /dev/null &” sends the shell script’s output to oblivion so that AppleScript doesn’t hang waiting for the server to start-up.

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 make TwonkyMedia start-up silently (last post on the page.)

No responses yet

Jan 27 2008

Using Mail.app with multiple users — using AppleScript: Part 2

Published by Jonathan Wise under Hacks

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 the questions that have popped up. If you haven’t already, check out part 1 to get caught up.

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…

Evan asks: Absolutely perfect, exactly what I was looking for. Well, almost. How about adding a 3rd account?

A third account is done very easily by modifying the script slightly. (Update: If you’d like to do more than 3 accounts, see the solution in the comments which changes our user interface to a listbox)

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:

display dialog "Choose the Mail account to use" buttons {"Jon", "Nicole"} default button 1 with icon note

You can add as many up to 3 options as you want, just by comma seperating them, so instead of {”Jon”, “Nicole”} you could have {”Jon”, “Nicole”, “Yoda”}
Then you need to modify the “if” statement to support each individual referred to. In the original we said:

if the button returned of the result is "Jon" then
-- do stuff to change account to Jon
else
-- do stuff to change account to Nicole
end if


The else is a problem now because it assumes only two conditions. Instead of an else, we can do an else if — one for each individual we want to switch between:

if the button returned of the result is "Jon" then
-- do stuff to change account to Jon
else if the button returned of the result is "Nicole" then
-- do stuff to change account to Nicole
else if the button returned of the result is "Yoda" then
-- do stuff to change account to Luke
end if


Inside each condition you’ll need to disable all the other accounts (set enabled to account X to false) and then enable the account they’ve chosen.

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.

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!

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.

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.

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 change the File Format to “application.”

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.

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.

If you want to do this, open the Application AppleScript Utility 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.

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 user’s Library folder you’ll find a folder called “Scripts.” Put them, or an alias to them, in there.

8 responses so far

Jan 06 2008

Using Mail.app with multiple users — using AppleScript!

Published by Jonathan Wise under Code Snippets, Hacks

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 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:

  • Setup Mail accounts for each user
  • Modify the script to prompt for each user you have, and reference their account name
  • Replace your Mail.app dock icon with a link to your AppleScript (you can even give it the Mail.app icon)
  • Whenever you launch Mail, you’ll be asked which user you want to use
  • 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!

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.

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.

-- Mail Account Chooser, by Jonathan Wise
-- Add user profiles to Mail.app
display dialog "Choose the Mail account to use" buttons {"Jon", "Nicole"} default button 1 with icon note
if the button returned of the result is "Jon" then
  tell application "Mail"
    activate
    delay 2
    set enabled of account "Nicole Home" to false
    set enabled of account "Jon Home" to true
  end tell
else
  tell application "Mail"
    activate
    delay 2
    set enabled of account "Jon Home" to false
    set enabled of account "Nicole Home" to true
  end tell
end if

11 responses so far