我制作了一个脚本,通过从列表中随机挑选7次菜肴来创建食物菜单。但是,我想这样做,以便它不会选择任何类似的菜肴。例如,如果' Lamb Chops'被选为dish1
,然后dish2
将是来自同一列表的随机项,其中包含所有选项,包括' lamb'和'排骨'除去。然后,如果dish2
是' Spaghetti Bolognese',那么dish3
将从同一列表中选择,不包括' lamb'' chops', ' spaghetti',' bolognese'等等。有人可以帮忙吗?
-Thanks。
答案 0 :(得分:2)
我对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次,如果menuItems为空则检查退出重复,如下所示:
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
<小时/> 我希望这会有所帮助 如果您需要任何其他帮助,或者有错误,请在下面评论,我会尽力而为。 修改的:
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 & "."