Hauke
Hauke

Reputation: 471

Global variable "not defined"

My AppleScript examines an image file and outputs some of that information. I need this information as normal output (stdout) of the script for further processing (in an InDesign script). But as soon as I open an image file in my AppleScript, the global variable which is meant to hold the result, gets lost.

This is fine (but useless):

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 following script is what I want, but throws the error "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

What is the problem here and how can I get the result out of my script?

Upvotes: 0

Views: 266

Answers (1)

CJK
CJK

Reputation: 6102

result is a special identifier in AppleScript, which you may notice in Script Editor is printed in a different colour/font/style to other variable names you use. It's an AppleScript property that always contains the value returned by the command that is executed immediately before it.

The solution is most likely to choose a different word other than result for your identifier.

As your script stands, you also don't need to declare the variable as a global, since there are no scoping issues in your script that require access to variables outside of its block, which becomes a consideration when creating handlers or script objects.

Upvotes: 2

Related Questions