Duplicate file into a new folder automatically..

Here is the completed script.

on adding folder items to thisFolder after receiving theseItems
	
	set targetName to "photos final"
	# The backup folder is supposed to be on the desktop
	set p2d to path to desktop as text
	# Define the path to the backup folder
	set backupFolder to (p2d & targetName)
	# Quoted it for the shell script instruction
	set qPbackupFolder to quoted form of POSIX path of backupFolder
	
	tell application "System Events"
		set folderName to name of thisFolder
		set folderContainer to path of (container of thisFolder)
		set targetFolder to folderContainer & targetName & ":"
		# If needed, create the final folder
		if not (exists folder targetFolder) then
			make new folder at end of folder folderContainer with properties {name:targetName}
		end if
		# If needed, create the backup folder
		if not (exists folder backupFolder) then
			make new folder at end of folder p2d with properties {name:targetName}
		end if
		repeat with aFile in theseItems
			log aFile
			if name of aFile contains "-final." then
				# Copy it in the backup folder
				my copyThat(aFile, qPbackupFolder)
				# Move it to the final folder
				move aFile to folder targetFolder
			end if
		end repeat
	end tell
end adding folder items to

on copyThat(aFile, qPdestFolder)
	set qPoriginal to quoted form of (POSIX path of (aFile as text))
	do shell script "cp " & qPoriginal & space & qPdestFolder
end copyThat

Yvan KOENIG running Sierra 10.12.1 in French (VALLAURIS, France) jeudi 3 novembre 2016 19:41:16

That works perfectly thanks again.

Which parts of the script would I need to change to change the destination? So currently the backup appears on the desktop as photos final but I’d like to have the option to change the destination with just typing a different path, is this possible?

I was thinking that ‘set backupFolder to (p2d & targetName)’ could be changed to ‘set backup folder to (documents/backup/folder)’ as long as that folder existed of course.

Am I completely misunderstanding it? I also read that an alias might be necessary but i haven’t seen that used in your script so maybe i am getting it wrong.

Edit: After a bit more playing about I have found the path I need is /Volumes/Media/Backup/ but I need the ability to change that easily within the script.

Thank you so very much for your time and efforts.

I carefully commented the instructions to allow you to edit the script.

At the beginning of the script everybody may read :

on adding folder items to thisFolder after receiving theseItems

set targetName to “photos final”
# The backup folder is supposed to be on the desktop
set p2d to path to desktop as text
# Define the path to the backup folder
set backupFolder to (p2d & targetName)

Quoted it for the shell script instruction

set qPbackupFolder to quoted form of POSIX path of backupFolder

If you want to put the backup folder in a folder named “my backups” in the Documents folder you just have to edit the instruction :
set p2d to path to desktop as text
as :
set p2d to (path to documents folder as text)&“my backups:”

Yvan KOENIG running Sierra 10.12.1 in French (VALLAURIS, France) vendredi 4 novembre 2016 10:55:36

Thanks, yeah I noticed you’d noted it all but i’m sorry but I just didn’t understand how to adjust what went on those lines although I had realised what you’d done. I read through your reply and give it another go. Scripting and things that you may find easy I just don’t understand. :frowning:

set targetName to “photos final”

The backup folder is supposed to be on the desktop

set p2d to path to desktop as text # ask the system to put in the variable p2d the path to the desktop folder

it will resemble to : Macintosh HD:Users:machinchose:Desktop:

Define the path to the backup folder

set backupFolder to (p2d & targetName) # As targetName is defined with the value “photos final”, backupFolder will be : Macintosh HD:Users:machinchose:Desktop:photos final"

Quoted it for the shell script instruction

set qPbackupFolder to quoted form of POSIX path of backupFolder # converts the pathname in a format required by the do shell script command. With the values defined above it would be “‘/Users/machinchose/Desktop/photos final’”

Don’t forget that if I am reasonably fluent with AppleScript, it’s not the same for English which is not my own language. I just studied it at school 55 years ago.

Yvan KOENIG running Sierra 10.12.1 in French (VALLAURIS, France) vendredi 4 novembre 2016 20:29:45

