如何用applescript打开多个随机文件? [英] How to open multiple random files with applescript?

查看:27
本文介绍了如何用applescript打开多个随机文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am successfully opening 1 random file, like this:

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

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

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.

谢谢!

推荐答案

这是使用某个文件"的 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

这篇关于如何用applescript打开多个随机文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