문제

I am working on the same app that I mentioned in my first question. I have gotten much farther, but when I try to play "2048" the first time, AppleScript give me the error:

"The variable result is not defined"

I will skip the main bulky body of the app and get to the area with the problem:

display dialog "What would you like to do?
    Sleep = Go to sleep
    Finder = Open Finder
    Time = Display current time and date
    2048 = play 2048
    Quit = Quit application" default answer "" with title "Control panel"
if the text returned of the result is "Sleep" then
    tell application "System Events" to sleep
    display dialog "Hello, welcome back!" buttons {"Thank you!"} default button 1
    return
else if the text returned of the result is "Finder" then
    tell application "Finder" to make new Finder window
else if the text returned of the result is "Time" then
    set a to (current date) as list
    set AppleScript's text item delimiters to linefeed
    set a to a as text
    display dialog a buttons {"OK"} default button 1
else if the text returned of the result is "Quit" then
    return
else if the text returned of the result is "2048" then
    tell application "Terminal"
            do script "/Users/student/Documents/2048.sh"
            activate
    end tell
else
    display dialog "Invalid response" with title "Invalid response" buttons {"Go back", "Quit"} default button 1
end if
if the button returned of the result is "Go back" then
    display dialog "What would you like to do?
    Sleep = Go to sleep
    Finder = Open Finder
    Time = Display current time and date
    2048 = play 2048
    Quit = Quit application" default answer "" with title "Control panel"
else
    return
end if
if the text returned of the result is "Sleep" then
    tell application "System Events" to sleep
    display dialog "Hello, welcome back!" buttons {"Thank you!"} default button 1
    return
else if the text returned of the result is "Finder" then
    tell application "Finder" to make new Finder window
else if the text returned of the result is "Time" then
    set a to (current date) as list
    set AppleScript's text item delimiters to linefeed
    set a to a as text
    display dialog a buttons {"OK"} default button 1
else if the text returned of the result is "Quit" then
    return
else if the text returned of the result is "2048" then
    tell application "Terminal"
            do script "/Users/student/Documents/2048.sh"
            activate
    end tell
end if
도움이 되었습니까?

해결책

Just set the result of the display dialog to a variable and use the variable. It's tricky to use "result" as you have in many if statements. One of them is bound to cause a problem because "result" will change at some point.

So do something like this right after your display dialog statement...

set {buttonReturned, textReturned} to {button returned of result, text returned of result}

Then in your if statements use buttonReturned or textReturned.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top