2017-07-04 129 views
0

我正在製作一個腳本,通過從列表中隨機挑選菜餚7次來創建食物菜單。不過,我想這樣做,它不會選擇任何類似的菜餚。例如,如果選擇「羊排」作爲dish1,那麼dish2將是來自同一列表的隨機項目,所有選項都被刪除,包括「羊肉」和「排骨」。然後,如果dish2是「肉醬意大利麪條」,那麼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 


您還可以通過菜單項循環7次被扭曲的 repeat 7 times代碼指定初始菜單項,並檢查後退出重複,如果的菜單項是空的,就像這樣:

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的項目randomItemNum2作爲字符串,您要求設置randomItem2設置爲{[nothing]}的項目[random number]作爲字符串' – Kamdroid

+0

我讀到了這個,很多內容超出了我的Applescript知識。我希望它選擇7種不相關的不相關的菜餚並在對話框中顯示它們。根據我的理解,我將以下腳本拼湊在一起:https://www.dropbox.com/s/upvdzfi6a6kv6od/Menu%20Picker%20V2.scpt?dl=0。但是,我總是得到相同的錯誤「無法將{}的項目1變爲類型字符串。」我希望你能告訴我什麼是錯的,我該如何解決它?對不起,如果我在這裏看起來很愚蠢,我對AppleScript的知識不是太好。 - 謝謝 –

+0

這仍然不起作用?我將所有的'set randomItem2'改爲'MenuItems2的項目randomItemNum2作爲字符串'在每行中將'randomItem2'設置爲MenuItems的項目randomItemNum2作爲字符串'。這隻會運行約30秒,然後崩潰腳本編輯器沒有任何錯誤。再次,真的很抱歉,如果我聽起來很愚蠢。 –

相關問題