全局变量 "not defined"

Global variable "not defined"

我的 AppleScript 检查图像文件并输出其中的一些信息。我需要此信息作为脚本的正常输出 (stdout) 以进行进一步处理(在 InDesign 脚本中)。但是,一旦我在我的 AppleScript 中打开图像文件,用于保存结果的全局变量就会丢失。

这很好(但没用):

global result
set result to "initialised"

set imgfile to POSIX file "/Users/hell/Pictures/interesting_stuff.jpg" as alias

tell application "Image Events"
    -- start the Image Events application
    launch

    set result to "new text"

end tell

return quoted form of result

下面的脚本是我想要的,但是抛出错误"The variable 'result' is not defined (-2753)"

global result
set result to "initialised"

set imgfile to POSIX file "/Users/me/Pictures/interesting_stuff.jpg" as alias

tell application "Image Events"
    -- start the Image Events application
    launch

    -- open image file
    set img to open imgfile

    -- here would go some commands about img instead of "new text"
    set result to "new text" 

    -- close image file
    close img
end tell

return quoted form of result

这里有什么问题,我怎样才能从我的脚本中得到结果?

result 是 AppleScript 中的一个特殊标识符,您可能会在 Script Editor 中注意到它 以与您使用的其他变量名称不同的 colour/font/style 打印.它是一个 AppleScript 属性,它始终包含在它之前执行的命令返回的值。

该解决方案很可能会为您的标识符选择 result 以外的其他词。

就您的脚本而言,您也不需要将变量声明为 global,因为您的脚本中没有需要访问其块之外的变量的范围界定问题,这变成了创建处理程序或脚本对象时的注意事项。