私は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 & "."
はあなたのアプローチ –