views:

45

answers:

3

I want to address the following annoyance with iTunes: After I have searched for a track (by pressing cmd+opt+f to move to the search field) I want to be able to play the first track in the songs list. Ideally I would like cmd+enter to start playing the first track in the song list and also move the focus to the song list. For example I enter 'Highway 61' in the search box, press cmd+enter and 'Like a Rolling Stone' starts playing.

My initial idea is for an applescript that moves the focus from the search field to the song list, selects the first song and plays it.

Here's what I have:

tell application "iTunes"
    set first responder of (window 1) to outline "songs"
end tell

When I try to run this script Applescript Editor throws a syntax error "Expected class name found identifier" and highlights responder. This script follows the same form as many of the applescripts I've found on the web. What am I doing wrong?

Aside/rant: Applescript is the most frustrating and confusing technology I've ever had the stupidity to inflict upon myself. I hate it. I hate it. I hate it. I hate it.

+1  A: 

Applescript's syntax is idiosyncratic, but it's not bad in the sense that you have a uniform scripting language for GUI. This was (and still is) something amazing. The strange sytanx is also not that strange once you go through Apple's language definition which can be found here.

That said, you need to open the AppleScript Dictionary of each app to see what kind of nouns and verbs are defined. Think of an app as a library of classes and methods from the point of view of the AppleScript. Of course you can't call a method which is not defined in a library, right?

Launch AppleScript Editor, go File→Open Dictionary, and choose iTunes. You soon find that there's no such noun as first responder is defined.

The point is, the app usually only exposes its internal object structure, not every element of the UI. But usually that's enough.

If what you want to do cannot be done using the proper Applescript interface an app exposes, as a last resort, you can directly manipulate the UI elements, using a helper app called "System Events".

So, go File→Open Dictionary again, and this time choose "System Events", and check the content of "Processes Suite." It allows you to manipulate the UI. For example, to get the list of all UI elements, use

tell application "System Events"
   tell application process "iTunes"
            get UI elements of window 1
   end tell
end tell

Have fun! Applescript looked horrible to me for a while, but it's not a bad thing once I learned I need to always refer to the dictionary.

Mac OS X Automation is a great source of tutorials.

Yuji
Good advice. Keep in mind that manipulating the UI elements is a technique of last resort if the application does not provide a useful Apple Events interface of its own. iTunes *does* provide a useful interface so chances are you shouldn't need to be working at that level here. See my comment above.
Ned Deily
I heartily agree with what you said.
Yuji
Thank you. That's given me something to work with.
Benedict Cohen
+1 for pushing the Dictionary; the Dictionary knows all. Also +1 for the comment of GUI Scripting being the last resort of Applescript.
Philip Regan
A: 

If I understand correctly, you don't need an AppleScript to do that. Just press the Tab key to move the focus between elements. In my case, for example, pressing Tab moves the focus from the search box to the left-side selection bar then to the main list of songs then back to the search box again.

EDIT Addressing your further refinement of the question: you can get from searching to playing with two key strokes - TAB to move the focus out of the search box to the song list, then SPACE to play the first track in the selection. You can also use the arrow keys to pick a different selection.

Just for fun, though, here's one way you could do the whole sequence in AppleScript:

tell application "iTunes"
    play first item of (search first library playlist for "Highway 61")
end tell
Ned Deily
You're correct, tab works perfectly (although strangely not in grid view). However I did a terrible job of explain my problem so this doesn't address the issue. I've updated the question.
Benedict Cohen
Seems to work for me in any of the views: list, grid, or cover flow.
Ned Deily
A: 

AFAIK, iTunes doesn't implement the command set first responder of. Applescript studio implements such command.

kiamlaluno