[macOS翻译]第 21.2 节 经典脚本添加

63 阅读2分钟

本文由 简悦 SimpRead转码, 原文地址 flylib.com

AppleScript: The Definitive Guide, 2nd Edition,2006, (isbn 0596102119, ean 0596102119), by Neuburg M......

21.2. 经典脚本添加

用于 Mac OS 9 或更早版本的脚本加载项与用于 Mac OS X 的脚本加载项是不同的。Mac OS X 类型的 osax 无法在 Mac OS 9 上运行。Mac OS 9 类型的 osax 只有在经过 Carbonized(即其内部的工具箱调用已与 CarbonLib 链接)处理后,才能在 Mac OS X 上运行。一般来说,任何特定的 osax 文件可能只适用于一种系统,而不是两种系统都适用。如果在 Finder 的上下文菜单中出现了 "显示软件包内容 "菜单项,则该 osax 文件肯定适用于 Mac OS X,否则你可能需要查阅 osax 的文档。

Mac OS X 和在 Mac OS X 下运行的 Classic 是分别实现 AppleScript 的,但两者是兼容的,Apple 事件可以在两者之间来回穿梭。这就提出了一个问题:Classic osaxen 的存在对在 Mac OS X 下运行的脚本有何影响?答案似乎是,如果可能的话,以经典应用程序为目标的代码中的任何 osax 术语都将由经典 osax 处理。你可以从显示对话框这样的术语中看出这一点,因为 Mac OS X 和经典版本的该命令所显示的对话框在外观上有所不同。例如

set f to "gromit:Applications (Mac OS 9):SimpleText"
tell application f to display dialog "hello" -- clearly the Classicdisplay dialog 

在我的电脑上,除非已安装的 Mac OS X 脚本附加程序定义了相同的术语,否则无法使用类似英文的术语从 Mac OS X 调用经典脚本附加程序命令。(苹果公司的文档声称有一种方法可以用术语块来实现,但我还没试过)。因此,举例来说,在经典脚本编辑器中运行该脚本是可行的:

min monitor depth -- 8 

但在 Mac OS X 脚本编辑器中却无法编译:

set f to "gromit:Applications (Mac OS 9):SimpleText"
tell application f to get min monitor depth
-- compile-time error: Expected end of line, etc. but found identifier 

最简单的解决方案是使用原始的四字母代码来调用脚本添加命令:

set f to "gromit:Applications (Mac OS 9):SimpleText"
tell application f to «event aevtgmnd» -- 8

但请记住,你的目标必须是经典应用程序。单靠它,同样的 Apple 事件也会失败:

«event aevtgmnd» -- error: «script» doesn't understand the «event aevtgmnd» message 

www.deepl.com 翻译