【问题标题】:Import global variable in AppleScript在 AppleScript 中导入全局变量
【发布时间】:2012-05-29 13:50:00
【问题描述】:

如何将全局变量从一个 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 从 “一些目录”

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

【问题讨论】:

    标签: applescript


    【解决方案1】:

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

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

    到...

    property someDirectory: "~/Documents/cs123-drj/demo"
    

    【讨论】:

    • 完美!正是我需要的。
    猜你喜欢
    • 2018-07-23
    • 1970-01-01
    • 1970-01-01
    • 2019-01-20
    • 2011-06-10
    • 1970-01-01
    • 2021-12-10
    • 1970-01-01
    • 2023-03-04
    相关资源
    最近更新 更多