Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Applescript to copy file, rename file and assume file name

I have to do the following.


Copy image-file-new from folder 1.

Paste image-file-new in folder 2.

Copy the name of image-file-old.

Rename image-file-old to image-file-old-"Original"

Rename image-file-new with the name from image-file-old.


I'd like to do this with a script since this will get tedious.


Even better, I would like to do that in bulk. The folder each of these new & old image files are contained in are prefixed with the same name. For example, A2.1 **** and A2.1 ********** using a space as a delimiter. So If the script could match up folder prefixes from two chosen directories, then perform those actions on any matching folders, that would be even better.


Josh

Posted on Apr 20, 2016 4:42 PM

Reply
3 replies

Apr 20, 2016 9:25 PM in response to jfordtmc

I get what you're trying to do - some simple file duplication and renaming, but I'm somewhat lost as to where you're getting your data from.


You start by saying you're duplicating files, but then go on to talk about matching directory names.

You also talk about multiple different directories using different terms... you have 'folder 1' and 'folder 2' in your step-by-step, both of which are related to the 'new' image file, but you refer to only two directories in your summary - 'old' and 'new'.


In short, each of the step-by-step actions are, in themselves, pretty trivial, but it's how they link together that's not apparent (at least to me).


Can you give an example of the initial states and what you want to end up with?

Apr 21, 2016 8:02 AM in response to Camelot

Sorry for the confusion.


I'll assume the first part was clear and I'll clarify the latter. You're right, I referred to folders as directories later in that description.


What I was trying to say is that it would be nice to do this for several files. Rather than try again, I will describe my exact scenario.


I use a takeoff software called Planswift (PS). You can view construction plans in this software. They store the page image and everything associated with that page in 'Page' folders in the back ground. This happens every time you import a new page. It should be noted that you can place these pages in a folder structure, which is then replicated in the back ground. They are all kept in a Parent folder called 'Pages'.


I have page folders at the 'Pages' level named with a page number for the prefix and then a description, i.e. "A2.1 ***". I have another folder at the 'Pages' level called 'Arch Renamed. In that folder are more page folders named with a page number for the prefix and then a description, i.e. "A2.1 ******".. So the two Parent Level Folders I would want the script to look at are 'Pages' and 'Arch Renamed' folders.


In the page folders (A2.1 ***) there is an image file, at .TIFF to be specific. Those are the files I need to work with.


You would tell the script that one of those folders contains the 'new' file and the other the 'old'. I need the script to match the prefix of one folder to another from those two locations. Once a match as been made, then the steps from the beginning of my post can be performed on the image files in those folders.


As an aside, this is necessary from time to time because PS has no way of handling page replacement and unfortunately, on plan sets that can reach 300-500 pages, this can be a real problem if a developer or contractor sends you a completely revised set of plans.


Hope this helps. I've also attached a few images.

Josh

User uploaded fileUser uploaded file

Apr 22, 2016 4:45 PM in response to jfordtmc

This is what we came up with. As an aside, I find it funny there isn't a Syntax Highlighting option for Applescript on Apple's forums. Lol. Unless of course I'm missing something.

set oldPlansFolderPath to (choose folderwith prompt "Select folder with old plans")

set newPlansFolderPath to (choose folderwith prompt "Select folder with updated plans")

set AppleScript'stext item delimiters to " "

tell application "Finder"



set oldPlans to every folder in oldPlansFolderPath

set newPlans to every folder in newPlansFolderPath

set numberOfOldFolders to countitems of oldPlans

set numberOfNewFolders to countitems of newPlans

repeat with n from 1 to numberOfOldFolders by 1

repeat with i from 1 to numberOfNewFolders by 1

local newFolderName

set newFolderName to name of itemi of newPlans

local oldFolderName

set oldFolderName to name of itemn of oldPlans

if first word of oldFolderName = first word of newFolderName then

set fileName to (name of some file of itemn of oldPlans whose name extension is "tiff")

set oldFile to (some file of itemn of oldPlans whose name extension is "tiff")

set newFile to (some file of itemi of newPlans whose name extension is "tiff")

set newFileName to name of newFile

set the name of oldFile to "Original - " & fileName


duplicate (some file of itemi of newPlans whose name extension is "tiff") toitemn of oldPlans

set (the name of some file of itemn of oldPlans whose name is newFileName) to fileName


end if

end repeat

end repeat

end tell

Josh

Applescript to copy file, rename file and assume file name

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.