I’ve been playing with this since I saw your last post. I’ve been trying really hard to make this work myself but I’m not able to make it work.


on adding folder items to thisFolder after receiving theseItems
	
	set targetName to "photos final"
	# Define the path to the backup folder
	set backupFolder to ("Volumes/Media/Backup")
	# Quoted it for the shell script instruction
	set qPbackupFolder to quoted form of POSIX path of backupFolder
	
	tell application "System Events"
		set folderName to name of thisFolder
		set folderContainer to path of (container of thisFolder)
		set targetFolder to folderContainer & targetName & ":"
		# If needed, create the final folder
		if not (exists folder targetFolder) then
			make new folder at end of folder folderContainer with properties {name:targetName}
		end if
		# If needed, create the backup folder
		if not (exists folder backupFolder) then
			make new folder at end of folder ("Volumes/Media/Backup/") with properties {name:targetName}
		end if
		repeat with aFile in theseItems
			log aFile
			if name of aFile contains "-final." then
				# Copy it in the backup folder
				my copyThat(aFile, qPbackupFolder)
				# Move it to the final folder
				move aFile to folder targetFolder
			end if
		end repeat
	end tell
end adding folder items to

on copyThat(aFile, qPdestFolder)
	set qPoriginal to quoted form of (POSIX path of (aFile as text))
	do shell script "cp " & qPoriginal & space & qPdestFolder
end copyThat

I have tried to get the original script changed so that it creates the folder ‘photos final’ in the ‘myphotos’ folder. this works. I then want anything named ‘-final’ to copy to that folder, that did work until I tried to change the backup folder name from desktop so i’ve obviously coded it poorly.
I then want the backup to be created in a folder called Volumes/Media/Backup/ which is the correct path to my network drive. This path works perfectly if I CD to it in terminal. I just don’t know and unfortunately can’t work out the correct syntax for placing it into your script instead of backing up to desktop.

If you wouldn’t mind, could you correct the script with what I’ve tried to change please?

I very much appreciate your time and I’m very sorry I’ve had to keep coming back to you to help me with it.

Thank you

Michael

You may try to use :


on adding folder items to thisFolder after receiving theseItems
	
	set targetName to "photos final"
	# Define the path to the backup folder
	set leve1Folder to "Volumes/Media/"
	-- set leve1Folder to "Volumes/Seagate 3To/" # CAUTION, I use the name of a bolume existing here.
	set folderName to "Backup"
	set level2Folder to leve1Folder & folderName & "/"
	set backupFolder to level2Folder & targetName
	# Quoted it for the shell script instruction
	set qPbackupFolder to quoted form of backupFolder # backupFolder is defined as a Posix path so you just have to quote it
	
	tell application "System Events"
		if not (exists folder level2Folder) then make new folder at end of folder leve1Folder with properties {name:folderName} # FAILS because the script is not allowed to create a folder at the root of a volume. I created the folder by hand
		# If needed, create the backup folder
		if not (exists folder backupFolder) then make new folder at end of folder level2Folder with properties {name:targetName}
		set folderName to name of thisFolder
		set folderContainer to path of (container of thisFolder)
		set targetFolder to folderContainer & targetName & ":"
		# If needed, create the final folder
		if not (exists folder targetFolder) then
			make new folder at end of folder folderContainer with properties {name:targetName}
		end if
		
		repeat with aFile in theseItems
			log aFile
			if name of aFile contains "-final." then
				# Copy it in the backup folder
				my copyThat(aFile, qPbackupFolder)
				# Move it to the final folder
				move aFile to folder targetFolder
			end if
		end repeat
	end tell
end adding folder items to

on copyThat(aFile, qPdestFolder)
	set qPoriginal to quoted form of (POSIX path of (aFile as text))
	do shell script "cp " & qPoriginal & space & qPdestFolder
end copyThat

Yvan KOENIG running Sierra 10.12.1 in French (VALLAURIS, France) mercredi 9 novembre 2016 22:56:00

It works perfectly. Thank you very much. :smiley: