Duplicate file into a new folder automatically..

You’ve taught me a great deal. Thank you very much. I apologise about my ignorance of how it all works. I thought it went into the proper folder automatically on running it. I appreciate your time and effort helping me with this.

I hope you have a good evening.

It would help to read the page :

https://developer.apple.com/library/prerelease/content/documentation/AppleScript/Conceptual/AppleScriptLangGuide/reference/ASLR_folder_actions.html

Yvan KOENIG running Sierra 10.12.0 in French (VALLAURIS, France) dimanche 23 octobre 2016 21:09:02

I have downloaded bridge plus and created a folder in the library folder called ‘Script Libraries’ Which I placed bridgeplus within.

I then created another folder called ‘Scripts’ and created a folder called ‘Folder Action Scripts’ within that and placed the compiled script into that.

I created a folder on my desktop called ‘photos’ and placed a couple of photos in there.

I right clicked on the folder called photos and selected the option folder action setup and selected the custom script you wrote and accepted that.

I changed one of the photos names to abc ‘123-final’ but nothing happened. I then moved a file called ‘123-final’ into the folder but again nothing happened.

I’m surprised I had to make the folders to place the scripts into as I expected them to be there already, have I put them in the right place?

Have I missed anything in my setup of the script?

Thanks again for your time in helping me with this.

Browser: Safari 537.36
Operating System: Mac OS X (10.10)

According to your very first message, the script is supposed to be attached to the folder “MyPhotos” containing the subfolder named “photos” and in which will be created the subfolder named “photos-final”.

Yvan KOENIG running Sierra 10.12.1 in French (VALLAURIS, France) jeudi 27 octobre 2016 22:03:08

I have adjusted my folder structure and it still isn’t working. I assume it works fine on your mac?

The file structure is myphotos with photos as another folder within it. I changed the name of one of the photos to abc-final and nothing happened, I placed a photo in the folder called abc-final and nothing happened. I tried this with the folder named myphotos as well as the folder named photos.

I seriously have no idea where I’m going wrong.

Oops, I’m an ass.

The script must be attached to the folder MyPhotos:photos:"
So the script will be triggered when you will add a “xxx-final.extension” file in the folder.

Will be back tomorrow.

Yvan KOENIG running Sierra 10.12.1 in French (VALLAURIS, France) jeudi 27 octobre 2016 23:31:18

I have used the shorter script and that works perfectly, it moves anything named -final to the newly created or existing folder photos final. It’s fantastic so thank you.

I can’t help thinking that the longer script may have issues as I am on 10.10 and not 10.12. Is this possible? What would the longer script do that the shorter one won’t? With my limited knowledge I get the feeling I will be better with a simpler script if I ever want to edit it for a different file etc.

Any further suggestions you may have I’d like to see, this process has made me learn so much and I am really impressed with your knowledge.

The longer script use ASObjC features available since 10.10.
It’s faster than the one using the old fashioned code.
In my real life, the handlers aren’t in the script, they are in a library file so that the script itself has the same length than the old fashioned one.

Yvan KOENIG running Sierra 10.12.1 in French (VALLAURIS, France) vendredi 28 octobre 2016 17:30:27

I’ve decided to use the shorter script as it appears easier to play with. How can I duplicate the new folder and move a duplicated file into there as a backup?

The path would be on a network drive /server/media/ on my Linux server

I apologizes.
You need help from an other helper.
I know absolutely nothing about networks which I never used.

Yvan KOENIG running Sierra 10.12.1 in French (VALLAURIS, France) dimanche 30 octobre 2016 22:18:30

It’s more the correct terms I need. What changes would I need to make to the script if I wanted to make the folder and duplicate the -final files onto the desktop as a backup to the ones made in the myphotos folder?

Thanks

I’ve been reading into this and not getting very far trying to work out how to change or add extra places for the selected files to go. Could you possibly supply me with where in the script I’d need to enter the path please?

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: