Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Executing applescript via web page

I have written a simple Applescript which changes the Airplay speakers in iTunes. This works fine if executed from the command line using osascript or by executing an app version of the script. However, I need a way to execute this script or app remotely via apache. In other words I want to be able to go to a URL that has the effect of switching the speakers. It appears that this is not possible as I find that using PHP or Python to execute the osascript or the app results in a -10810 error. I presume this is because I am trying to get apache to run a script which is dependent on a logged in user's running copy of iTunes. If this is not possible can anyone suggest a way to accomplish the speaker switch remotely?

Posted on Jul 4, 2015 6:32 PM

Reply
5 replies

Jul 7, 2015 2:20 AM in response to Ira Fuchs

You could use VNC or SSH, but in lieu of a web solution, you can use Apple's Mail client to send an Email to yourself from a remote computer or iOS device that triggers a rule action to run an AppleScript on the target computer. You could set up a different rule for each AirPlay device. Scripts are saved to /Users/<username>/Library/Application Scripts/com.apple.mail. A basic script might look like this:

tell application "iTunes" activate delay 3 try set current AirPlay devices to AirPlay device "Apple TV" end try end tell


You set up rules from the Rules pane in Mail's Preferences. You might set up rules something like this:

User uploaded file

User uploaded file


Mail (Yosemite): Use scripts as rule actions

iTunes: About playing music with AirPlay - Apple Support

Jul 7, 2015 3:34 AM in response to Ira Fuchs

Another option is use Messages' AppleScript handler and send a text message to yourself from another computer or iOS device to trigger an AppleScript. Scripts are saved to /Users/<username>/Library/Application Scripts/com.apple.iChat. Select the script to use from the General pane of Messages' Preferences.

User uploaded file


A Script sample:

using terms from application "Messages" on message received theMessage from theBuddy for theChat if theMessage begins with "/Apple TV" then try tell application "iTunes" activate delay 3 set current AirPlay devices to AirPlay device "Apple TV" end tell end try end if end message received end using terms from


You'll need to export the script (File > Export from the menu bar) in a text format, which has an .applescript extension rather than in a script file format with a .scpt extension:

User uploaded file


You'll also need to give the Messages.app accessibility permission by adding it to the list to control your computer; navigate to  > System Preferences > Security & Privacy > Privacy > Accessibility and click on the plus button to select and add.


Once set up, text yourself with the message text you used in the script, "/Apple TV" in the example. I haven't used this method enough to access it's reliability, nor tested it outside a LAN.

Jul 7, 2015 5:59 AM in response to Roote

Thanks for your replies. These are excellent suggestions. I devised and implemented another somewhat similar way to achieve my objective. I wrote a tiny python script that is in the cgi directory. When the URL is called it opens a socket to another python script that the logged in user runs and sends a packet which causes the script to execute the applescript. It works well.

Executing applescript via web page

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.