a newbie so please excuse long subject line.
Renaming files in folder with n, n+1, … where n = numeric variable input prior to running the script.
I need to rename files in various folders with numeric character. Files are currently always named “untitled-1”, “untitled-2”, etc. For each folder I know what number I will need to begin with. After renaming I can put all files in same folder with no name duplicates and all in numeric order.
I did a record under Finder but cannot work out how to edit it to do what I want. Script below:
tell application “Finder”
select folder “KCBC Scans”
select file “Untitled-1.tif” of folder “KCBC Scans”
set name of selection to “3.tif”
– I want to specify a starting numeric for “3.tif”, i.e. n
select file “Untitled-2.tif” of folder “KCBC Scans”
set name of selection to “n+1.tif”
– I want to add 1 to starting numeric and repeat till all files in folder renamed
Any help appreciated.
The following, long winded post, will work if you are religious about getting the highest number used in the folder with all of your renamed scans. Sorry it is so heavily commented, I know when I was new to scripting it was nice to have a little explanation next to the code - comments saved me hours of creating new expletives. Here goes.
set KCBCErrorOccured to false
--if there are any errors we'll set this to true and use it later
--to notify the user. Or not if you don't want that
set FolderPath to choose folder with prompt "Choose a folder of files to rename"
--define the path to the folder of scans or you can hard code it
--if it will always be the same folder by using the line below
--set FolderPath to "Mac HD:Desktop Folder:KCBC Scans:"
--be sure you change the path to reflect where the folder is on
--your computer - this must end in ":" so the Mac know's it is a folder
tell application "Finder"
display dialog "Choose a number to begin numbering" default answer "" with icon note
set n to the text returned of the result
--instruct the Finder to prompt the user for textual input and set n to it
set FileList to get the name of every file of folder FolderPath whose name contains "Unt"
--this is a more efficient method of getting a list of items in a folder (files in this case)
--you can remove the "whose name contains "Unt", this just makes sure you are
--only looking at files with Untitled, since you said you know they will always
--be named as such. BTW, this has to be within the Tell Application "Finder" block
set FileList to FileList as list
--okay, some may look at this line and think, isn't what you have already
--a list? Yes, but I've run into problems when there is only 1 file present.
--later on we will repeat a process on every item of FileList and if
--the Finder comes up with 1 file it will try to repeat with every letter of
--the name, since there are no files named "U" or "n" or "t"...etc
--it will freak out.
repeat with ThisFile in FileList
--this is the same way of saying repeat the following with every file name we found
--you can use "x", or "i" or any word or string that strikes you. I prefer to use
--variables longer than "i" or "x" as some of my scripts get pig-like in size with
--multiple repeats- Less confusion for me and my Mac
set ThisFilePath to FolderPath & ThisFile as string
--piece the path to the folder together with the name of the first file
--this is the full path to the file and what makes it possible for the Finder
--to see and work with it
set nAsString to n as string
if the (count of characters in nAsString) = 1 then
set TheNumber to " " & n as string
set TheNumber to n as string
--optional, if you want your files to sort correctly numerically
--add a space in front of single digits. If the final destination
--of these files will house over a hundred files you will have to
--add another couple of lines adding two spaces for double digit numbers.
--or you can print these lines out and burn them shouting "house on fire, house on fire"
set NewName to TheNumber & ".tif" as string
--this will be the new name of the file we on this time
tell application "Finder"
--Now, the best written script should account for user
--malfunction. Say you forget to move your newly, renamed
--files, or forgot what number to start on. Your script can't
--use a name that already exists without either replacing it
--or prompting the user for guidance on what to do. So, for a
--little insurance we'll use the "Try" statement (more on that later)
set the name of file ThisFilePath to NewName
--tell the finder to change the name
--If the "Finder" tries to rename the file and fails for whatever reason
-- the script ends up here.
set KCBCErrorOccured to true --we'll check if this is true or false later
set NewName to n & "error.tif"
--since the file name was already used, if you want the script to continue
--anyway you could make a new name, or just have it skip the file and do nothing
--if it errors
set n to n + 1
--add 1 to the number we are on
if KCBCErrorOccured = true then
display dialog "One or more errors occurred during the renaming process" with icon stop
--if there were errors you'll get a dialog so you can check it out, you could also log exactly
--what happened if you wanted to but I'm sure just looking in the folder would
--tell you what went wrong.
Like I said, you can take out the everything between the try…on error if you don’t want error handling, and remove the part of adding a space to the front of the names to make them sort better if you wish.
I am using OS 8.6 and built and ran this using Script Debugger 136 times.
Good Luck michaeljohn!
After posting this someone suggested that I check on versiontracker using search on rename. There are quite a few shareware progs and I found what I needed. However, appreciate the code as I can tinker with this as well.
Thanks for your generosity in time and knowledge.
I’m surprised that you searched on Versiontracker. :shock:
While no doubt you will find renaming “apps” there, why not look on ScriptBuilders? http://macscripter.net/script-builder.t
There are a lot of scripts here that in all likelyhood do what you need, and don’t take up near the size footprint most of the other apps take. There are some there that I use everyday at work, and come directly from Apple.