如何使用 applescript 移动第一响应者?

发布于 2024-09-08 05:47:28 字数 576 浏览 5 评论 0原文

我想解决 iTunes 中的以下烦恼:在搜索曲目(通过按 cmd+opt+f 移至搜索字段)后,我希望能够播放歌曲列表中的第一首曲目。理想情况下,我希望 cmd+enter 开始播放歌曲列表中的第一首曲目,并将焦点移至歌曲列表。例如,我在搜索框中输入“Highway 61”,按 cmd+enter 键,然后“Like a Rolling Stone”开始播放。

我最初的想法是使用一个 applescript 将焦点从搜索字段移动到歌曲列表,选择第一首歌曲并播放它。

这就是我所得到的:

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

当我尝试运行此脚本时,Applescript 编辑器会抛出语法错误“预期的类名找到标识符”并突出显示responder。该脚本与我在网上找到的许多 applescript 遵循相同的形式。我做错了什么?

旁白/咆哮: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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

弥繁 2024-09-15 05:47:28

Applescript 的语法很特殊,但从某种意义上说,它还不错,因为您拥有统一的 GUI 脚本语言。这曾经是(现在仍然是)令人惊奇的事情。一旦你了解了 Apple 的语言定义,奇怪的 sytanx 也就不那么奇怪了 可以在此处找到

也就是说,您需要打开每个应用程序的 AppleScript 词典来查看定义了哪些名词和动词。
从 AppleScript 的角度来看,将应用程序视为类和方法的库。当然你不能调用库中未定义的方法,对吗?

启动 AppleScript 编辑器,转到文件→打开词典,然后选择 iTunes。您很快就会发现没有定义“第一响应者”这样的名词。

关键是,应用程序通常只公开其内部对象结构,而不是 UI 的每个元素。但通常这就足够了。

如果您想要做的事情无法使用应用程序公开的正确 Applescript 界面来完成,那么作为最后的手段,您可以使用名为“系统事件”的帮助应用程序直接操作 UI 元素。

因此,再次进入“文件”→“打开词典”,这次选择“系统事件”,然后检查“进程套件”的内容。它允许您操纵 UI。例如,要获取所有 UI 元素的列表,请使用

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

Have fun!有一段时间,Applescript 对我来说很糟糕,但一旦我知道我需要经常查阅字典,这并不是一件坏事。

Mac OS X Automation 是一个很好的教程来源。

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.

旧街凉风 2024-09-15 05:47:28

如果我理解正确的话,你不需要 AppleScript 来做到这一点。只需按 Tab 键即可在元素之间移动焦点。例如,就我而言,按 Tab 键会将焦点从搜索框移动到左侧选择栏,然后移动到歌曲主列表,然后再次返回搜索框。

编辑解决您对问题的进一步细化:您可以通过两次击键从搜索到播放 - TAB 将焦点从搜索框移至歌曲列表,然后 SPACE 播放所选内容中的第一首曲目。您还可以使用箭头键选择不同的选项。

不过,只是为了好玩,您可以通过以下一种方法在 AppleScript 中完成整个序列:

tell application "iTunes"
    play first item of (search first library playlist for "Highway 61")
end tell

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
篱下浅笙歌 2024-09-15 05:47:28

AFAIK,iTunes 没有实现命令设置第一响应者。 Applescript studio 实现了这样的命令。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文