Please help with simple illustrator applescript

Hoy!

I’m more into the design thing, and don’t know much about scripting.

I have a (I’m assuming…) simple script request:

In Illustrator (10 or CS, whichever) I need to…

  • open a file

  • select all text objects (there is a Select Menu command for this one)

  • convert each text object into dynamic text/variable with an identical name as the contents of the text box (replacing any spaces with an “_” underscore)(also an option in the variable window in Illustrator)

  • save the variable library to a folder (also an option in the variable window in Illustrator)

Please, please, please… Anyone brave enough to write this one will have my lifelong gratitude and
a thank you postcard from Slovenia. It would really save my…

Thank you, thank you, thank you!

Jure

zlatokosi,

I’m not sure if this is what you’re looking for but it should hopefuly be a start. I don’t usually work with variable data and such but this is what I worked out. CS is the easiest to script for when compared to AI10, so this is for CS.

tell application "Finder"
	choose folder with prompt "Choose variable library folder."-- this sets the path for your xml library
	set theChoice to result as alias
	set destPath to theChoice as string-- you could probably set theChoice to string in the first place
end tell
set nameText to ""
tell application "Illustrator CS"
	activate
	set thisDoc to current document-- this assumes you have a document open. If you need AI to open files that can be done easily.
	set frameCount to count every text frame of thisDoc
	repeat with i from 1 to frameCount
		set theText to contents of text frame i of thisDoc as string
		set charCount to count every character of theText
		set nameText to ""
		
		repeat with j from 1 to charCount
			set char to character j of theText
			if char is " " then
				set nameText to nameText & "_"
			else
				set nameText to nameText & char
			end if
		end repeat
		make new variable in thisDoc with properties {name:nameText, kind:textual}
		
		set properties of text frame i of thisDoc to {content variable:variable nameText of thisDoc}
	end repeat
	make new dataset in thisDoc with properties {name:"firstData"}-- you can set the name to be a variable if you need.
	display dialog "Name your variables." default answer ""-- this is for naming the variable library file. It can be "hard coded" if you want.
	set varName to text returned of the result
	set varPath to destPath & varName & ".xml" as string-- this sets the name of the file that your variable library is saved in.
	export variables of thisDoc to file varPath
end tell

You’ll probably have to tweak this to suit your purposes. If I can help more, let me know.

PreTech

Model: dual 1.8 G5
AppleScript: 2.1 (80)
Browser: Safari 412.5
Operating System: Mac OS X (10.4)

Jure,

I had to make a few changes to the script above. This now allows you to set a folder with files to open and process. There were problems if the folder only contained one file so I modified that. It also closes the current document but does not save that document. You may want to change that.

Here 'Tis

set fileList to {}
tell application "Finder"
	choose folder with prompt "Choose variable library folder."
	set theChoice to result as alias
	set destPath to theChoice as string
	choose folder with prompt "Choose files to work on."
	set sourceFolder to result as alias
	if (count of folder sourceFolder) is 1 then
		set theFile to file 1 of folder sourceFolder as alias
		set fileList to fileList & theFile as list
	else
		set fileList to every file of sourceFolder as alias list
	end if
end tell
set nameText to ""
tell application "Illustrator CS"
	activate
	repeat with aFile in fileList
		open aFile
		set thisDoc to current document
		set frameCount to count every text frame of thisDoc
		if frameCount is 0 then
			display dialog "No text frames in this file."
		else
			repeat with i from 1 to frameCount
				set theText to contents of text frame i of thisDoc as string
				set charCount to count every character of theText
				set nameText to ""
				
				repeat with j from 1 to charCount
					set char to character j of theText
					if char is " " then
						set nameText to nameText & "_"
					else
						set nameText to nameText & char
					end if
				end repeat
				make new variable in thisDoc with properties {name:nameText, kind:textual}
				set obName to (nameText & i) as text
				set properties of text frame i of thisDoc to {content variable:variable nameText of thisDoc}
			end repeat
			make new dataset in thisDoc with properties {name:"firstData"}
			display dialog "Name your variables." default answer ""
			set varName to text returned of the result
			set varPath to destPath & varName & ".xml" as string
			export variables of thisDoc to file varPath
		end if
		close thisDoc saving no
	end repeat
end tell

WOW!! Thank you again. I tested this script and it works wonders. Really, I know that this probably took
quite some time, and am very greatful for your help. Just one more question:

Would it be very hard to include a line of script telling Illustrator to disregard empty text boxes, as now
the script stalls when encountering an empty text field, I assume because variable names can’t be repeated.

Otherwise the script is PERFECT! It does exactly what I need. Where should I send the postcard to?

A gain, thanks.

Jure

PS. Great idea regarding the folders, had’t thought of that…

Jure,

I didn’t think about empty text boxes when writing this but it is an easy fix. This is the whole code with the addition of an if statement around the part dealing with the properties of the text frame.

set fileList to {}
tell application "Finder"
	choose folder with prompt "Choose variable library folder."
	set theChoice to result as alias
	set destPath to theChoice as string
	choose folder with prompt "Choose files to work on."
	set sourceFolder to result as alias
	if (count of folder sourceFolder) is 1 then
		set theFile to file 1 of folder sourceFolder as alias
		set fileList to fileList & theFile as list
	else
		set fileList to every file of sourceFolder as alias list
	end if
end tell
set nameText to ""
tell application "Illustrator CS"
	activate
	repeat with aFile in fileList
		open aFile
		set thisDoc to current document
		set frameCount to count every text frame of thisDoc
		if frameCount is 0 then
			display dialog "No text frames in this file."
		else
			repeat with i from 1 to frameCount
				set theText to contents of text frame i of thisDoc as string
				set charCount to count every character of theText
				set nameText to ""
				
				repeat with j from 1 to charCount
					set char to character j of theText
					if char is " " then
						set nameText to nameText & "_"
					else
						set nameText to nameText & char
					end if
				end repeat
				make new variable in thisDoc with properties {name:nameText, kind:textual}
				set obName to (nameText & i) as text
				if contents of text frame i of thisDoc is not "" then
					set properties of text frame i of thisDoc to {content variable:variable nameText of thisDoc}
				end if
			end repeat
			make new dataset in thisDoc with properties {name:"firstData"}
			display dialog "Name your variables." default answer ""
			set varName to text returned of the result
			set varPath to destPath & varName & ".xml" as string
			export variables of thisDoc to file varPath
		end if
		close thisDoc saving no
	end repeat
end tell

I’m glad this has helped you. I’d like a postcard too. That would be cool. If you click on my name and then send an email to me, I can email you an address.

If I can help again, just let me know.

PreTech