Conditional AppleScript as a sanity saver
Like many Mac users, I like to have a keystroke utility running to make repetitive tasks easier. My drink of choice here is Script Software's iKey, but you can do similar things with other programs (Keyboard Maestro, Quicksilver, QuicKeys, etc.). Two of the most basic functions I have iKey set to assign keystrokes to are 1) controlling iTunes and 2) opening frequently used folders. The latter task is something iKey can do natively, with shortcuts built in. But for years I've relied on AppleScript for the iTunes controls, and fortunately, iKey can easily be set to fire AppleScript .scpt documents as well, so it's all good. However, there are some annoyances, the main one being that (for example) if I accidentally hit my iTunes play/pause keystroke, and iTunes isn't running, then iTunes will launch and start playing. So what's to be done? Writing some conditional AppleScript, that's what. Now, the following isn't exactly new information, and I won't pretend that you can't fire up Google and find dozens of other similar tutorials, but in the hopes of helping anyone else who has the same needs I do, here goes:
Let's stay with our iTunes play/pause example, and you can follow along at home, kids! First, open Script Editor (found in the /Applications/AppleScript folder). Then, type in the bare minimum for telling iTunes to do its play/pause thing:
tell application "iTunes"
playpause
end tell
Hit the "Run" button, and iTunes will launch and start playing. If you leave iTunes open and go back to the Script Editor and hit "Run" again, iTunes will pause. Simple enough, right? It's all fine and good when iTunes is running, but it's the iTunes launching and playing part I wanted to get rid of. In a nutshell, the script is spot on if iTunes is open, but if it's not, I don't want anything to happen when the script is run. So, it's just a matter of checking to see if iTunes is running first, and if it is, then fire the playpause action. So here's how that would look:
tell application "System Events"
if exists process "iTunes" then
tell application "iTunes"
playpause
end tell
end if
end tell
Even if you're predisposed to have your eyes glaze over at even the mere mention of code, this should be easy enough to follow. Put in plain(er) English:
Hey, Mac!
Is iTunes running?
OK, good! Tell it to pause if it's playing, or play if it's paused.
So, now when I assign a keystroke to that script in iKey, iTunes will no longer launch if it's not open, and do what I want it to if it is. Problem solved.
Which brings me to the second of the basic functions I alluded to earlier: using iKey to open frequently used folders. If I moved those calls into AppleScript, it would solve a newer problem I've created for myself. You see, depending on the task, I find myself toggling back and forth between the Finder and Cocoatech's Path Finder. What I want is for the same keystroke to open a directory in whichever program I have open. So it's time for another conditional ActionScript. Let's say I have a drive in my machine called Dev (which I do, but let's act as if we're being all hypothetical and stuff). The script I need would check to see if Path Finder is running, and if it is, to reveal the root of that drive as a new tab in the Path Finder window. If Path Finder is not running, I want the same script to open the root of that drive in the Finder. So here's what that script would look like:
tell application "System Events"
if exists process "Path Finder" then
tell application "Path Finder"
reveal "Dev"
activate
end tell
else
tell application "Finder"
open "Dev"
activate
end tell
end if
end tell
Here's the conversational version, which won't actually work in the Script Editor but may explain things further:
Hey, Mac!
Is Path Finder running?
Well, if it is, tell it to reveal my Dev drive first, and then make sure Path Finder is the active program
If it's not, then tell the Finder to open the same drive and then make the Finder the active program.
Simple enough, right? One thing of note here, though. The use of reveal versus open. In Path Finder, you can effectively force a single window mode if you reveal directories instead of opening them, which will show items in a new tab in the same window instead of forcing open new windows. So that's important to heavy Path Finder users. In the tabless Finder, the open command is just fine (though reveal will also work, but it behaves somewhat strangely). In any event, I can assign that script a keystroke in iKey, and my Dev drive will open in Path Finder if I have that running, and open in the Finder otherwise.
Well, there you have it. If these scenarios don't apply to you, the concept is easily adaptable to other programs and situations.
Posted at 01:42PM Jul 17, 2007 by Kevin Schmitt in Mac and Apple | Comments[0]