【问题标题】:AppleScript : variable is not definedAppleScript:未定义变量
【发布时间】:2017-07-29 09:33:45
【问题描述】:

我有一个从 plist 文件中提取的变量(这是我想出的将变量从一个脚本传递到另一个脚本的最佳方法)

它工作正常,如果我做一个随机的“显示通知 var1”我有正确的结果。

但是,如果我想在函数 ex. 中传递变量。

set var1 to ""
set the plistfile_path to "~/Desktop/_DATA.plist"

tell application "System Events"
    set p_list to property list file (plistfile_path)
    -- read the plist data

    set var1 to value of property list item "DSID" of p_list as text
end tell

[...]

on makeStatusBar()
    set bar to current application's NSStatusBar's systemStatusBar

    set StatusItem to bar's statusItemWithLength:-1.0

    -- set up the initial NSStatusBars title
    StatusItem's setTitle:var1
    -- set up the initial NSMenu of the statusbar
    set newMenu to current application's NSMenu's alloc()'s initWithTitle:"Custom"

    newMenu's setDelegate:me (*
    Requied delegation for when the Status bar Menu is clicked  the menu will use the delegates method (menuNeedsUpdate:(menu)) to run dynamically update.


    *)

    StatusItem's setMenu:newMenu

end makeStatusBar

我有这个错误

"变量 var1 未定义。变量 var1 未定义。 (-2753)"

我该如何解决这个问题?提前谢谢你。

【问题讨论】:

    标签: function variables applescript handler var


    【解决方案1】:

    var1 在本地范围内。将其声明为属性:

    property var1 : ""
    

    或者将其声明为全局:

    global var1
    set var1 to ""
    

    另一个变量theDSIDFromPlist没有出现在代码中。

    【讨论】:

    • 是的,这是有道理的,我习惯于“将 var 设置为:”,这是不正确的。完美,谢谢!
    【解决方案2】:

    我不确定您是否提供了所有相关代码。我无法完全重现您的问题。

    我的方法是在脚本编辑器中调试脚本并添加一些 Try 块以更好地管理错误。

    有问题的变量既没有在你的 sn-p 中查询也没有定义,所以这实际上是任何人都可以为你做的。

    干杯,拉尔夫

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-06-11
      • 2021-02-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多