0
votes

This is my first attempt at AppleScripting.

I have 3 folders that contain image files. Test Folder 1 has 77 large, master files. Test Folder 2 has 4 smaller files in a subfolder called ABC with the same name as files in Test Folder 1. Test Folder 3 Contains an empty sub folder called ABC. I want a script to check the file names in Test Folder 2 and copy the equivalent file names from Test Folder 1 to Test Folder 3 subfolder ABC

Here is what I have so far:

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

Currently it will copy all the files from Test Folder 1 so its seams that the "If" script does not work.

Can someone help?

Ronnie

1
I think I know what you're doing, and I've formulated an answer, but I need to know 1 thing: when testing for the files in test folder 1 matching files in test folder 2, would a duplication only be done if all the file names exactly matched, or are you simply wanting to copy files that match?CRGreen
In other words, there may be files in one folder that don't exist in the other. if such a mismatch is found, the way I have it now, such a file is ignored, but other files with a match are copied.CRGreen
Thank you for responding. Only files that match need to be copied. Hope you can help. I am teaching myself and have hit the wall on this one and I am hoping to learn with your help.Ronnie

1 Answers

0
votes

This is one way to do it.

--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