How to open multiple random files with applescript?

别等时光非礼了梦想. 提交于 2019-12-11 10:28:14

问题


I am successfully opening 1 random file, like this:

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

I would like to open 3 random files in the folder. 3 different ones. If I run the applescript 3 times I will get the same ones sometimes.

Thanks!


回答1:


Here's 1 way using "some file"...

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

Here's another way using random numbers...

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


来源:https://stackoverflow.com/questions/12377927/how-to-open-multiple-random-files-with-applescript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!