【问题标题】:AppleScript find folder with specific fileAppleScript 查找具有特定文件的文件夹
【发布时间】:2017-11-09 10:01:13
【问题描述】:

我有一个包含文件/resources/video.mov 的文件夹。这个包含视频的文件夹可以位于任何地方,例如/Users/UserName/Desktop/resources/video.mov/Applications/resources/video.mov 等。我需要找到这个包含视频的文件夹并复制到特定的地方。我找不到要复制视频的文件夹路径。

tell application "Finder"
    duplicate items in folder "_locatedPath_" to folder "_destiontionPath_" with replacing
end tell

【问题讨论】:

  • 如果您不知道位置,shell 命令findmdfind 是您的朋友。 Vanilla AppleScript 太慢了。

标签: macos applescript


【解决方案1】:

这适用于我使用最新版本的 Sierra

将变量destinationFolder 的值更改为您选择的输出文件夹

property theContainer : missing value
property containingFolder : "resources" -- change if needed
property theSearch : "video.mov" -- change if needed
property destinationFolder : (path to desktop as text) -- change if needed

set findFile to do shell script "mdfind -name " & theSearch 

repeat with i from 1 to number of paragraphs in findFile
    set this_item to POSIX file (paragraph i of findFile) as alias
    tell application "Finder"
        if name of this_item is theSearch then
            set theContainer to the container of this_item as text
            if theContainer contains containingFolder then
                set resultObject to duplicate this_item ¬
                    to destinationFolder ¬
                    replacing true ¬
                    routing suppressed true ¬
                    with exact copy
            end if
        end if
    end tell
end repeat

【讨论】:

  • 我不清楚 OP 是否只想将“video.mov”或“video.mov”包含文件夹的所有内容(即“resources”)复制到目标文件夹。我假设 OP 只是想复制位于“resources”文件夹中的“video.mov”。
  • 需要考虑的事项:在脚本编辑器中打开 Finder 的字典并搜索 POSIX,你不会找到它。 set this_item to POSIX file this_item as alias 不应该在 tell application "Finder" block 中,因为 Finder 实际上不能 set this_item to POSIX file this_item as alias 并且它实际上会在 --> error number -1728 from POSIX file ... 中默默地出错。它应该在该块之外。但是,您也不需要property fileList : {}copy findFile to fileList。看看这个reworked example of your code
  • do shell script "mdfind -name " & theSearch 返回的fileList 已经是POSIX 路径名的段落,所以去掉set this_item to POSIX file this_item as alias 并将set this_item to paragraph i of fileList 更改为set this_item to POSIX file (paragraph i of fileList) as alias。此外,set fileList to do shell script "mdfind -name " & theSearch 处理带或不带空格的文件名,因为您将它们别名回 HFS+ 路径以供 Finder 使用。
  • 不知道为什么你会得到这个结果,但是当我重新编码时,你的代码的重新设计示例在我的系统上运行没有问题。 Here's the output run with and without as space in the name. BTW log theContainer 应该在我发布代码之前被删除,所以显然不需要它,我只是在验证一些东西。 :)
  • BTW 很好地修改了您删除的原始答案。 +1
猜你喜欢
  • 2014-11-12
  • 2018-04-29
  • 2011-03-10
  • 1970-01-01
  • 2018-05-17
  • 1970-01-01
  • 1970-01-01
  • 2014-07-25
  • 1970-01-01
相关资源
最近更新 更多