简体   繁体   English

如何使用applescript移动急救人员?

[英]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. 我想解决iTunes带来的以下烦恼:搜索曲目(按cmd + opt + f移至搜索字段)后,我希望能够播放歌曲列表中的第一首曲目。 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. 理想情况下,我希望cmd + enter开始播放歌曲列表中的第一首曲目,并将焦点移到歌曲列表中。 For example I enter 'Highway 61' in the search box, press cmd+enter and 'Like a Rolling Stone' starts playing. 例如,我在搜索框中输入“ Highway 61”,按cmd + enter,然后开始播放“ Like a Rolling Stone”。

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. 我的最初想法是创建一个applescript,将焦点从搜索字段移至歌曲列表,选择第一首歌曲并播放。

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 . 当我尝试运行此脚本的AppleScript编辑器抛出一个语法错误“预期的类名发现标识符”和亮点responder This script follows the same form as many of the applescripts I've found on the web. 该脚本遵循与我在网络上找到的许多applescript相同的形式。 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. Applescript的语法是特殊的,但是就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 . 一旦您了解了Apple的语言定义(可在此处找到) ,奇怪的sytanx也不是那么奇怪。

That said, you need to open the AppleScript Dictionary of each app to see what kind of nouns and verbs are defined. 也就是说,您需要打开每个应用程序的AppleScript词典,以查看定义了哪些名词和动词。 Think of an app as a library of classes and methods from the point of view of the AppleScript. 从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. 启动AppleScript编辑器,转到文件→打开字典,然后选择iTunes。 You soon find that there's no such noun as first responder is defined. 您很快会发现没有定义first responder这样的名词。

The point is, the app usually only exposes its internal object structure, not every element of the UI. 关键是,应用程序通常仅公开其内部对象结构,而不是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". 如果您想要使用应用程序公开的正确Applescript接口无法完成操作,作为最后的选择,则可以使用名为“系统事件”的助手应用程序直接操作UI元素。

So, go File→Open Dictionary again, and this time choose "System Events", and check the content of "Processes Suite." 因此,再次转到“文件”→“打开字典”,这次选择“系统事件”,并检查“ Processes Suite”的内容。 It allows you to manipulate the UI. 它允许您操纵UI。 For example, to get the list of all UI elements, use 例如,要获取所有UI元素的列表,请使用

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. Applescript对我来说一阵子看起来很恐怖,但是当我得知我需要始终参考字典时,这并不是一件坏事。

Mac OS X Automation is a great source of tutorials. Mac OS X Automation是大量的教程资源。

If I understand correctly, you don't need an AppleScript to do that. 如果我理解正确,则不需要AppleScript。 Just press the Tab key to move the focus between elements. 只需按Tab键即可在元素之间移动焦点。 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. 以我为例,例如,按Tab键会将焦点从搜索框移到左侧的选择栏,然后移到歌曲的主要列表,然后再次移回搜索框。

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. 编辑解决你的问题的进一步细化:你可以搜索到两个击键打得到- TAB就出了搜索框的焦点移动到歌曲列表,然后SPACE在选择播放第一首曲目。 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: 不过,只是为了好玩,这是您可以在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 . AFAIK,iTunes并未实现命令set first responder of Applescript studio implements such command. Applescript Studio实现了这样的命令。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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