Applescript / Finder按文件名或文件路径列表选择文件夹中的特定项目,包括公开的子文件夹中的项目

时间:2017-11-10 14:34:00

标签: applescript finder

以下是我们的情况:

我们有一个文件名和/或完整文件路径列表(我们可以生成)

我们列表中的文件都包含在一个文件夹下,但分散在多个子文件夹中。 (我们的选择列表中有数百个项目来自数千个可能的文件。手动选择不是一个选项)

我们已经在列表视图中打开了根文件夹,并打开了所有子文件夹,子子等公开(顺便说一句,感谢http://hints.macworld.com/article.php?story=20030218164922494的快捷方式“命令,选项,控制和转移当按下右箭头“)

在一个窗口打开后可以看到所有文件,我们希望自动选择文件列表中的所有项目,以便可以立即将它们拖到应用程序中。

2 个答案:

答案 0 :(得分:0)

这可以使用最新版本的Sierra。

我无法找到一种方法来选择性地选择带有子文件夹等子文件夹的文件夹中的文件。我能想到的唯一解决方案是创建名为“Aliases”的文件夹并让AppleScript为所有人创建别名文件“选定文件”并将所有别名存储在别名文件夹中。从那里你可以拖动所有文件并根据需要将它们放入你的应用程序

如果你有一个包含POSIX路径文件名的纯文本文件,每个文件都在一个单独的行上,就像下一张图片中的例子一样,这个版本会将文本文件中的路径名直接加载到脚本中。只需将此脚本另存为应用程序即可。您可以将文本文件直接拖到应用程序的图标上,因为代码设置为Droplet

enter image description here

global theFile
property theInfo : missing value
property theName : missing value
property theList : {}
property theList2 : {}
property aliasFolder : (path to desktop as text) & "Aliases"

on open theFiles
    set theInfo to info for theFiles
    set theName to POSIX path of theFiles

    getLinesofFileAsList(theName)

    tell application "Finder"
        if not (exists of alias aliasFolder) then
            make new folder at (path to desktop as text) with properties {name:"Aliases"}
        end if
        delete every item of alias aliasFolder
    end tell

    repeat with i from 1 to count of theList
        try
            set theResult to POSIX file (item i of theList) as text
            set end of theList2 to theResult
            tell application "Finder"
                set theAliases to make new alias file at aliasFolder to theResult
            end tell
        end try
    end repeat

    delay 0.5

    tell application "Finder"
        activate
        delay 0.5
        set hmmm to reveal aliasFolder
        delay 0.5
        set hmmm to select every item of alias aliasFolder
        activate hmmm
    end tell
end open

on getLinesofFileAsList(theName)
    set theFile to theName
    set theFile to POSIX path of theName
    set theList to read POSIX file theFile as text
    set saveTID to AppleScript's text item delimiters
    set AppleScript's text item delimiters to linefeed
    set theList to paragraphs of theList
    if last item of theList is "" then
        set theList to reverse of rest of reverse of theList
    end if
    set AppleScript's text item delimiters to saveTID
end getLinesofFileAsList


--on run
--  --  Handle the case where the script is launched without any dropped files
--end run

enter image description here

答案 1 :(得分:0)

我不相信使用基本 AppleScript ,可以通过编程方式选择 Finder 中跨多个文件夹的多个项目窗口中给定文件夹中的em>和子文件夹。也许使用 Cocoa-AppleScript ,但不知道,如果 Avid 可以从文件别名打开文件 ,那么以下示例 AppleScript 代码是一个可行的选择。

示例 AppleScript 代码做出以下假设:

  • 纯文本文件,其中包含目标文件完全限定POSIX路径名,需要使用 Avid进行处理和文件名称是:使用Avid.txt处理的文件列表
  • 包含别名文件夹名称是:要使用Avid处理的文件的别名
  • 桌面用作上述文件文件夹中文件系统中的位置 存在,由 Avid 处理。

显然,这些设置可以根据需要/需要进行更改,请参阅代码中的注释

示例 AppleScript 代码

--  # Set the value of the following three property variables:
--  #
--  # The value of 'thisLocation' is an colon-delimited path string, e.g. 'path to desktop as string' returns: "Macintosh HD:Users:me:Desktop:"
--  # NOTE: When not using 'path to (folder)' where 'folder' is a 'folder constant' , the special folder for which to return the path, the value should be in the form of an colon-delimited path string.
--  # See: https://developer.apple.com/library/content/documentation/AppleScript/Conceptual/AppleScriptLangGuide/reference/ASLR_cmds.html#//apple_ref/doc/uid/TP40000983-CH216-SW19
--  #
--  # The value of 'theListFilename' is the name of the plain text file containing the fully quilified pathnames of the target files to be opened in Avid.
--  # The value of 'theFolderName' is the name of the temporary folder the temporary aliases will be created in. This folder gets created new each run with new aliases.
--  #
--  # NOTE: For ease of use, as each run is presumed to be temporary to get that job run done, the location of the 'theListFilename' and 'theFolderName' are both in 'thisLocation'.

property thisLocation : (path to desktop as string)
property theListFilename : "List of Files to Process with Avid.txt"
property theFolderName : "Aliases of Files to Process with Avid"

--  # The remaining code is tokenized and should not need to be modified.

tell application "Finder"
    if (exists thisLocation & theListFilename) then
        tell current application to set theList to read alias (thisLocation & theListFilename)
    else
        display dialog "The file, \"" & theListFilename & "\", was not found at the expected location." buttons {"OK"} ¬
            default button 1 with title "Missing File" with icon 0
        return
    end if
    set theFolderPathname to thisLocation & theFolderName
    if not (exists theFolderPathname) then
        make new folder at thisLocation with properties {name:theFolderName}
    else
        move theFolderPathname to trash
        make new folder at thisLocation with properties {name:theFolderName}
    end if
    repeat with i from 1 to length of theList
        try
            make new alias file at theFolderPathname to POSIX file (paragraph i of theList)
        end try
    end repeat
    reveal theFolderPathname
    activate
    -- delay 1 --   # In necessary, uncomment and adjust value as appropriate.
    select every item of alias theFolderPathname
end tell

脚本编辑器中,保存此脚本应用程序,例如选择要使用Avid处理的文件,然后在替换例如使用Avid.txt处理的文件列表,并使用 Avid 处理当前的目标文件

脚本执行以下操作:

  • 检查以查看文件,例如使用Avid.txt处理的文件列表存在,如果没有则显示错误消息并退出。
  • 检查是否文件夹,例如要使用Avid处理的文件的别名存在,如果没有创建,如果存在,则将其移至已删除邮件并重新创建的新运行>目标文件进行处理。
  • 文件中创建列出的每个文件别名,作为完全限定的POSIX路径名,例如:
    使用Avid.txt处理的文件列表
  • 打开文件夹,例如选择要使用Avid处理的文件,在 Finder 中选择别名

您现在可以将选定的别名拖放到 Avid

注意:此脚本假定要使用Avid处理的目标文件的完全限定POSIX路径名在其路径名中不包含换行符,回车符和/或空字符。