1

如何将全局变量从一个 AppleScript 文件导入另一个?

我正在使用两个 AppleScript 文件为项目课程创建演示。

一个 AppleScript 文件“main.scpt”以全局变量开头

global someDirectory
set someDirectory to "~/Documents/cs123-drj/demo"

on openServerWindow()
    # Open the server
    tell application "System Events" to keystroke "n" using command down
    tell application "System Events" to keystroke "i" using {command down, shift down}
    typeKeys("server")
    typeKeys(return)
    tell application "System Events" to keystroke "i" using command down
    typeKeys("cd ")
    typeKeys(someDirectory)
    typeKeys(return)
    typeKeys("./cs123-server.sh")
    typeKeys(return)
end openServerWindow

从这个文件执行时,这工作正常。我想将此文件用作库,其方式类似于此处找到的内容。我的第二个 AppleScript 的全文如下。

#
# Demo script for doing simultaneous selects from a CS123-DRJ database.
#

property CS123Commands : load script POSIX file "/Users/admin/Documents/cs123-drj/demo/main.scpt"

tell CS123Commands to openServerWindow()

当我尝试运行此代码时,出现以下错误:

错误“未定义变量 someDirectory。” 来自“someDirectory”的数字 -2753

如何将此变量导入我的第二个 AppleScript 文件?

4

1 回答 1

4

加载脚本时实际上并没有运行脚本,因此 someDirectory 永远不会被设置。您可以通过将其设为属性来解决此问题。所以改变这个...

global someDirectory
set someDirectory to "~/Documents/cs123-drj/demo"

到...

property someDirectory: "~/Documents/cs123-drj/demo"
于 2012-05-29T05:50:00.627 回答