Tuesday, June 14, 2011

How to Automatically Pause and Resume Music using AppleScript - Apple

Use AppleScript to Automatically Pause and Resume Music


Source: thenextweb.com

Here’s a tiny tutorial to achieve the same result as Take Five using AppleScript.
If you’re not too sure what AppleScript is, wikipedia explains:
AppleScript is a scripting language created by Apple Inc. and built into Macintosh operating systems, [...] designed to exchange data between and control other applications in order to automate repetitive tasks.
To put simply, it’s a scripting language with a syntax that very closely resembles the English language that lets you control most applications on your Mac. We’ll see that in a minute.
1. First, we need an app to invoke our AppleScript using a global shortcut. I personally recommend FastScripts — a little menubar utility for running AppleScripts, but you can also choose to use QuickSilver or Launchbar. Download and install any of these apps and proceed to the next step.
2. We need to write an AppleScript that will automatically pause iTunes and wait for X minutes before resuming playback again. Launch the AppleScript Editor app from your Utilities folder and create a new script with the following code:
if appIsRunning("iTunes") then
tell application "iTunes"
pause
delay 120
play
end tell
end if
on appIsRunning(appName)
tell application "System Events" to (name of processes) contains appName
end appIsRunning


The code has a delay of 120 seconds or 2 minutes by default, but you can change it to any value you like.
3. Save this script to Macintosh HD/Library/Scripts folder as itunes.scpt. Alternatively, you can click FasctScripts’ menubar icon to “Open Scripts Folder” under FasctScripts menu. Once saved, the script should automatically show up under the FastScript menu.
4. Go to FastScripts’ preferences and navigate to the “Script Shortcuts” tab to assign a global shortcut to this script. I personally use Cmd+Shift+P.
5. Fire up iTunes and start playing your favorite playlist. If and when you want to temporarily pause your music, just hit the global shortcut key you just assigned and the script will do its thing.
Unfortunately, the other popular music apps such as Spotify and Rdio do not support AppleScript natively i.e they do not answer to the pause/play or similar commands. For this reason, it isn’t easy to achieve the same functionality as you can for iTunes. While there is a way to write an AppleScript to do this, it’s not guaranteed to work for you. Here’s the code for Rdio:
if appIsRunning("Rdio") then
tell application "System Events"
tell application "Rdio" to activate
tell process "Rdio" to click menu item "Pause" of menu "Controls" of menu bar 1
   delay 10
tell process "Rdio" to click menu item "Play" of menu "Controls" of menu bar 1
end tell
end if
on appIsRunning(appName)
tell application "System Events" to (name of processes) contains appName
end appIsRunning


Hopefully, these apps will add AppleScript support in the future. Until then, I guess I’ll just have to manually resume playback.
The AppleScript for iTunes presently resumes playback at the set volume instead of fading in the music, so an improvement to the AppleScript would be to add the code to fade in the music after playback resumes. AppleScript can achieve some really complex tasks with a few lines of code, so I’ll definitely update this post.

Enhanced by Zemanta