简体   繁体   中英

How do I move first responder with applescript?

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.

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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM