2017-07-04 8 views
0

私はリストから料理を無作為に7回選んで食べ物メニューを作るスクリプトを作っています。しかし、似たような料理を選びません。たとえば、 'Lamb Chops'がdish1と選択された場合、dish2は、 'lamb'と 'chops'を含むすべてのオプションが削除された同じリストのランダムな項目になります。次にdish2が 'Spaghetti Bolognese'の場合は、 'lamb'、 'chops'、 'spaghetti'、 'bolognese'などを除いて同じリストからdish3が選択されます。誰も助けることができますか?ランダムなアイテムをリストから選択し、ランダムに選択したアイテムと同じ単語を含む後続のリストアイテムを削除しますか?

- おねがいします。

+0

はあなたのアプローチ –

答えて

1

私はAppleScriptでやや錆びていますので、間違いがあれば私は言い訳をします。

あなたは何をしなければならないか打破するには、

1)

-- The initial menuItems we start off with 
set menuItems to {"Lamb Chops", "Lamb", "Chops", "Spaghetti Bolognese", "Spaghetti", "Bolognese"} 

set randomItemNum to random number from 1 to count of menuItems 
set randomItem to item randomItemNum of menuItems as string 

2)を繰り返してmenuItemsは、私たちはrandomItem
から単語を含むアイテムを除外することができ、ランダムな項目を選択します。このサブルーチンを使用して文字列を分割します:

on splitString(theString, theDelimiter) 
    -- save delimiters to restore old settings 
    set oldDelimiters to AppleScript's text item delimiters 
    -- set delimiters to delimiter to be used 
    set AppleScript's text item delimiters to theDelimiter 
    -- create the array 
    set theArray to every text item of theString 
    -- restore the old setting 
    set AppleScript's text item delimiters to oldDelimiters 
    -- return the result 
    return theArray 
end splitString 
今、それが動作するはずです、

-- This loops through menuItems, and we can use menuItem to access the current looped item 
-- We'll use this to reconstruct menuItems, but excluding what we need 

set newMenuItems to {} 
repeat with menuItem in menuItems 
    -- Get words from randomItem 
    -- For example, if randomItem is "Lamb Chops", it will return {"Lamb","Chops"} 
    set menuItemWords to splitString(randomItem, " ") 

    -- This is the conditional that will determine to include menuItem or not 
    -- Feel free to change this to get the outcome you want 
    if menuItemWords does not contain menuItem as string and menuItem does not contain menuItemWords then 

     -- And this conditional to not re-add the randomItem 
     -- However, it does it to one word items in the previous if statement 
     -- Note: Without using "as string" to menuItem it doesn't work correctly, not sure why   
     if menuItem as string is not equal to randomItem then 
      set the end of newMenuItems to menuItem 
     end 

    end 

end repeat 
-- Now we've finished creating a new menu with the changes we want, set it to the main one 
set menuItems to newMenuItems 

そして:

、今、私たちは簡単に分割でき、文字列、とメニューの復興を継続することができます。

あなたはまた、にしたいかもしれませんが、あなたが文字列に配列を連結するために、このサブルーチンを使用することができ、その場合には、簡単に結果を確認:あなたはにメニューを設定する前に

on arrayToString(theArray, theSeperator) 
    set arrayString to "" 
    repeat with theItem in theArray 
     if arrayString is equal to "" then 
      set arrayString to theItem 
     else 
      set arrayString to arrayString & theSeperator & theItem 
     end if 
    end repeat 
end arrayToString 

だけでは結果を表示します新しいもの。

........ 

display alert "I've picked " & randomItem & " for you." message "Old menu: 
" & arrayToString(menuItems, ", ") & " 
New menu: 
" & arrayToString(newMenuItems, ", ") 

-- Now we've finished creating a new menu with the changes we want and displayed the results, set it to the main one 
set menuItems to newMenuItems 


こともでき、メニュー項目をループメニューアイテムが空の場合、このように、繰り返しを終了するには、最初のmenuItem、チェックを指定した後 repeat 7 timesのコードをワープで7回:

on splitString(theString, theDelimiter) 
    ... 
end splitString 

-- The initial menuItems we start off with 
set menuItems to {"Lamb Chops", "Lamb", "Chops", "Spaghetti Bolognese", "Spaghetti", "Bolognese"} 

-- Repeat statement to loop 7 times 
repeat 7 times 
    -- If menuItems is empty, exit the repeat 
    if count of menuItems is 0 then 
     exit repeat 
    end if 

    set randomItemNum to random number from 1 to count of menuItems 
    set randomItem to item randomItemNum of menuItems as string 

    set newMenuItems to {} 
    repeat with menuItem in menuItems 
     ... 
    end repeat 
    -- Now we've finished creating a new menu with the changes we want, set it to the main one 
    set menuItems to newMenuItems 

-- This is the end of loop for 7 times 
end repeat 


これが役立つことを願っています。
他に助けが必要な場合、またはエラーがある場合は、下記のようにコメントしてみてください。
編集:ここ
は、それが7回を繰り返し、完了したときにユーザーが選択した料理のリストを示し、あなたが必要なものを行う必要があり、完全なコードです:

on splitString(theString, theDelimiter) 
    -- save delimiters to restore old settings 
    set oldDelimiters to AppleScript's text item delimiters 
    -- set delimiters to delimiter to be used 
    set AppleScript's text item delimiters to theDelimiter 
    -- create the array 
    set theArray to every text item of theString 
    -- restore the old setting 
    set AppleScript's text item delimiters to oldDelimiters 
    -- return the result 
    return theArray 
end splitString 

on arrayToString(theArray, theSeperator) 
    set arrayString to "" 
    repeat with theItem in theArray 
     if arrayString is equal to "" then 
      set arrayString to theItem 
     else 
      set arrayString to arrayString & theSeperator & theItem 
     end if 
    end repeat 
end arrayToString 

-- The menu after you finish looping to show the user 
set yourMenu to {} 

-- The initial menuItems we start off with 
set menuItems to {"Lamb Chops", "Lamb", "Chops", "Spaghetti Bolognese", "Spaghetti", "Bolognese"} 

-- Repeat statement to loop 7 times 
repeat 7 times 
    -- If menuItems is empty, exit the repeat 
    if (count of menuItems) is 0 then 
     exit repeat 
    end if 

    set randomItemNum to random number from 1 to count of menuItems 
    set randomItem to item randomItemNum of menuItems as string 

    -- This loops through menuItems, and we can use menuItem to access the current looped item 
    -- We'll use this to reconstruct menuItems, but excluding what we need 
    set newMenuItems to {} 
    repeat with menuItem in menuItems 
     -- Get words from randomItem 
     -- For example, if randomItem is "Lamb Chops", it will return {"Lamb","Chops"} 
     set menuItemWords to splitString(randomItem, " ") 

     -- This is the conditional that will determine to include menuItem or not 
     -- Feel free to change this to get the outcome you want 
     if menuItemWords does not contain menuItem as string and menuItem does not contain menuItemWords then 

      -- And this conditional to not re-add the randomItem 
      -- However, it does it to one word items in the previous if statement 
      -- Note: Without using "as string" to menuItem it doesn't work correctly, not sure why   
      if menuItem as string is not equal to randomItem then 
       set the end of newMenuItems to menuItem 
      end if 

     end if 

    end repeat 

    -- Now we've finished creating a new menu with the changes we want, set it to the main one 
    set menuItems to newMenuItems 
    set the end of yourMenu to randomItem 

    -- This is the end of loop for 7 times 
end repeat 

set itemSelected to (choose from list yourMenu with prompt "Here's your menu.") 
display alert "You selected " & itemSelected & "." 
+0

を示し@ TheTobemonster MenuItems2は何も設定していないので、空の配列です。'' randomItem2'を 'MenuItems2のitemItemNum2'を' 'string''として設定します' set randomItem2を{[nothing]}の項目[string]として設定するようにしています ' – Kamdroid

+0

これを読んでいて、 Applescriptの私の知識。無関係な7種類の料理を選択してダイアログボックスに表示したかったのです。私が理解したところで、私は次のスクリプトをまとめました:https://www.dropbox.com/s/upvdzfi6a6kv6od/Menu%20Picker%20V2.scpt?dl=0。しかし、私はいつも同じエラー "{}の項目1を型文字列にできません"何が間違っているのか、どうやって修正できるのか教えてくれることを願っていましたか?申し訳ありませんが、私はここで馬鹿に見えますが、私のAppleScriptに関する知識はそれほど大きくありません。 -Thanks –

+0

これはまだ動作していないようですか?すべての行の 'set randomItem2をMenuItems2のitemItemNum2を' string'に 'set randomItem2をitemItemのitemItemNum2を文字列として'に変更しました。これは約30秒間実行された後、エラーなしでスクリプトエディタがクラッシュします。もう一度、本当に申し訳ありませんが、私はここで愚かに聞こえる。 –

関連する問題