Hi everyone,
Sorry if my question seems simple but I’m new to scripting.
The question is at the end of this post.
Here is my current workflow:
I have folders with this kind of name:
00002032
00002033
00002034
etc etc,
I remove the 4 leading zero using batch rename
then it looks like this:
2032
2033
2034
The files contains in the folders look like this:
000020340001
000020340002
000020340003
I then run this script to rename the files so now they look like this:
2034_01
2034_02
2034_03
NOTE: I actually select the folder that contains the subfolders and the script rename all files from all subfolders
tell application “Finder”
set a to every folder of (choose folder)
repeat with aa in a
set Base_Name to my MakeBase(aa as string)
set count_er to 1
set all_files to (every file in aa)
repeat with ff in all_files
set ff’s name to (Base_Name & “.” & (text -2 thru -1 of (“00” & (count_er as string))) & “.” & (ff’s name extension))
set count_er to count_er + 1
end repeat
end repeat
end tell
So my question is how to add something to this script to remove the 4 leading zeros to the folders name (rather than using batch rename?). The idea is to have only 1 script to do everything.
tell application "Finder"
set a to every folder of (choose folder)
repeat with aa in a
set aa to aa as alias
set aa to[b] my [/b]RenameFolder(aa)
set Base_Name to my MakeBase(aa as string)
set count_er to 1
set all_files to (every file in aa)
repeat with ff in all_files
set ff's name to (Base_Name & "." & (text -2 thru -1 of ("00" & (count_er as string))) & "." & (ff's name extension))
set count_er to count_er + 1
end repeat
end repeat
end tell
on RenameFolder(aFolder)
tell application "Finder"
set folderName to name of aFolder
set AppleScript's text item delimiters to {"0000"}
set newName to text item 2 of folderName
set the name of newFolder to newName
end tell
return aFolder
end RenameFolder
Thank you estockly,
I just ran it multiple times and it doesn’t work.
It says: Finder got an error: Can’t continue RenameFolder.
As the folders need to be renamed before the files I tried to put your script before the one that I already have (that rename the files after folders name). It doesn’t work either.
Hi. You should get accustomed to using descriptive variables from the start, as the alternative complicates matters. As long as the file names won’t be significantly longer than what you’ve exemplified, a simple way to do your rename task would be to coerce the names to a number, which would drop the leading zeroes.
tell application "Finder"
repeat with aFolder in (get my (choose folder)'s folders)
set aFolder's name to aFolder's name as number
repeat with aFile in (get aFolder's files)
set dispNom to aFile's displayed name
set aFile's name to ((get dispNom as number) as text)'s text 1 thru 4 & "_" & dispNom's text -2 thru -1 & "." & aFile's name extension
end repeat
end repeat
end tell
That’s because the call to that handler is in a Finder tell block. And Finder has no idea of what a handler is.
You need to say my RenameFolder(), just as in the next line.
Um… you do have a MakeBase handler?
estockly’s RenameFolder() has another problem: it does not store/restore text item delimiters, which could cause unexpected results later. Improved version below:
on RenameFolder(aFolder)
tell application "Finder"
set folderName to name of aFolder
set {TIDs, AppleScript's text item delimiters} to {AppleScript's text item delimiters, "0000"}
set newName to text item 2 of folderName
set AppleScript's text item delimiters to TIDs
set the name of newFolder to newName
end tell
return aFolder
end RenameFolder
You should get into the habit of always storing/restoring text item delimiters when using them.
I just wrote a note in bold to my initial post. I have the feeling the element I forgot to mention might have confused the persons who tried to help me.
Oops. That’s what happens when you don’t test things. Replace
[format]repeat with aFolder in my (choose folder) [/format]–an alias
with:
[format]repeat with aFolder in (get my (choose folder)'s folders) [/format]–a Finder reference list