【问题标题】:Copying files in Finder with AppleScript使用 AppleScript 在 Finder 中复制文件
【发布时间】:2015-03-01 15:27:33
【问题描述】:

这是我第一次尝试 AppleScripting。

我有 3 个包含图像文件的文件夹。 测试文件夹 1 有 77 个大型主文件。 测试文件夹 2 在名为 ABC 的子文件夹中有 4 个较小的文件,与测试文件夹 1 中的文件同名。 测试文件夹 3 包含一个名为 ABC 的空子文件夹。 我想要一个脚本来检查测试文件夹 2 中的文件名并将等效文件名从测试文件夹 1 复制到测试文件夹 3 子文件夹 ABC

这是我目前所拥有的:

tell application "Finder"
    set the_files to (files of folder "Macintosh HD:Users:Ronnie:Pictures:Test Folder 1:")
    set the_file_names to (files of folder "Macintosh HD:Users:Ronnie:Pictures:Test Folder 2:ABC:")
    set target_folder to ("Macintosh HD:Users:Ronnie:Pictures:Test Folder 3:ABC:")
    if document files of the_file_names is equal to document files of the_files then duplicate the_files to target_folder
end tell

目前它将复制测试文件夹 1 中的所有文件,因此“If”脚本不起作用。

有人可以帮忙吗?

罗尼

【问题讨论】:

  • 我想我知道你在做什么,我已经制定了一个答案,但我需要知道一件事:在测试测试文件夹 1 中的文件匹配测试文件夹 2 中的文件时,只有在所有文件名完全匹配的情况下才会进行复制,或者您只是想复制匹配的文件?
  • 换句话说,一个文件夹中的文件可能在另一个文件夹中不存在。如果发现这样的不匹配,按照我现在的方式,这样的文件会被忽略,但会复制其他匹配的文件。
  • 感谢您的回复。只有匹配的文件需要被复制。希望你能帮忙。我正在自学,并且在这方面遇到了困难,我希望在您的帮助下学习。

标签: applescript


【解决方案1】:

这是一种方法。

--this is my preference. I prefer to work with strings then coerce them to aliases
--"aliases" in the old AppleScript/Mac API sense, meaning special file/folder path data type
-- (this is what is returned when you use the choose file or choose folder commands, e.g.)
--  aliases can be coerced to and from strings
-- not to be confused with POSIX style paths which use "/" as separator, OR
-- Finder item objects, which are specific to the Finder ('file x of folder y of startup disk z' style)

set tf1 to "Macintosh HD:Users:Ronnie:Pictures:Test Folder 1:"
set tf2 to "Macintosh HD:Users:Ronnie:Pictures:Test Folder 2:ABC:"
set target_folder to "Macintosh HD:Users:Ronnie:Pictures:Test Folder 3:ABC:"

--above not needed in Finder tell block

tell application "Finder"
    set fileNames to (name of files of alias tf1) --we get the names of the files here; "name" filters, so does "files"
    set matchNames to (name of files of alias tf2) --and the names of the files we want to match in testing
    repeat with thisName in fileNames --repeat loop is necessary for this
        if thisName is in matchNames then
            --notice the concatenating of folder and name to make new alias that gets copied
            duplicate alias (tf1 & thisName) to alias target_folder
        end if
    end repeat
end tell

【讨论】:

  • 谢谢!这似乎按应有的方式工作!曾经那么厉害。我有很多要学习的。我需要花点时间看看它到底是如何工作的。但再次感谢您愿意和称职的工作。我在 TF2 中仅选择了 4 个文件进行了尝试,它找到了 4 个文件中的 3 个。搜索 TF1 表明该文件不存在,因此程序似乎工作正常。我会在进行过程中反馈我的经验。
猜你喜欢
  • 1970-01-01
  • 2012-12-13
  • 1970-01-01
  • 2012-06-30
  • 1970-01-01
  • 2014-07-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多