Applescript,我试图让一个列表出现在一个对话框中,我只有3个总量

时间:2015-12-22 04:43:44

标签: macos list applescript

Applescript,我试图在一个对话框中显示一个列表,其中只有3个项目。例如,我有一个包含5个可能项目的列表,我希望对话框显示3个随机项目,但没有重复。我不知道我是否在正确的轨道上,但我卡住了。

set myList to {"Apples", "Bananas", "Oranges", "Grapes", "Turkey"}

set r to some item of myList

set newList to (myList without r)

set t to some item of newList

set newerList to (newList without t)

newList

3 个答案:

答案 0 :(得分:1)

与vadian不同的方法。 vadian脚本的问题在于理论上脚本可以永远运行,如果脚本不断选择它之前采用的项目。因此,最好有一个没有碰撞的解决方案。这需要更多的努力,因为每次选择一个项目后,必须从列表中删除该值。由于在AppleScript中没有简单的命令来执行此操作,因此脚本必须“手动”执行此操作。

最简单的方法是创建一个包含输入列表索引的并行列表,在每次迭代中选择一个随机索引,并将其设置为非整数值。这样我们就可以确保只挑选一次该项目。

set myList to {"Apples", "Bananas", "Oranges", "Grapes", "Turkey"}
set idxList to {}
-- first create a list with indexes
repeat with x from 1 to count myList
    set end of idxList to x
end repeat

set newList to {}
repeat 3 times
    -- pick a random index
    set theIndex to some integer of idxList
    -- add item to newlist based on picked index
    set end of newList to item theIndex of myList
    -- set the picked index to missing value so it will not be picked again
    set item theIndex of idxList to missing value
end repeat
newList

答案 1 :(得分:0)

此解决方案使用重复循环执行,直到达到最大项目数。

property maxItemsInNewList : 3

set newList to {}
set myList to {"Apples", "Bananas", "Oranges", "Grapes", "Turkey"}
repeat while (count newList) < maxItemsInNewList
    set r to some item of myList
    if newList does not contain r then set end of newList to r
end repeat
newList

答案 2 :(得分:0)

使用perl来调用# Input list. set myList to {"Apples", "Bananas", "Oranges", "Grapes", "Turkey"} # Convert the list to a string with each item on its own line. set AppleScript's text item delimiters to "\n" # ' perhaps restore original val. later set myTextList to myList as text # Set the number of random items to extract, without replacement. set sampleSize to 3 # Call the shell to let Perl shuffle the lines, then # convert the lines back into a list and extract the first 3 items. set randomSample to paragraphs 1 thru sampleSize of ¬ (do shell script "perl -MList::Util=shuffle -e 'print shuffle <>' <<<" & ¬ quoted form of myTextList) 的解决方案来补充dj bazzie wazzie's helpful pure AppleScript answer,这样可以非常轻松地改变列表:

RewriteRule ^partners/(.*)$ partners?id=$1 [L]