WARNING: Please read before continuing ▼
Difficulty: Expert
Time: varies
Last Updated: 24th October 2009
Applies to: OS X
Applescript is a scripting language provided by OS X that has the ability to automate significant portions of the system.
PHP has the ability to, given the right configuration, make arbitrary system calls, including applescript.
Using the two together, one can remotely control a number of functions on an OS X system via a web page.
In the example, Apache has to be run as the logged in user. This is a security risk, and should not be used in a production system. If you are planning on using this technique for anything serious, you should write an extra program to relay commands between PHP and the logged in user.
You should also be aware that applescript can control a significant number of system functions, meaning that if a malicious user finds a flaw in your code, they can do just about anything. With that said…
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | <?php switch ($_GET['do']) { case "next": exec("osascript -e 'tell application \"iTunes\" to next track'"); break; case "previous": exec("osascript -e 'tell application \"iTunes\" to previous track'"); break; case "play": exec("osascript -e 'tell application \"iTunes\" to play'"); break; case "pause": exec("osascript -e 'tell application \"iTunes\" to pause'"); break; default: break; } ?> <html> <head> <title>iTunes Web Controller</title> </head> <body> <a href="<?php echo $_SERVER['PHP_SELF']; ?>?do=play">Play</a> <br /> <a href="<?php echo $_SERVER['PHP_SELF']; ?>?do=pause">Pause</a> <br /> <a href="<?php echo $_SERVER['PHP_SELF']; ?>?do=next">Next</a> <br /> <a href="<?php echo $_SERVER['PHP_SELF']; ?>?do=previous">Previous</a> <br /> </body> </html> |
osascript -e is the command used to execute applescipt commands. exec may not be available depending on your security configuration.
The developer documentation for applescript can be found here if anyone is interested in further developing the sample.
Code: