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

时间:2019-07-23 05:32:35

标签: button applescript

当我按“返回公式列表”按钮时,我正在尝试使应用程序“重新运行”。

我认为这会行得通,因为我基本上可以将其返回到主列表,但是一旦回到列表并进行了其他选择,它将退出应用程序。这显然不是全部代码,我只包括了前两个选项。

--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

就像我说的那样,“返回公式列表”按钮有效,但是当我从主列表中进行另一选择时,应用程序关闭,它将不会继续执行下一个选定的功能,这就是我要寻找

3 个答案:

答案 0 :(得分:2)

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

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

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

答案 1 :(得分:1)

我认为,最简单的解决方案是将代码包含在重复循环中,然后让重复循环处理所有“返回列表”操作。然后,您可以删除所有重复的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

除非您的应用程序被设计为保持开放状态的应用程序,否则您无需显式调用“退出”。退出重复循环并点击最后一行代码后,该应用程序将自动终止。

答案 2 :(得分:0)

这应该为您工作...

在代码中的任何地方都可以看到此...

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