Duplicate file into a new folder automatically..

I apologizes but I’m not a sooth sayer.
Given that I can’t guess that you run a five years old system.

The given code requires 10.10 or higher.

Below is a version which doesn’t use new features so I hope that it will work under 10.7.5.
I can’t test it because I don’t have a disk with your old OS. In fact I have no machine able to run such pythecanthropus.

on adding folder items to thisFolder after receiving theseItems
	tell application "System Events"
		set folderName to name of thisFolder
		set folderContainer to path of (container of thisFolder)
		set targetName to "photos final"
		set targetFolder to folderContainer & targetName & ":"
		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
			if name of aFile contains "-final." then
				move aFile to folder targetFolder
			end if
		end repeat
	end tell
end adding folder items to

Yvan KOENIG running Sierra 10.12.0 in French (VALLAURIS, France) dimanche 23 octobre 2016 17:36:35

Don’t apologise. I am very grateful for your help. I have 2 machines, one is an old macbook which newer versions won’t work on and one is a newer machine on 10.11. So I can use both versions.

Is there anything I need to change in the script to make it personal to my folders or should it work as is?

Thank you very much for your fast reply.

I’m sorry for not understanding a lot of this. I’m a complete beginner with this kind of thing and am not sure when the info in the script is there because i need to change it to my exact folders if if things like targetfolder are commands for applescript.

There is no change to apply to the script.

You just have to install it it the folder dedicated to folder actions scripts :
“/Library/Scripts/Folder Action Scripts/”

and use the dedicated tool to attach it to your folder :
“/Library/Scripts/Folder Actions/Attach Script to Folder.scpt”

When the script will be attached to the folder, every time a file will be added to the folder with the name “wxyz-final.jpg” or “azert-final.pdf”. it will be moved in the subfolder “photos final” of the folder.

It’s the script itself which stores the value ( name of thisFolder) in the variable folderName
It’s the script itself which stores the value (path of (container of thisFolder)) in the variable folderContainer
It’s the script itself which stores the value (folderContainer & targetName & “:”) in the variable targetFolder

I apologize but I feel unable to teach you the basics of the Applescript language.

Yvan KOENIG running Sierra 10.12.0 in French (VALLAURIS, France) dimanche 23 octobre 2016 20:57:41

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: