如何用applescript打开多个随机文件?

时间:2012-09-11 21:06:50

标签: macos applescript

我正在成功打开1个随机文件,如下所示:

tell application "Finder"
   open some file of folder "HD:random"
end tell

我想在文件夹中打开3个随机文件。 3个不同的。如果我运行AppleScript 3次,我会得到相同的那些。

谢谢!

1 个答案:

答案 0 :(得分:1)

这是使用“某些文件”的一种方式......

set theNumber to 3
set theFolder to choose folder

set theFiles to {}
repeat
    tell application "Finder" to set aFile to (some file of theFolder) as text
    if aFile is not in theFiles then
        set end of theFiles to aFile
        tell application "Finder" to open file aFile
    end if
    if (count of theFiles) is equal to theNumber then exit repeat
end repeat

这是使用随机数的另一种方式......

set theNumber to 3
set theFolder to choose folder

tell application "Finder" to set theFiles to files of theFolder
set fileCount to count of theFiles
if fileCount is less than theNumber then error "I couldn't find " & (theNumber as text) & " files in the chosen folder."

set randomNumbers to {}
repeat
    set aNum to random number from 1 to fileCount
    if aNum is not in randomNumbers then
        set end of randomNumbers to aNum
        tell application "Finder" to open (item aNum of theFiles)
    end if
    if (count of randomNumbers) is equal to theNumber then exit repeat
end repeat