如何为我的应用程序添加命令以重新运行

how can I add a command for my app to rerun

我正在尝试在按下 "return to formula list" 按钮时让应用程序进入 "rerun"。

我认为这会起作用,因为我基本上让它返回到主列表,但是一旦它返回到列表并进行了另一个选择,它就会退出应用程序。这显然不是全部代码,我只包含了前两个选项。

--DIVE FORMULAS--
--RUN FORMULA LIST--
set x to item 1 of (choose from list {"Divers consumption at depth", "Free Gas Volume (FGV)", "Cylinder Duration", "Partial Pressure", "Initial Depth", "Cylinder Mixing"})

--DIVERS CONSUMPTION AT DEPTH--
if x is "Divers consumption at depth" then

--VARIABLES--
display dialog "What is divers breathing volume (L/min) (default 35 l/min?" default answer "35"
set varDvol to text returned of result
display dialog "What is divers absolute depth pressure (bar)" default answer ""
set varPbar to text returned of result

--FORMULA--
set answer to varDvol * varPbar

--ANSWER--
display dialog "A diver breathing " & varDvol & " l/min, at an absolute pressure of " & varPbar & " bar, will have a consumption of " & answer & " l/min" as text buttons {"See formula", "Return to formula list", "Exit"}

--OPTIONS--
set response to button returned of result

--SEE FORMULA--
if response = "See formula" then
    display dialog "Divers Consumption = Divers Breathing Volume * Absolute pressure of depth (bar)" buttons ("Return to Formula List")

    --RETURN TO FORMULA LIST--
    set response to button returned of result
    if response = "Return to Formula List" then
        set x to item 1 of (choose from list {"Divers consumption at depth", "Free gas volume (FGV)", "Cylinder Duration", "Prartial Pressure", "Initial Depth", "Cylinder Mixing"})
    end if

    --RETURN TO FORMULA LIST--
else if response = "Return to formula list" then
    set x to item 1 of (choose from list {"Divers consumption at depth", "Free gas volume (FGV)", "Cylinder Duration", "Prartial Pressure", "Initial Depth", "Cylinder Mixing"})

    --EXIT APP--
else if response = "Exit" then
    display dialog "Are you sure you wish to exit?" buttons {"Return to Formula List", "Exit"}
    set response to button returned of result
    if response = "Return to Formula List" then
        set x to item 1 of (choose from list {"Divers consumption at depth", "Free gas volume (FGV)", "Cylinder Duration", "Prartial Pressure", "Initial Depth", "Cylinder Mixing"})
    end if
else if response = "EXIT" then
    quit
end if



--FREE GAS VOLUME--
else if x is "Free gas volume (FGV)" then

--VARIABLES--
display dialog "What is the floodable volume of the cylinder (L)?" default answer ""
set varFvol to text returned of result
display dialog "What is the pressure of the cylinder (bar)?" default answer ""
set varCylPress to text returned of result

--FORMULA--
set answer to varFvol * varCylPress

--ANSWER--
display dialog "The free gas volume is (L)" & answer as text buttons {"See formula", "Return to formula list", "Exit"}

--OPTIONS--
set response to button returned of result

--SEE FORMULA--
if response = "See formula" then
    display dialog "FGV = Floodable volume of cylinder * Pressure of cylinder (bar)"

    --RETURN TO FORMULA LIST--
else if response = "Return to formula list" then
    set x to item 1 of (choose from list {"Divers consumption at depth", "Free gas volume (FGV)", "Cylinder Duration", "Partial Pressure", "Initial Depth", "Cylinder Mixing"})

    --EXIT APP--
else if response = "Exit" then
    #do a third thing
end if

就像我说的,"return to formula list" 按钮有效,但是当我从主列表中做出另一个选择时,应用程序关闭,它不会继续到下一个选择的功能,这就是我的我正在寻找

这应该适合你……

你在代码中的任何地方看到这个...

if response = "Return to Formula List" then
    set x to item 1 of (choose from list {"Divers consumption at depth", "Free gas volume (FGV)", "Cylinder Duration", "Prartial Pressure", "Initial Depth", "Cylinder Mixing"})
end if

尝试用这个替换它...

if response = "Return to Formula List" then
    run me
end if

我认为最简单的解决方案是将代码包含在重复循环中,并让重复循环处理所有 'return to list' 操作。然后你可以删除所有重复的choose from list行,你需要做的就是在你想退出时调用exit repeat

