As the subject suggests, I need to convert all the files in a folder to be only the first nine characters, and if this results in a duplication, then add a suffix of a, b, c, etc.
I don’t think this is a complicated script for someone who knows, however I’m stumped.
set this_folder to choose folder with prompt "Choose a folder to fix filenames:"
tell application "Finder"
set all_files to every file in folder this_folder
repeat with a_file in all_files
set f_name to a_file's name
set f_ext to a_file's name extension
set p_off to offset of f_ext in f_name
if p_off is greater than 11 then
set f_name to (characters 1 thru 9 of f_name & "." & f_ext) as string
my CheckName(f_name, this_folder, f_ext, a_file)
end if
end repeat
end tell
on CheckName(fn, fld, ex, fl)
set next_let to 97
tell application "Finder"
set all_names to the name of every file in folder fld
if all_names contains fn then
repeat until all_names does not contain fn
set fn to (characters 1 thru 8 of fn & (ASCII character next_let) & "." & ex) as string
set next_let to next_let + 1
end repeat
end if
set name of fl to fn
end tell
end CheckName
Of course, if there are more than 26 of any given 9 character filename files, this will continue on with the ASCII characters after the letter z, and I have no idea what will happen. Perhaps your machine will explode. Anyway, I tested it with a bunch of different length extensions, and a bunch of files with the same 9 characters in their filenames, and it seems to work. If your chosen folder is huge, it may take awhile to run, so be prepared for that.
The only thing I need to be different is that the 9th character not be replaced with the suffix, but rather the suffix be the 10th character when required.
Thank you so much, this is huge hurdle for me is a big race I’m running.
Hello, I wanted to use this scrip to truncate some filenames to the first 45 characters only, but it is the first time that I’m using AppleScript and when I click play, it works only if the files have 45 or more characters, if they have less characters the scrip gives me an error.
What should I modify from the script to solve this?
To truncate some filenames to the first 45 characters only, replace thru 9 and thru 8 in the script above with thru 45 and thru 44 respectively.
The script provided above works correctly. There is only one drawback. Code:
if all_names contains fn then
repeat until all_names does not contain fn
set fn to (characters 1 thru 8 of fn & (ASCII character next_let) & "." & ex) as string
set next_let to next_let + 1
end repeat
end if
can be replaced with this:
repeat while all_names contains fn
set fn to (characters 1 thru 8 of fn & (ASCII character next_let) & "." & ex) as string
set next_let to next_let + 1
end repeat
As I am fond of multipurpose scripts, I enhanced a bit the proposed script and I introduced some useful changes.
set this_folder to choose folder with prompt "Choose a folder to fix filenames:"
set theButtons to {"Cancel", "OK"}
set reply to display dialog "Define the number of characters to keep before the extension" default answer "45" buttons theButtons default button 2 cancel button 1
if button returned of reply is theButtons's item 1 then error number -128
set nbChar to text returned of reply as integer
tell application "Finder"
set all_files to every file in folder this_folder
repeat with a_file in all_files
set f_name to a_file's name
set f_ext to a_file's name extension
set p_off to offset of f_ext in f_name
if p_off > nbChar + 2 then # EDITED
set f_name to text 1 thru (nbChar + 1) of f_name & "." & f_ext
my CheckName(f_name, this_folder, f_ext, a_file, nbChar)
end if
end repeat
end tell
on CheckName(fn, fld, ex, fl, nbChar)
set next_let to 97
tell application "Finder"
set all_names to the name of every file in folder fld
repeat while all_names contains fn
tell me to set fn to text 1 thru nbChar of fn & (character id next_let) & "." & ex
set next_let to next_let + 1
end repeat
set name of fl to fn
end tell
end CheckName
I replaced characters by text and the deprecated ASCII character by character id.
[format]ASCII character
Returns the character for a specified number.
Important: This command is deprecated starting in AppleScript 2.0—use the id property of the text class instead.[/format]
Using text allowed me to drop the as string coercions.
Funny behavior, ASCII character which belong to AppleScript was understood by Finder but character id which belongs to Standard Additions is not so it was necessary to insert "tell me to " at the beginning of the instruction.
If you look carefully you will also that I dropped some parenthesis and an if … then … end instruction which was useless.
Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) mardi 20 aout 2019 16:12:33
Edited an instruction:
[format]if p_off > nbChar + 2 then # EDITED[/format]
But, I tried both of your Scripts and it keeps telling me the same error: error “Can’t get characters 1 thru 45 of "CAT_vo_com_dimensio_number_00.mp3".” number -1728 from characters 1 thru 45 of “CAT_vo_com_dimensio_number_00.mp3”.
From this message I understand that the script doesn’t find 45 characters in this file, but all I want is to apply it as a filter, so all the files are 45 or under 45 characters. Does anyone know which could be the problem?