Solution to Illustrator CS2 File Location and Apply Character Style

I am a graphic designer who prints a lot of proofs for sales people in the office. The problem I kept coming across was when I delivered the printed proof, the sales person may not know where the file was to be able to send to the customer. So I wanted to add the location of the file in the bleed or slug area of each file.

So I created this script to be able to have the computer do all the work for me. The script takes the current file (note, the file should be saved before running this script), locates the actual file in the finder, gets the location of the file, then removes the first 8 characters (I do this to get rid of the “/Volumes” that appears from a networked server). After it gets this info, the script returns to Illustrator, sets the document ruler orign back to 0,0 (the bottom left corner), then imports the character style, makes the text frame with the file location text, applies the style and then places the text box at the ruler orgin.

One of the obstacles I had was trying to apply the character style. I found several post that told me how to create a new style and apply it, but I could never get it to choose the font I wanted. So I created a new document with a line of text, and the character style I want to use, that way I could easily import the style and apply it without having to define all the features of the style. You could easily save a file with multiple character style to be able to use them in your scripts.

This is my first submission of code, so if you have any comments, good or bad, please let me know. I am new to scripting and have lots to learn. I only have tried this on Illustrator CS2, so I am not sure how it would work on other versions. I hope this code helps someone as much as other post have helped me.

tell application "Adobe Illustrator"
	set filePath to file path of current document
end tell

tell application "Finder"
	activate
	reveal filePath
	set the_selection to the selection
	if (the_selection is {}) then my displayError("Sorry, nothing is selected in the Finder. Please try again.")
	set the_folder to POSIX path of ((item 1 of the_selection) as alias)
end tell

set comp_file_name to ((characters 9 thru end of the_folder) as string)

on displayError(msg)
	tell application (path to frontmost application as Unicode text)
		display dialog msg buttons {"OK"} default button 1 with icon 2 giving up after 5
	end tell
	error number -128
end displayError

tell application "Adobe Illustrator"
	activate
	set thisdoc to current document
	set ruler origin of thisdoc to {0.0, 0.0}
	import character styles thisdoc from "~/Documents/Character Styles/compfilenameChST.ai"
	make new text frame in thisdoc with properties {name:"File Path", contents:comp_file_name}
	apply character style character style "compfilenameChST" of thisdoc to text range of text frame "File Path" of thisdoc
	set the position of text frame "File Path" of thisdoc to {0, 0}
	
end tell

Hi Hockey

Nice script.
Had a problem with it though if the illustrator doc had layers had to flatten them to get rid of error.
also adjusted it abit so the character style does change to the specified font.

tell application "Illustrator CS"
	set filePath to file path of current document
end tell

tell application "Finder"
	activate
	reveal filePath
	set the_selection to the selection
	if (the_selection is {}) then my displayError("Sorry, nothing is selected in the Finder. Please try again.")
	set the_folder to POSIX path of ((item 1 of the_selection) as alias)
end tell

set comp_file_name to ((characters 8 thru end of the_folder) as string)

on displayError(msg)
	tell application (path to frontmost application as Unicode text)
		display dialog msg buttons {"OK"} default button 1 with icon 2 giving up after 5
	end tell
	error number -128
end displayError

tell application "Illustrator CS"
	activate
	set thisdoc to current document
	set ruler origin of thisdoc to {0.0, 0.0}
	make new character style in thisdoc with properties {name:"Ch_style"}
	set the size of character style "Ch_style" of thisdoc to 12
	set the text font of character style "Ch_style" of thisdoc to text font "Helvetica"
	set textRef to make new text frame in thisdoc with properties {name:"File Path", position:{0, 0}, contents:comp_file_name}
	apply character style character style "Ch_style" of thisdoc to text range of text frame "File Path" of thisdoc
end tell