2017-02-07 4 views
0

Ich bin ziemlich neu bei Applescript. Ich versuche zu ändern, was eine Variable basierend auf einer If-Bedingung festgelegt ist. Der Benutzer wählt eine Zeit und abhängig davon, welche Zeit er wählt, ändert sich die Variable "Zeit". Ich bekomme den Fehler "Ein Ende der Zeile kann nicht danach gehen" "." "Bezug auf die Zitate nach" 0 ", aber ich muss diese Zahlen als String-Werte festgelegt werden. Nicht sicher, was ich hier so vermisse jede Hilfe ist willkommenVariable innerhalb der if/else-Anweisung setzen?

property time : "12" 
choose from list {"12 am", "1 am", "2 am", "3 am", "4 am", "5 am", "6 am", "7 am", "8 am", "9 am", "10 am", "11 am", "12 pm", "1 pm", "2 pm", "3 pm", "4 pm", "5 pm", "6 pm", "7 pm", "8 pm", "9 pm", "10 pm", "11 pm"} with title "Time Selection" with prompt "What time would you like?" OK button name "This Time" cancel button name "Cancel" default items {"12 am"} 

if answer is equal to "12 am" then 
    set time equal to "0" 
else if answer is equal to "1 am" then 
    set time equal to "1" 
end if 

Antwort

0

es gibt viele Fragen sind:..

  • time ist ein reserviertes Wort Sie es nicht als Variable verwenden
  • set ... equal to ist falsche Syntax, müssen Sie schreiben. set ... to
  • answer steht nicht im Zusammenhang mit dem Ergebnis choose from list.
  • Und selbst wenn die ersten drei Probleme gelöst sind, gibt choose from list eine Liste zurück.

    property myTime : "12" 
    set answer to choose from list {"12 am", "1 am", "2 am", "3 am", "4 am", "5 am", "6 am", "7 am", "8 am", "9 am", "10 am", "11 am", "12 pm", "1 pm", "2 pm", "3 pm", "4 pm", "5 pm", "6 pm", "7 pm", "8 pm", "9 pm", "10 pm", "11 pm"} with title "Time Selection" with prompt "What time would you like?" OK button name "This Time" cancel button name "Cancel" default items {"12 am"} 
    if answer is false then return -- catch if nothing is selected 
    set answer to item 1 of answer -- flatten the list 
    if answer is equal to "12 am" then 
        set myTime to "0" 
    else if answer is equal to "1 am" then 
        set myTime to "1" 
    end if 
    
0

1) Sie nicht "Zeit" als Variablennamen verwenden sollten. Es ist ein reserviertes Wort in Applescript. Wählen Sie zum Beispiel "myTime" als Variablennamen.

2) Die Variable "Antwort" ist in Ihrem Skript nicht definiert. Das Ergebnis der "Auswahl aus Liste" ist in der Standardvariablen "text returned". Diese Variable kann auch "false" zurückgeben, wenn der Benutzer auf die Schaltfläche Abbrechen klickt, anstatt in der Liste zu wählen. für Klarheit des Skripts besser zuweisen formal eine Variable

Dann Skript wird:

set myResponse to choose from list {"12 am", "1 am", "2 am", "3 am", "4 am", "5 am", "6 am", "7 am", "8 am", "9 am", "10 am", "11 am", "12 pm", "1 pm", "2 pm", "3 pm", "4 pm", "5 pm", "6 pm", "7 pm", "8 pm", "9 pm", "10 pm", "11 pm"} with title "Time Selection" with prompt "What time would you like?" OK button name "This Time" cancel button name "Cancel" default items {"12 am"} 

set UserChoice to item 1 of myResponse 
set myTime to "" -- goods practice to initialise a variable 
if UserChoice is "12 am" then 
set myTime to "0" 
else if UserChoice is "1 am" then 
set myTime to "1" 
end if 
log myTime 
Verwandte Themen