Compare certain file types and copy the newest files over

hello folks,

I’m new to the AppleScript world and would appreciate some help with my task.

I want to compare only certain file types between a source and target folder and copy the files that are not in the target folder. For example, the folder contains .gif, .jpg, and .png files and other folders but only care about .png and .jpg.

So far, this is what I’ve pieced together with the help of google and this site. It’s currently not working and also doesn’t account for specific file types. I get the following after it tries to process the .DS_Store file that’s in the source folder:

error “The operation can’t be completed.” number -8082


tell application "Finder"
	set sfolder to "MAC:Users:Me:Documents:Source Folder:"
	delay 0.5
	set tfolder to "MediaCard:Target Folder:"
	set f1Files to every file of folder sfolder
	set f2Files to every file of folder tfolder
	set f2Names to name of every file of folder sfolder
	
	repeat with k from 1 to count f1Files
		if name of item k of f1Files is not in f2Files then
			duplicate item k of f1Files to tfolder
		end if
	end repeat
end tell

Thanks in advance! :slight_smile:

Model: MBP
AppleScript: 2.5
Browser: Chrome
Operating System: Mac OS X (10.8)

Hi,

three issues:
¢ you are comparing a file name with a file object reference
¢ the file names are retrieved from the source folder instead of the target folder (therefore I prefer more meaningful variable names)
¢ the files are duplicated to a literal string instead of a folder reference

try this


property fileTypes : {"png", "jpg", "jpeg"}

set sourceFolderPath to ((path to documents folder as text) & "Source Folder:")
set targetFolderPath to "MediaCard:Target Folder:"

tell application "Finder"
	set sourceFiles to every file of folder sourceFolderPath whose name extension is in fileTypes
	set targetNames to name of every file of folder targetFolderPath
	
	repeat with k from 1 to count sourceFiles
		if name of item k of sourceFiles is not in targetNames then
			duplicate item k of sourceFiles to folder targetFolderPath
		end if
	end repeat
end tell

Thanks so much for the help, StefanK.

One last piece I need help with, I need this script to only run if a certain volume is mounted, otherwise do nothing. How can this be accomplished?

Thanks again!

to run a script automatically after a volume is mounted you need something like this

But if you want just to abort the script in case the volume is not mounted,
add this line after the property line

if "MediaCard" is not in (list disks) then return