--DIVE FORMULAS--
--RUN FORMULA LIST--
repeat
    set x to item 1 of (choose from list {"Divers consumption at depth", "Free Gas Volume (FGV)", "Cylinder Duration", "Partial Pressure", "Initial Depth", "Cylinder Mixing"})

    --DIVERS CONSUMPTION AT DEPTH--
    if x is "Divers consumption at depth" then

        --VARIABLES--
        display dialog "What is divers breathing volume (L/min) (default 35 l/min?" default answer "35"
        set varDvol to text returned of result
        display dialog "What is divers absolute depth pressure (bar)" default answer ""
        set varPbar to text returned of result

        --FORMULA--
        set answer to varDvol * varPbar

        --ANSWER--
        display dialog "A diver breathing " & varDvol & " l/min, at an absolute pressure of " & varPbar & " bar, will have a consumption of " & answer & " l/min" as text buttons {"See formula", "Return to formula list", "Exit"}

        set response to button returned of result

        --SEE FORMULA--
        if response = "See formula" then
            display dialog "Divers Consumption = Divers Breathing Volume * Absolute pressure of depth (bar)" buttons ("Return to Formula List")
        else if response = "Exit" then
            display dialog "Are you sure you wish to exit?" buttons {"Return to Formula List", "Exit"}
            set response to button returned of result
            if response = "exit" then
                exit repeat
            end if
        end if

        --FREE GAS VOLUME--
    else if x is "Free gas volume (FGV)" then

        --VARIABLES--
        display dialog "What is the floodable volume of the cylinder (L)?" default answer ""
        set varFvol to text returned of result
        display dialog "What is the pressure of the cylinder (bar)?" default answer ""
        set varCylPress to text returned of result

        --FORMULA--
        set answer to varFvol * varCylPress

        --ANSWER--
        display dialog "The free gas volume is (L)" & answer as text buttons {"See formula", "Return to formula list", "Exit"}

        --OPTIONS--
        set response to button returned of result

        --SEE FORMULA--
        if response = "See formula" then
            display dialog "FGV = Floodable volume of cylinder * Pressure of cylinder (bar)"                
            --EXIT APP--
        else if response = "Exit" then
            #do a third thing
        end if
    end if
end repeat

除非您的应用程序设计为 stay-open 应用程序,否则您无需显式调用 'quit'。退出重复循环并点击最后一行代码后,应用程序将自动终止。

您应该研究处理程序(子例程)的使用,否则您在实现其他选项或扩展脚本中的功能时会很快迷失方向。

实现此目的的一种方法是重构脚本,将主列表放入 repeat 语句,并将每个选择 运行 放在单独的处理程序中。这些处理程序只关心它们的特定功能(也便于查看和调试),并且可以 return 主循环使用的最后一个对话框的按钮,例如:

on run -- main
    repeat -- forever
        set choice to (choose from list {"Divers consumption at depth", "Free Gas Volume (FGV)", "Cylinder Duration", "Partial Pressure", "Initial Depth", "Cylinder Mixing"} cancel button name "Exit") as text — just one item
        if choice is "Divers consumption at depth" then
            set theResult to consumption()
        else if choice is "Free Gas Volume (FGV)" then
            set theResult to volume()
        else if choice is "Cylinder Duration" then
            set theResult to duration()
        else if choice is "Partial Pressure" then
            set theResult to pressure()
        else if choice is "Initial Depth" then
            set theResult to depth()
        else if choice is "Cylinder Mixing" then
            set theResult to mixing()
        end if
        if choice is "false" or theResult is "Exit" then -- exit
            set response to (display dialog "Are you sure you wish to exit?" buttons {"Return to Formula List", "Exit"})
            if button returned of response is "Exit" then exit repeat
        end if
    end repeat
end run

on consumption() -- Divers consumption at depth
    --VARIABLES--
    set response to (display dialog "What is divers breathing volume (L/min) (default 35 l/min?" default answer "35" buttons {"Exit", "OK"} default button 2)
    if button returned of response is "Exit" then return "Exit"
    set varDvol to text returned of response
    set response to (display dialog "What is divers absolute depth pressure (bar)" default answer "1" buttons {"Exit", "OK"} default button 2)
    if button returned of response is "Exit" then return "Exit"
    set varPbar to text returned of response

    --FORMULA--
    set answer to varDvol * varPbar

    --ANSWER--
    set response to (display dialog "A diver breathing " & varDvol & " l/min, at an absolute pressure of " & varPbar & " bar, will have a consumption of " & answer & " l/min" as text buttons {"See formula", "Return to formula list", "Exit"})

    --SEE FORMULA--
    if button returned of response = "See formula" then
        set response to (display dialog "Divers Consumption = Divers Breathing Volume * Absolute pressure of depth (bar)" buttons {"Return to Formula List", "Exit"})
    end if
    return button returned of response
end consumption

on volume() -- Free Gas Volume (FGV)
    set response to (display dialog "volume" buttons {"Exit", "OK"} default button 2)
    return button returned of response
end volume

on duration() -- Cylinder Duration
    set response to (display dialog "duration" buttons {"Exit", "OK"} default button 2)
    return button returned of response
end duration

on pressure() -- Partial Pressure
    set response to (display dialog "pressure" buttons {"Exit", "OK"} default button 2)
    return button returned of response
end pressure

on depth() -- Initial Depth
    set response to (display dialog "depth" buttons {"Exit", "OK"} default button 2)
    return button returned of response
end depth

on mixing() -- Cylinder Mixing
    set response to (display dialog "mixing" buttons {"Exit", "OK"} default button 2)
    return button returned of response
end mixing