如何学习 Applescript

How to learn Applescript

我是编程新手。我可以写一些 Python 代码,我想用脚本编辑器在 Macbook 上写一些脚本。

那么学习 AppleScript 的最佳方法是什么?我需要什么 material?

我尝试通过 language guide and Window Anatomy 学习 AppleScript。

但是当我试图阅读一些代码时,我无法理解很多语句。

示例:

tell application "System Preferences"
    activate
    set current pane to pane "com.apple.preference.sound" --I can't understand "com.apple.preference.sound".What's the rule of strings like this?
end tell
tell application "System Events"
    tell application process "System Preferences"
        tell tab group of window "Sound"
            
            click radio button "output" --There,I don't exactly know what's radio button,pane,table is.
            
            if (selected of row 1 of table 1 of scroll area 1) then
                set selected of row 2 of table 1 of scroll area 1 to true
                set deviceselected to "外置音响"
            else if (selected of row 2 of table 1 of scroll area 1) then
                set selected of row 3 of table 1 of scroll area 1 to true
                set deviceselected to "LG"
            else
                set selected of row 1 of table 1 of scroll area 1 to true
                set deviceselected to "MAC"
            end if
        end tell
    end tell
end tell

看来我不知道语言语法之外的这些概念。我怎样才能学到这些东西?

首选项面板的名称不是 AppleScript 语言的一部分。

声音首选项面板的权限是/System/Library/PreferencePanes/Sound.prefPane/Contents/Info.plist。在那里,你可以注意到

<key>CFBundleIdentifier</key>
<string>com.apple.preference.sound</string>

这是窗格的名称。

您可以参考How to know the name of UI elements using Accessibility inspector (or any other tool) and AppleScript – How can I get UI elements' names, attributes, properties, classes programmatically without "guessing" via AccessibilityInspector?了解如何了解radio button "output"部分。

我可以推荐 2 本书来学习 AppleScript:

  1. AppleScript 1-2-3,AppleScript 学习自定进度指南,作者:Sal Soghoian 和 Bill Cheeseman
  2. 学习 AppleScript,Mac OS X 上的脚本和自动化综合指南(第 3 版),作者:Hamish Sanderson 和 Hanaan Rosenthal

第一本书适合脚本初学者,但这两本书互为补充。 有许多示例脚本,两本书的作者都讨论了使用 AppleScript 编写脚本的基础关键概念。

第一本书的作者 Sal Soghoian 曾在 Apple 担任多年自动化技术产品经理。

  1. 字符串为字符串(字符序列;写成文字-代码-形式时用引号括起来)。该特定字符串的内容是一个 Uniform Type Identifier,这只是一个命名约定,很像 URL 中的域名,除了向后。

  2. 如果您真的非常需要,了解各种 GUI 小部件的名称需要 passing familiarity with GUI programming. e.g. Radio buttons are the circular buttons which come in groups where you can pick any one. There is an helpful app GUI 脚本。此外,GUI 脚本是笨拙的脆弱的猪,只有在应用程序不提供自己的脚本界面时才作为最后的手段使用。这给我们带来了一个更普遍的观点……

  3. 要了解 AppleScriptable 应用程序中存在的各种类型的对象的名称,以及它们包含的信息以及如何使用命令操作它们,需要阅读该应用程序的 AppleScript 字典(文件 > 打开脚本编辑器中的字典)加上一些智能猜测(因为应用程序字典几乎总是在细节上不充分)。一本好的 AppleScript 书籍将帮助您发展这项技能,并在漫长的 运行 中为您省去大量的麻烦和时间。 (顺便说一句,我是@BernardR 第 2 本书的主要作者。)

  4. 如果你认真学习 AppleScript,我强烈建议你学习 Script Debugger。它远远优于 Apple 的脚本编辑器,不仅可以探索应用程序字典,还可以探索 运行ning 应用程序本身中的活动对象。那里的用户对用户论坛也提供针对 AppleScript 的建议。

  5. 最后,如果您熟悉面向对象的编程语言,例如 Python,您会发现 AppleScript 在谈论可编写脚本的应用程序时使用了很多相同的行话:类、对象等 不要被愚弄。 应用程序脚本(Apple 事件 IPC)不是 OOP:它是远程过程调用传递简单的 queries 作为参数。查询标识您希望命令操作的数据(例如 size of first word of every paragraph of front document),命令告诉应用程序如何处理它找到的任何数据(例如 set (size of…) to 18)。因此,在 AppleScript 中,您可以执行以下操作:

    告诉应用程序“TextEdit” 设置前面文档每段第一个字的大小为18 结束告诉

这不是您可以用 OOP 表达的概念;它比那更强大。 (顺便说一句,这就是程序员讨厌 AppleScript 的原因:因为他们错误地认为应用程序脚本是 OOP;然后当它以某种深刻的非 OOP 方式运行时,就会让他们感到困惑。)

--

p.s。祝你好运。您将需要它,因为它既很棒又令人沮丧。 (虽然令人敬畏的部分通常使它值得为到达那里所费尽心思。)