Can I restart iTunes remotely from my iPhone/iPad? I am running iTunes, 10.6.1 on my Mac mini running OS X Lion 10.7.4. This Mac mini has my entire media library including a ton of DVDs that were ripped and transcoded to work on Apple TV. I have 3 of the very latest Apple TVs that stream content from the Home Sharing library of my Mac mini. Everything works great most of the time.

I am running to an issue with increasing frequency, where the Apple TVs cannot load the library from the Mac mini (it shows the spinning gear). The only solution I have found is to quit iTunes on the Mac mini and then restart it.

What I would like to do is have something on my iPhone that will allow me to stop and then restart iTunes on the Mac mini. I run Apache on this machine and I have written a little PHP script that will stop and then start iTunes using AppleScript and osascript. It works fine from the Terminal, but not from the web server. I am sure it is a permissions problem, but I do not really want to run the web server under my user account.

Any suggestions? I can buy an app or script something using PHP or AppleScript. I do not want to use just an SSH or VNC client because I want my kids to do this easily as well.
One option would be to run sudo visudo to edit the sudoers file and add the following line:

apacheuser ALL=(youruser) NOPASSWD: /usr/bin/osascript


Be sure to change apacheuser to the account that apache runs under and youruser to the account that iTunes runs under. This allows apache to run AppleScript commands as the chosen user without prompting for a password. You can find more information about this in the sudoers man page.

However, this could introduce a security hole, since the apache user would then be able to perform any action as the user that AppleScript allows. A better option would be to create an intermediary shell script that takes start and stop as an argument. 

#!/bin/sh

case $1 in
    stop)  /usr/bin/osascript -e 'tell application "iTunes" to quit';;
    start) /usr/bin/open /Applications/iTunes.app;;
    *)     echo 'Please use "start" or "stop" as an argument.';;
esac


Save this in a file somewhere, and make it executable with chmod +x. In this case, your line in the sudoers file would now be:

apacheuser ALL=(youruser) NOPASSWD: /Some/Directory/iTunesControl.sh


Then, from your PHP script, call the shell script with:

sudo -u youruser /Some/Directory/iTunesControl.sh stop|start