dougscripts.com

August 21 2012 - 7:39 pm

Use TextExpander to Run iTunes AppleScripts

I was inspired by this recent article by Dr. Drang on using TextExpander to insert the URL of Safari's front document wherever you're entering text. There might be some benefits to being able to get current information in iTunes with a TextExpander AppleScript snippet or two. Or, as I describe below, three.

In case you don't know, TextExpander is a typing shortcut tool for the Mac whereby you enter a little abbreviated text and a predefined batch of text is inserted where you're typing. One of its amazing features is its ability to fire AppleScript snippets the same way. TextExpander can't accommodate hulking huge AppleScripts but it does allow for some pretty flexible 'scripting.

Before I get to the AppleScript part, here's how to set up TextExpander:
First, click on the TextExpander icon in the Menubar to display its menu and select "Open TextExpander". Create a new Snippet by clicking the "New Snippet" button or pressing Command-N or selecting "New Snippet" from the "+" action menu at the lower-left.

On the right side of the TextExpander window a new empty Snippet Content box appears. At the top of the this box is what looks like a column header named "Content:". It's actually a drop-down popup menu. Click on it and select "AppleScript" from the menu.

The main Content box will be where you will enter the AppleScript.

Below the Content box is a text entry box named "Label:". Here is where you will enter the name of the Snippet. And below that is where you will enter the shortcut to launch the Snippet.

Just to get an idea of how this'll go, enter this AppleScript in the box:

tell application "iTunes"

if player state is playing then

set nom to (get name of current track)

else

return

end if

end tell

(quote & nom & quote) as text

Then give the Snippet a Label and an Abbreviation. TextExpander should look something like this:

Now (you know how TextExpander works, right?), whenever I type ",curtr" the script will be activated and the double-quoted name of the currently playing iTunes track (which only exists if iTunes is actually playing) will appear right at my cursor.

Those are the basics. Some things to keep in mind when creating AppleScripts for TextExpander:

  • The final result of the AppleScript, if it can be coerced to text, will be the output.
  • TextExpander essentially interprets the script as a "one liner". You cannot use properties, globals, or handlers. (TextExpander provides a means to nest script Snippets which I didn't experiment with.)
  • You may want to include a premilinary routine that checks if iTunes is running.

But this still leaves plenty of room to get some interesting data from iTunes to insert in your writing. Let's say you wanted a tab delimited list of tag data from a selection of tracks, say, the name and time of each track:

tell application "iTunes"

set sel to selection

if sel is {} then return

set oput to ""

repeat with aTrack in sel

set oput to (oput & (get name of aTrack) & tab & (get time of aTrack) & return) as text

end repeat

end tell

With a little more finagling you could include additional formatting, HTML list tags or other markup tags.

For a final example, here's a script that toggles iTunes mute. Sure, control iTunes by typing!

tell application "iTunes" to set mute to (not mute)

This won't enter any text, so you can easily mute/unmute iTunes from wherever you're typing—probably faster than you could otherwise depending on the brevity of your abbreviation.

Site contents © 2001 - 2024 (that's right: 2001) Doug Adams and weblished by Doug Adams. Contact support AT dougscripts DOT com. About.
All rights reserved. Privacy.
AppleScript, iTunes, iPod, iPad, and iPhone are registered trademarks of Apple Inc. This site has no direct affiliation with Apple, Inc.
The one who says "it cannot be done" should not be interrupting the one who is doing it.