2017-02-07 15 views
0

私はかなり新しいアプリです。私は変数がif条件に基づいて設定されているものを変更しようとしています。ユーザーは時間を選択し、選択した時間に応じて、変数 '時間'が変化します。私はエラー "行の終わりはこの後に行くことはできません" "" "0"の後の引用を参照していますが、これらの数字を文字列値として設定する必要があります。すべてのヘルプは高く評価されif/else文内に変数を設定しますか?

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 

答えて

0

多くの問題があります。。。

  • timeは予約語である変数としてそれを使用しないでください
  • set ... equal toあなたが書かなければならない、間違った構文でありますset ... to
  • answerchoose from listの結果とは関係ありません。
  • はまた、最初の3つの問題が解決されている場合でも、choose from listリストを返します。あなたは、変数名として "時間" を使うべきではない

    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)。それはApplescriptの予約語です。たとえば、変数名として「myTime」を選択します。

2)変数「答えは」あなたのスクリプトで定義されていません。 «リストから選択»の結果は、デフォルトの変数 "text returned"にあります。この変数は、ユーザーがリスト内で選択するのではなく、キャンセルボタンをクリックした場合にも「false」を返します。スクリプトを明確にするために、より良い正式に割り当てる変数

は、スクリプトは次のようになります。

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 
関連する問題