使用 AppleScript 在 Finder 中复制文件
Copying files in Finder with AppleScript
这是我第一次尝试 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" 脚本无法正常工作。
有人可以帮忙吗?
罗尼
这是一种方法。
--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
这是我第一次尝试 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" 脚本无法正常工作。
有人可以帮忙吗?
罗尼
这是一种方法。
--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