AI Document Rename (Simple)

Hi all,

A while back this community gave me quality help with a script I was trying to write. The script opened and resaved AI documents (Adobe Illustator).
After usuing my script for a while I found a bug in Illustrator that I am now trying to retify in Applescript.

Currently if the script loads a file name such as:

filename.bad.eps

After Illustrator opens and saves it, the name becomes:

filename.eps

This poses a problem as my naming conventions are essential to the smooth running of the business.
So the problem is:

If someone could write the code which would allow me to select a directory with the files and, go through each file which can ONLY be “.ai” “.eps” OR “.pdf” and convert any “.” (point) symbols to “_” underscores. However if there is no point symbols present, other than the ones in the extension, just to leave it as is.

I am assuming that a simple loop would be required as well as a name check of the extensions to see if the last 4 characters match. Unfortunately my Apple Script is quite rusty :frowning:

To provide furthur explanation"

filename.bad.eps
filename.eps
filename.eps.is.bad.ai

These should become through the correct script:

filename_bad.eps
filename.eps
filename_eps_is_bad.ai

I extend my thanks to the BBS pre-emptively as everyone here is always very helpful and kind.

Thanks all,

Dave

Try this Dave,

set sourceFolder to choose folder with prompt "Choose your source folder"
tell application "Finder"
	set listFiles to get the name of every file of folder sourceFolder as list
end tell

repeat with thisFile in listFiles
	try
		set oldDelims to AppleScript's text item delimiters --capture the TID's so we can reset them later
		set AppleScript's text item delimiters to "." --set the TID's to a period so we can split the file name up by periods
		set nameItems to the text items of thisFile --get every item between periods
		set AppleScript's text item delimiters to oldDelims --once we get the text items, reset the TID's
		set noExt to the (number of items in nameItems) - 1 --this is the number of items we have to build a new name with (minus the last one, the extension)
		set newname to item 1 of nameItems as string --set this to the first item in the name
		repeat with thisItem from 2 to noExt --repeat, starting with the second piece of the name, and end with the second to last
			set newname to newname & "_" & item thisItem of nameItems as string --continue to rebuild the name, separating each piece with your underscore
		end repeat
		set newname to newname & "." & the last item of nameItems as string --when you get the name reassembled add the file extension
		set thisAlias to sourceFolder & thisFile as string --define the path to the file as it exists now
		tell application "Finder" to set the name of file thisAlias to newname --rename it
	on error
		set AppleScript's text item delimiters to oldDelims --if there are any errors the TID's are set back
	end try
end repeat

Good luck!

Mytzlscript thanks for that!!!

I’ll give it a run and see how it goes.

Dave

Hi Dave and Mytzlscript :slight_smile:

Here another suggestion (tested with OsX 10.24):


--Script to replace "." by "_" in files name
on run
  tell application "Finder"
    try
      activate
      --List files from folder to choose
      set FileList to every file of ¬
        (choose folder with prompt "Choose the source folder :")
      --Loop to considering each file
      repeat with FileItem in FileList
        --The original file name
        set FileItemName to name of FileItem
        --If the original file name contains the "."
        if "." is in FileItemName then
          --Extract extension only
          set FileExt to my ExtSansNom(FileItemName)
          --Extract name without extension
          set FileName to my NomSansExt(FileItemName)
          --If short name contains the "."
          if "." is in FileName then
            --Replace every "." by "_"
            set text item delimiters of AppleScript to "."
            set FileNameList to text items of FileName
            set text item delimiters of AppleScript to "_"
            set FileName to FileNameList as text
            set text item delimiters of AppleScript to ""
            --Recompose the new name
            set NewFileName to (FileName & FileExt) as text
            --Rename the file
            set name of FileItem to NewFileName
          end if
        end if
      end repeat
    on error MsgErr number NroErr
      if NroErr is not -128 then ¬
        display dialog "" & NroErr & " : " & MsgErr with icon 0
    end try
  end tell
end run

--Handler to extract the last extension in file name
--Source "NomExtension" <http://wirinum.free.fr/>
on ExtSansNom(Nom)
  if ("." is in Nom) then return (text -(offset of "." in ¬
    ("" & (reverse of (characters of Nom)))) thru -1 of Nom)
  return Nom
end ExtSansNom

--Handler to extract the name of files without the last extension
--Source "NomExtension" <http://wirinum.free.fr/>
on NomSansExt(Nom)
  if ("." is in Nom) then return (text 1 thru -((offset of "." in ¬
    ("" & (reverse of (characters of Nom)))) + 1) of Nom)
  return Nom
end NomSansExt

:wink:

Mytzlscript and Fredo I can’t thanks you both enough.

Mytzlscript, I am currently using your script and it is working perfectly. I can’t thank you enough. I learnt quite alot from it and I especially like your approach by where you took all the extensions, I was initially going to try to exclude .eps, .ai etc. but your approach was definately much more robust as it now expands this scripts utility furthur :slight_smile: Thankyou.

Fredo, good to have you back buddy! As for your script… WOW. I’ll definately give it a while once I make the big transition to OSX. For the time being I’m still constrained to 9 :frowning:

Thanks once again,

Dave

Hi Dave :slight_smile:

In the company where I work, I am also constrained to still use MacOs9.
I have just tested my script under MacOs9, such as it is, and it functions very well.
You can test it if you want, but they is true that the script of “Mytzlscript” is very good.

Thank you for your encouraging joviality… :wink: