1

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

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

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

谢谢!

4

1 回答 1

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
于 2012-09-11T22:15:06.373 回答