Script to trim file name to 6 characters

Hi, I am after a script that will trim the size of a file name and keep just the first 6 digits, for example it would like to trim the file name 507597OW_VC.eps to just 507597.eps. Is this at all possible and if so how can I do it? I am working on OS 10.6.8. Thank you.

Mac OS X (10.6.8)

Posted on Nov 6, 2013 7:49 AM

Reply
16 replies

Nov 6, 2013 8:04 AM in response to Tonybrian1979

I haven't tested this because I don't have any eps files handy so test it on a scratch folder first, but this should work:


set theFiles to (choose fileof type "com.adobe.postscript" with multiple selections allowed)


tell application "System Events"

repeat with thisFile in theFiles

set fName to (get name of thisFile)

if length of fName > 10 then

set newFName to text 1 thru 6 of fName & text -4 thru -1 of fName

set name of thisFile to newFName

end if

end repeat

end tell

Nov 6, 2013 8:11 AM in response to Tonybrian1979

The following uses a general-purpose handler that deals with whatever extension (or none):

on run -- example   set theFile to (choose file)   set {theName, theExtension} to (getNameAndExtension from theFile)   try -- trim name, skipping errors (already short, etc)     set theName to text 1 thru 6 of theName -- keep first 6 characters   end try   set fileName to theName & theExtension   display dialog fileName with title "Shortened File Name" -- or rename, whatever end run on getNameAndExtension from someItem -- return name and extension from a file item   tell application "System Events" to tell disk item (someItem as text)     set {namePart, extensionPart} to {name, name extension}   end tell   if extensionPart is not "" then     set namePart to text 1 thru -((count extensionPart) + 2) of namePart -- just the name     set extensionPart to "." & extensionPart   end if   return {namePart, extensionPart} end getNameAndExtension

Nov 6, 2013 8:26 AM in response to Tonybrian1979

Sure - the example run handler I posted can be changed to get multiple files and step through them:

on run -- example with multiple files   set theFiles to (choose file with multiple selections allowed)   repeat with aFile in theFiles     set {theName, theExtension} to (getNameAndExtension from aFile)     try -- trim name, skipping errors (already short, etc)       set theName to text 1 thru 6 of theName -- keep first 6 characters     end try     set fileName to theName & theExtension     display dialog fileName with title "Shortened File Name" -- or rename, whatever   end repeat end run

Nov 6, 2013 8:50 AM in response to Tonybrian1979

to make it work with any file, delete the of type "com.adobe.postscript" phrase. then you'll be able to select any file you want. however, if you have file extensions that are not exactly three characters long you need to modify the script:


set theFiles to (choose fileof type "com.adobe.postscript" with multiple selections allowed)


tell application "System Events"

repeat with thisFile in fileList

set fName to (get name of thisFile)


-- extract extension form original name to add to new name

set fExtension to (get name extension of thisFile)


-- only work on file names that are (6 chars plus the period plus the length of the extension) long

if length of fName > 7 + (length of fExtension) then

set newFName to text 1 thru 6 of fName & "." & fExtension

set name of thisFile to newFName

end if

end repeat

end tell


edited by twtwtw to correct oversights

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Script to trim file name to 6 characters

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.