如何对按钮激活的applescript进行重复命令?

时间:2015-05-18 23:42:25

标签: applescript

我有一个AppleScript代码来选择你自己的冒险游戏。我想在单击按钮时创建一个重复整个代码的命令。 这就是我的所有代码:

set volume 3
beep
set volume 5
delay 0.5
beep
set volume 7
delay 0.5
beep
delay 0.5
display dialog "Hello? Anyone there? SOS, repeat, SOS!" buttons {"Hello", "Who is this?"} default button 1
if the button returned of the result is "Who is this?" then
    display dialog "Nick. Nick Gilbert." buttons {"Ok, What happened?"} default button 1
else
    display dialog "Oh, Thank god, human contact! I've been longing for this to work and now it is! The name's Nick Gilbert, by the way." buttons {"Ok, what happened?"} default button 1
end if
display dialog "Me and the rest of the 42nd mission were heading to the international space station on a standard repair mission." buttons {"Next"} default button 1
display dialog "We docked with it ok, but then an explosion from an engine in the shuttle killed all on board other than me. The International Space Station was damaged too." buttons {"Next"} default button 1
display dialog "I couldn't contact Earth, so I built this transmitter. Somehow It can only reach you." buttons {"Lucky me", "Go on"} default button 1
if the button returned of the result is "Lucky me" then
    display dialog "Lucky you" buttons {"Go on"} default button 1
else
    display dialog "Whoops, I dropped my radio. What was that?" buttons {"Go on"}
end if
display dialog "Well, The reason we were on a repair mission is because the International Space Station is heading towards Mars! I should be able to survive on Mars because there are space suits on board, but NASA didn't let the public in on a secret." buttons {"What's that?", "Let me guess, Aliens?"} default button 1
if the button returned of the result is "What's that?" then
    display dialog "Martians. And no, they are not peaceful. These 'Martians' are evil. I know that because a fellow astronaut was killed by one when he was exploring Mars. I saw the whole thing through the camera. They are evil and they will kill me." buttons {"Oh no!"} default button 1
else
    display dialog "Yup, Aliens. And these aliens are hostile." buttons {"Oh no!"} default button 1
end if
display dialog "Oh no is right. What should I do?" buttons {"Look for reverse thrusters", "Try to radio NASA again"} default button 1
if the button returned of the result is "Look for reverse thrusters" then
    display dialog "Nick is looking for reverse thrusters" buttons {""} giving up after 5.0
    display dialog "Nope, No reverse thrusters. I found a machete though :)" buttons {"Darn it"} default button 1
else
    display dialog "Nick is trying to call NASA again" buttons {""} giving up after 5.0
    display dialog "Nope, not working." buttons {"Darn it"}
end if

display dialog "Maybye I should try the escape pod" buttons {"Yes", "No"} default button 1
if the button returned of the result is "Yes" then
    display dialog "The escape pods are online!" buttons {"YAY!"} default button 1
    display dialog "Launching in 10" buttons {""} giving up after 1.0
    display dialog "9" buttons {""} giving up after 1.0
    display dialog "8" buttons {""} giving up after 1.0
    display dialog "7" buttons {""} giving up after 1.0
    display dialog "6" buttons {""} giving up after 1.0
    display dialog "5" buttons {""} giving up after 1.0
    display dialog "4" buttons {""} giving up after 1.0
    display dialog "3" buttons {""} giving up after 1.0
    display dialog "2" buttons {""} giving up after 1.0
    display dialog "1" buttons {""} giving up after 1.0
    display dialog "Escape pod fired" buttons {"Ye-ha!"} default button 1
    delay 2.0
    beep
    beep
    beep
    display dialog "EMERGENCY EMERGENCY! Escape pod going to Mars! EMERGENCY EMERGENCY!" buttons {"Oh no!"} default button 1
    display dialog "Roll the dice. If the answer is 1 - 3 then Nick dies. If the answer is 4 - 6 then Nick lives" buttons {"Roll the Dice"}
    set x to random number 6
    display dialog (x)
    if (x) is less than 4 then
        display dialog "Nick dies" buttons {"Start again", "Quit"}
        if button returned of the result is "Quit" then
            tell "His life in your hands" to quit
        else
            #This is where i need the command
        end if
    else
        display dialog "Nick lands safely on Mars" buttons {"Phew!"}
    end if
else
    display dialog "That's probably the best choice anyway. What should I do?" buttons {"Look for useful items", "Man the controls"} default button 1
end if

我想在命令下重复脚本。 谢谢!

1 个答案:

答案 0 :(得分:0)

您是否尝试将整个代码放在处理程序中?以这种方式,您可以从脚本中的任何位置访问它,包括处理程序本身的内部。试试这个:

set theButton to button returned of (display dialog "Hello" buttons {"Hello", "goodbye"})
if theButton is "Hello" then my GoSub()

to GoSub()
    set subButton to button returned of (display dialog "Hello" buttons {"Hello Again", "goodbye Forever"})
    if subButton is "Hello Again" then my GoSub()
end GoSub

你的代码很长,但这基本上就是我所说的:

delay 0.5

    my StartGame()

    on StartGame()
        display dialog "Hello? Anyone there? SOS, repeat, SOS!" buttons {"Hello", "Who is this?"} default button 1
    .....The rest of your code......
    else
                    --#This is where i need the command
                    my StartGame()
                end if
            else
                display dialog "Nick lands safely on Mars" buttons {"Phew!"}
            end if
        else
            display dialog "That's probably the best choice anyway. What should I do?" buttons {"Look for useful items", "Man the controls"} default button 1
        end if
    end StartGame