Show, Hide or Launch iTunes in Context – AppleScript

itunes-icon.pngIt isn’t often that iTunes isn’t running on my Mac. Music is the lubricant of my life, and doubtlessly I’m not alone in this. Here is the best way to quickly bring iTunes to the front, hide it if that’s where it currently is, or launch it otherwise. Copy the following code into Script Editor, compile it (Cmd + K) and save it to your scripts folder.

tell application "System Events"
	set iTunesState to count (every process whose name is "iTunes")
	if iTunesState is 0 then
		tell application "iTunes"
			activate
		end tell
	else
		if frontmost of process "iTunes" is true then
			set visible of process "iTunes" to false
		else
			tell application "iTunes"
				activate
			end tell
		end if
	end if
end tell

It’s really fast! I’ve mapped it to F2 so iTunes is always a keystroke away.

A problem that I can’t presently figure, however, is as follows. iTunes is an application that will continue to run (that is, play music) even if you close the window (by pressing Cmd + W or selecting “iTunes” under “Window” in the menu bar). If this is the case, iTunes won’t open the window back up, but only come the to the front. Hmmm… I’ll get to the bottom of it.

As always, if you know a better way to do this (or can solve my window problem) – AppleScript or otherwise – let me know in the comments.

1 Response to Show, Hide or Launch iTunes in Context – AppleScript

  1. Open the window with Cmd-Option-1 or Window > iTunes.

    Applescript: tell application “iTunes” to set visible of front browser window to true

    (I thought up that as I was writing “there’s no way to script a new browser window…”)

Think different?