Duplicate file into a new folder automatically..

I’ve been searching the net for a solution to this and through my lack of experience and lack of knowledge I’m probably not wording my question correctly, I hope one of you can work out what I need and write a script to do the job.

I have a folder called ‘my photos’ which I edit a photo through various phases until i create a ‘-final’ one. What I want is once I name the photo ‘forest-final’ or ‘fish-final’ it will duplicate automatically to the ‘photos final’ folder under the same parent folder. The tree of my folders is ‘my Photos’ with ‘photos’ and ‘photos final’ inside it.

Please help as I’m pulling my hair out right now and I’m simply not smart enough to work this out.

Thanks in advance.

Compile and save this script :

use scripting additions
use framework "Foundation"
use script "BridgePlus"
load framework

on adding folder items to thisFolder after receiving theseItems
	set {folderName, folderContainer} to my getNameAndContainer:(POSIX path of thisFolder)
	set targetFolder to my createFolderNamed:"photos final" inFolder:folderContainer
	tell application "System Events"
		repeat with aFile in theseItems
			if name of aFile contains "-final." then
				set targetFile to (my buildFullPath:(name of aFile) inFolder:targetFolder)
				set {theResult, theError} to (current application's SMSForder's moveItemAt:(POSIX path of aFile) toItem:targetFile replace:true |error|:(specifier))
				if theResult as boolean is false then error (theError's localizedDescription() as text)
			end if
		end repeat
	end tell
end adding folder items to

#=====#=====#=====#=====#=====#=====

on buildFullPath:proposedName inFolder:PosixPath # appelé par une instruction
	local theFolderURL, proposedName, theDestURL
	
	log linefeed & "# Entre dans buildFullPathname" & linefeed
	
	set theFolderURL to current application's |NSURL|'s fileURLWithPath:PosixPath
	if class of proposedName is text then set proposedName to current application's NSString's stringWithString:proposedName
	set proposedName to proposedName's stringByReplacingOccurrencesOfString:"/" withString:":"
	set theDestURL to theFolderURL's URLByAppendingPathComponent:proposedName
	
	log linefeed & "# Sort de buildFullPath" & linefeed
	
	return theDestURL's |path| as text
end buildFullPath:inFolder:

#=====#=====#=====#=====#=====#=====

-- Creates a new folder. There is no error if the folder already exists, and it will also create intermediate folders if required
on createFolderNamed:proposedName inFolder:PosixPath
	
	log linefeed & "# Entre dans createFolderNamed" & linefeed
	
	set theFolderURL to current application's |NSURL|'s fileURLWithPath:PosixPath
	if class of proposedName is text then set proposedName to current application's NSString's stringWithString:proposedName
	set proposedName to proposedName's stringByReplacingOccurrencesOfString:"/" withString:":"
	set theDestURL to theFolderURL's URLByAppendingPathComponent:proposedName
	set theFileManager to current application's NSFileManager's |defaultManager|()
	--set {theResult, theError} to theFileManager's createDirectoryAtURL:theDestURL withIntermediateDirectories:true attributes:(missing value) |error|:(specifier)
	theFileManager's createDirectoryAtURL:theDestURL withIntermediateDirectories:true attributes:(missing value) |error|:(missing value)
	--if not (theResult as boolean) then error (theError's |localizedDescription|() as text)
	
	log linefeed & "# Sort de createFolderNamed" & linefeed
	
	return theDestURL's |path| as text
end createFolderNamed:inFolder:

#=====#=====#=====#=====#=====#=====

Attach it to the folder “my:folder:containing:photos”
When the folder will receive a file named “my wonderful photo-final.extension” the script will move it in the folder “my:folder:containing:photos final”.

It uses BridgePlus available for free at : https://www.macosxautomation.com/applescript/apps/BridgePlus.html

Yvan KOENIG running Sierra 10.12.0 in French (VALLAURIS, France) jeudi 20 octobre 2016 10:05:57

Thank you very much. I’ll let you know how it goes. :smiley:

Hi, I’ve just tried the script and I got error after error unfortunately.

‘A application constant or consideration can’t go after this identifier’ it is highlighting use scripting additions when it comes up with that error.

Could this be anything to do with me running 10.7.5 or is that a syntax error within the script?

Thanks again for your assistance.

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?