Truncate Long File Name?

Get help. Get answers. Let others lend you a hand.

Moderator: Mr_Noodle

Truncate Long File Name? Tue Jun 07, 2016 10:18 pm • by iampariah
Is there a way to have Hazel (by itself, with an AppleScript, or with something configured in Keyboard Maestro) evaluate if a file name is longer than X characters and, if so, chop the name down to be no longer than X characters?

I use an IFTTT recipe to deliver fresh wallpapers from Reddit to Dropbox. Hazel then moves them into the folder from which my Mac rotates my desktop wallpapers. Often of late I've noticed that some of the wallpaper file names are quite long. A couple are more than 200 characters! That screws with my cloud-based backup service... And it's just unsightly. :D
iampariah
 
Posts: 37
Joined: Thu May 12, 2016 1:21 pm

Re: Truncate Long File Name? Thu Jun 09, 2016 11:43 am • by Mr_Noodle
If the characters are only letters and numbers (no spaces or symbols), then you can use a match pattern. Otherwise you'll need to use a script at the moment.
Mr_Noodle
Site Admin
 
Posts: 11872
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

Re: Truncate Long File Name? Thu Jun 09, 2016 12:21 pm • by iampariah
They include spaces. They're the arbitrary names given them by their authors. I read through the match pattern section of the Help but couldn't find a pattern within the filenames for matching.

I did find the below script, but I don't know how to adapt it for use within Hazel. I don't know AS. Would you mind helping me ready it for Hazel's use?

Code: Select all
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 25 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 24 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
iampariah
 
Posts: 37
Joined: Thu May 12, 2016 1:21 pm

Re: Truncate Long File Name? Fri Jun 10, 2016 12:21 pm • by Mr_Noodle
It's a bit beyond the support I can give but looking at it, the script seems to be a bit excessive. I would look around for another one as there's a bunch of stuff in there that doesn't seem to be needed for your uses.
Mr_Noodle
Site Admin
 
Posts: 11872
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

Re: Truncate Long File Name? Wed Mar 22, 2017 8:47 pm • by iampariah
Revisiting. I had the below script working great for months, but now it errors every time--maybe something to do with Sierra? Can anyone help me make it work again?

Code: Select all
tell application "Finder"
   set original_name to name of theFile
   set short_name to characters 1 thru 36 of original_name as string
   set name of theFile to (short_name & ".jpg" as string)
end tell
iampariah
 
Posts: 37
Joined: Thu May 12, 2016 1:21 pm

Re: Truncate Long File Name? Thu Mar 23, 2017 11:43 am • by Mr_Noodle
Check the logs for errors as will help track down the problem.
Mr_Noodle
Site Admin
 
Posts: 11872
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City


Return to Support