如何将全局变量从一个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
从此文件执行时,此方法正常。我想将此文件用作库,与找到here的方式类似。我的第二个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。”编号-2753来自 “someDirectory”
如何将此变量导入我的第二个AppleScript文件?
答案 0 :(得分:3)
在加载脚本时,您实际上并未运行该脚本,因此someDirectory永远不会被设置。您可以通过将其设为属性来解决此问题。所以改变这个......
global someDirectory
set someDirectory to "~/Documents/cs123-drj/demo"
为...
property someDirectory: "~/Documents/cs123-drj/demo"