Applescript选择其名称包含值列表或文本列表的任何部分的文件

时间:2013-06-11 13:59:08

标签: file select applescript

我需要从文件夹中选择一组具有不同名称的文件。这是我到目前为止小规模工作的剧本。

tell application "Finder" to select (every file of target of front window whose 
name contains "3123" or name contains "3125" or name contains 3166)

但是我需要一个可以大规模运行的脚本,我可以输入类似于

的myValues
set myValues to {3101, 3102, 3103, 3456, 6789, etc. etc.}
tell application "Finder" to select (every file of target of front window whose 
name contains myValues)

但是当我使用该脚本时,如果我将多个数字设置为myValues,则不会选择任何文件。此外,如果您可以告诉我如何更改它以使用文本,这将是非常棒的。

感谢您的帮助!

1 个答案:

答案 0 :(得分:2)

尝试:

set myValues to {"3101", "3102", "3103", "3456", "6789"}

tell application "Finder" to set fileList to files of target of front window

set matchedFiles to {}
repeat with aFile in my fileList
    repeat with aValue in myValues
        tell application "System Events" to if aFile's name contains (contents of aValue) then set end of matchedFiles to (aFile as text)
    end repeat
end repeat

tell application "Finder" to select matchedFiles

编辑 - 添加了Finder窗口和regulus6633的建议:

set myValues to {"3101", "3102", "3103", "3456", "6789"}

tell application "Finder" to set fileList to files of target of front Finder window as alias list

set matchedFiles to {}
repeat with aFile in my fileList
    repeat with aValue in myValues
        tell application "System Events" to if aFile's name contains (contents of aValue) then set end of matchedFiles to (aFile as text)
    end repeat
end repeat

tell application "Finder" to select matchedFiles

编辑2

set myValues to {"3125", "3123"}

tell application "Finder" to set fileList to files of target of front Finder window as alias list
set matchedFiles to {}
repeat with aFile in my fileList
    repeat with aValue in myValues
        tell application "System Events" to if aFile's name contains (contents of aValue) then set end of matchedFiles to (aFile as text)
    end repeat
end repeat

if matchedFiles ≠ {} then
    tell application "Finder"
        select matchedFiles -- you don't need to select files to duplicate them
        duplicate matchedFiles to (choose folder)
    end tell
end if