Friday, September 20, 2019

#1 2005-11-15 06:15:32 pm

Tortle2
Member
Registered: 2005-11-16
Posts: 65

Truncate filenames to first 9 characters only.

Hey all,

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.

Could someone help?

Thanks in advance.

Jason B

Offline

 

#2 2005-11-15 06:26:22 pm

Craig Smith
Administrator
From:: Tacoma, WA
Registered: 2005-05-16
Posts: 1027
Website

Re: Truncate filenames to first 9 characters only.

Do the nine characters include the extension (.jpg, .txt, etc.) or is it nine characters before the period?

casdvm


Craig Smith

Offline

 

#3 2005-11-15 06:49:53 pm

Tortle2
Member
Registered: 2005-11-16
Posts: 65

Re: Truncate filenames to first 9 characters only.

Sorry, probably shoul have mentioned that!

I need the extension to stay the same, so yes, only the characters before the period!

Thanks,

Jason B

Offline

 

#4 2005-11-15 07:40:16 pm

Craig Smith
Administrator
From:: Tacoma, WA
Registered: 2005-05-16
Posts: 1027
Website

Re: Truncate filenames to first 9 characters only.

Jason:

Give this script a whirl:

Applescript:

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.

casdvm


Craig Smith


Filed under: Finder

Offline

 

#5 2005-11-15 08:21:00 pm

Tortle2
Member
Registered: 2005-11-16
Posts: 65

Re: Truncate filenames to first 9 characters only.

Ok, so you are a total legend.

It's almost perfect.

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.

Jason

Offline

 

#6 2005-11-15 08:35:42 pm

Tortle2
Member
Registered: 2005-11-16
Posts: 65

Re: Truncate filenames to first 9 characters only.

Actually,

It's Ok, I figured out how to change it.

Thanks once again for your help.

This is just an amazing community!

Jason

Offline

 

#7 2005-11-15 08:39:00 pm

Craig Smith
Administrator
From:: Tacoma, WA
Registered: 2005-05-16
Posts: 1027
Website

Re: Truncate filenames to first 9 characters only.

Jason:

Nice work.  It should have been simple to solve, glad you were able to do so on your own.  Be sure to post again if there are still issues.

casdvm

Oh, hey, thanks for the legend remark.  I am sure it will make the real legends jealous (or at least they will have a good laugh.)


Craig Smith

Offline

 

#8 2019-08-20 01:18:09 am

carlessegura
Member
Registered: 2019-08-19
Posts: 3

Re: Truncate filenames to first 9 characters only.

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?

Thanks smile

Offline

 

#9 2019-08-20 06:32:06 am

KniazidisR
Member
Registered: 2019-03-03
Posts: 544

Re: Truncate filenames to first 9 characters only.

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:

Applescript:


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:

Applescript:


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

Last edited by KniazidisR (2019-08-20 06:57:07 am)


macOS Mojave -- version 10.14.4
Safari -- version 12.1

Offline

 

#10 2019-08-20 08:14:30 am

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 3577

Re: Truncate filenames to first 9 characters only.

As I am fond of multipurpose scripts, I enhanced a bit the proposed script and I introduced some useful changes.

Applescript:

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.

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.

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:

if p_off > nbChar + 2 then # EDITED

Last edited by Yvan Koenig (2019-08-21 04:28:41 am)

Offline

 

#11 2019-08-21 03:25:22 am

carlessegura
Member
Registered: 2019-08-19
Posts: 3

Re: Truncate filenames to first 9 characters only.

First of all, thank you both.

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?

Offline

 

#12 2019-08-21 03:53:55 am

KniazidisR
Member
Registered: 2019-03-03
Posts: 544

Re: Truncate filenames to first 9 characters only.

carlessegura wrote:

First of all, thank you both.
Does anyone know which could be the problem?


replace if p_off is greater than 11 then with

Applescript:

if p_off is greater than 47 then

The second script is better. All you need is change if p_off > 11 then
with this:

Applescript:

if p_off > nbChar+2 then

Last edited by KniazidisR (2019-08-21 04:08:10 am)


macOS Mojave -- version 10.14.4
Safari -- version 12.1

Offline

 

#13 2019-08-21 04:06:20 am

carlessegura
Member
Registered: 2019-08-19
Posts: 3

Re: Truncate filenames to first 9 characters only.

Okey, I think now it works.

You're all f*cking amazing. You don't know, but you saved my a looooooot of time. smile

Thanks to all of you.

Offline

 

#14 2019-08-21 04:26:47 am

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 3577

Re: Truncate filenames to first 9 characters only.

Oops

I forgot to edit an instruction.
Now, message #10 is corrected.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) mercredi 21 aout 2019  12:26:40

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)