I’m trying to write a script that will rename files within nested folders. For example lets say I have a folder layout as shown below:-
x - a folder
y - a folder within x
z - a folder within y
file.jpg - a file within z
What I’m trying to do is add the folder names x,y,z to the filename of the file so I’d end up with the filename ‘xyzfile.jpg’.
So far I come up with the script below. To do this I’m trying to extract the relevant bits from the file path of the file, so if the file path was ‘Macintosh HD:Digital Camera Shots:x:y:z:file.jpg’ the last 4 parts of the path would give me what I want i.e. xyzfile.jpg.
I’m having problems in that when I run the script I get the error ‘Can’t make “g” into a number’, can anyone help please?
I’ve included a number of display dialogs down the line to see what’s happening but I’m abit stuck.
Any help would be appreciated.
Thanks
Nick
on open (someitems)
repeat with theitem in someitems
tell application "Finder"
set theFilePath to my nameOf(theitem)
display dialog theFilePath
end tell
end repeat
end open
on nameOf(aFile)
set aBackwardsFilePath to (reverse of characters of (aFile as text) as text)
display dialog aBackwardsFilePath
set nameLength to (length of aBackwardsFilePath)
display dialog nameLength
set numOfColons to 0
set charCount to 1
set theFilename to "" as text
repeat until numOfColons = 3
set char to (character charCount of (aBackwardsFilePath as text) as text)
display dialog "char = " & char
if char = ":" then
numOfColons = numOfColons + 1
else
display dialog theFilename
theFilename = (theFilename + char) as text
charCount = charCount + 1
end if
end repeat
display dialog theFilename
end nameOf
I’ve tried the script and it worked fine. I’ve been running through the script to try and understand what’s happening.
After posting the query I’d started playing with something slightly different, I’ve attached the code below.
set holdDelims to AppleScript's text item delimiters
set AppleScript's text item delimiters to {":"}
set itemPath to anItem as string -- thisItem is in alias file format
set ItemName to last text item of itemPath
set itemFolder to the text item -2 of itemPath
set itemFolder2 to the text item -3 of itemPath
set itemFolder3 to the text item -4 of itemPath
set AppleScript's text item delimiters to holdDelims
set theNewFileName to itemFolder3 & itemFolder2 & itemFolder & "_" & ItemName
set name of anItem to theNewFileName
This seems to do the same thing but in a slightly different way.
I amended the script so the user selects a folder and all the files within that folder are renamed accordingly.
While what you’ve done is technically fine, they are actually doing the exact same thing except that the script I posted allows for much more flexibility to various situations that the script may encounter. For instance, if you use your script and the files processed are not in a folder structure of at least 3 folders deep, your script will error and you do not adjust for this. Also, if a file is a package file whose name will end with a colon (I know, not the intent of the script itself but this may be useful to someone else or even to you in the future) or if the item is a folder, then your script will leave you with unwanted results.
The two scripts are doing the exact same thing (other than adding an underscore to the name), it’s just that in mine, the script is a little smarter about which text items to extract and uses try statements just in case the files are not filed so deeply.
Having looked at my script I think some error handling might be a good idea. The folder structure of the files should be the same everytime however if a slip were made then that could cause problems.
I’m going to have to go away and look up ‘package files’, having read your comments they could potentially cause a problem.
The final script I came up with looks like this:-
display dialog "RENAMER
Use with caution as the process cannot be undone easily!
Click OK to choose the main folder or click Cancel to stop." with icon caution
choose folder
set theFolder to result
parseFolder(theFolder)
on parseFolder(aFolder)
tell application "Finder"
set theNumItems to number of items in aFolder
end tell
repeat with i from 1 to theNumItems
tell application "Finder"
set anItem to item i of aFolder
if kind of anItem = "folder" then
set continueProc to true
else
set continueProc to false
set holdDelims to AppleScript's text item delimiters
set AppleScript's text item delimiters to {":"}
set itemPath to anItem as string -- thisItem is in alias file format
set ItemName to last text item of itemPath
set itemFolder to the text item -2 of itemPath
set itemFolder2 to the text item -3 of itemPath
set itemFolder3 to the text item -4 of itemPath
set AppleScript's text item delimiters to holdDelims
set theNewFileName to itemFolder3 & itemFolder2 & itemFolder & "_" & ItemName
set name of anItem to theNewFileName
end if
end tell
if continueProc = true then
--display dialog "Searching folder " & (name of anItem)
parseFolder(anItem)
end if
end repeat
end parseFolder
I think I’ll create a version containing your ‘safer’ code, hopefully it’ll be a bit more bullet proof. As mentioned in my last post I need to go away and have a good look at your version to understand it more, I can understand most of it there are just one or two bits I’m not so sure of. The main bits being:-
tell (a reference to my text item delimiters)
set {old_delim, contents} to {contents, ":"}
set {x, y, z, f} to {"", "", "", text item (item 4 of the_indexes) of aFile}
Do you have any helpful comments on the rest of my script?
I’ve tried the script and it works fine. I did get an error the first time but managed to sort that. Thanks for commenting the code, that’s a big help. I think I need to have a good look at your script and see how certain parts have been ‘streamlined’, I’ve briefly looked at both I can already see parts where I’m doing too much.
I really appreciate the help and the time you’ve spent on this.
I’ve been using the script and everything has been working brilliantly. One thing I was thinking of doing is taking the script one stage further and allowing the user to enter the number of levels to be used for the final filename, at the mo it’s 3. Is it possible to add this functionality to the script given that a few parts of the of the script are fixed to the 3 levels?
Of course the maximum filename length on a Mac would restrict the final length and I guess this would have to be taken into account.
This version would allow the user to enter any number they wish for the number of folders to include in the name. The script will get as many as it can up to the number specified before returning the name: