If you weren’t around yesterday, Iwrote abouta brilliant little menubar utility for the Mac calledTake Five, which lets you pause and automatically resume iTunes playback after X minutes.

If you don’t want to spend $2 on the app, or you don’t need a UI for a simple action like this, here’s a tiny tutorial to achieve the same result as Take Five using AppleScript.

If you’re not too sure whatAppleScript 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.

Calling all Scaleup founders! Join the Soonicorn Summit on November 28 in Amsterdam.

Meet with the leaders of Picnic, Miro, Carbon Equity and more during this exclusive event dedicated to Scaleup Founders!

  1. We need to write an AppleScript that will automatically pause iTunes and wait for X minutes before resuming playback again. Launch theAppleScript Editorapp from your Utilities folder and create a new script with the following code:

if appIsRunning(“iTunes”) thentell application “iTunes"pausedelay 120playend tellend if

on appIsRunning(appName)tell application “System Events” to (name of processes) contains appNameend appIsRunning

The code has a delay of 120 seconds or 2 minutes by default, but you can change it to any value you like.

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

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

  3. 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 asSpotifyandRdiodo 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”) thentell application “System Events"tell application “Rdio” to activatetell process “Rdio” to click menu item “Pause” of menu “Controls” of menu bar 1

delay 10tell process “Rdio” to click menu item “Play” of menu “Controls” of menu bar 1

end tellend if

on appIsRunning(appName)tell application “System Events” to (name of processes) contains appNameend 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.

Let us know how this works out for you in the comments.

Story byPreshit Deorukhkar

Preshit is our India Editor based out of Bombay. A technology enthusiast at heart, he has immense passion for everything Apple and gadgets i(show all)Preshit is our India Editor based out of Bombay. A technology enthusiast at heart, he has immense passion for everything Apple and gadgets in general. You can follow him ontwitteror hispersonal blog.

Get the TNW newsletter

Get the most important tech news in your inbox each week.

Also tagged with