Illustrator change text contents...

Hi All …

I have a new one to writing AppleScript, i try to replace /change some filename in illustrator.

Here is my sample script…


tell application "Adobe Illustrator"
	activate
	
	-- Date Create:
	set thisDoc to current document
	set curDay to day of (current date)
	set curMon to month of (current date) as number
	set curMon2 to month of (current date)
	set curYear to year of (current date)
	set docDate to curDay & "-" & curMon & "-" & curYear
	set toDay1 to "Date: " & docDate
	
	--user information create
	
	set the ppList to {"AAA", "BBB", "CCC", "DDD", "EEE"}
	
	set the ppUser to (choose from list ppList with prompt ("Who Create File...") ¬
		OK button name "Choose") as string
	
	if the ppUser is "False" then error number -128
	
	--File information create
	
	display dialog "Enter the File Name:" default answer "JB"
	set filename to the text returned of the result as string
	
	-- Data Variable change...
	
	set the contents of text frame "<<user#_1>>" of current document to ppUser
	set the contents of text frame"<<Filename>>" of current document to filename
	set the contents of text frame "<<date_1>>" of current document to toDay1
	
end tell

i want to change my file name (<>) in the job .
but when i run this script, the script show me error message ,
i try to amend but also can’t finish
can anyone help…

In file i have 2 page , artboard 1 & artboard 2,
artboard 1 set the variable file name is <>_F on the artboard (not text frame name)
artnoard 2 set the variable file name is <>_B on the artboard(not text frame name)

Model: MacBook Pro
AppleScript: AppleScript 2.7
Browser: Firefox 62.0
Operating System: macOS 10.14

In answering this kind of question, it would be very helpful if you could provide a link to download a sample .ai document.

A couple things:

  • You aren’t providing enough information to the script to fully describe the location of the text frame. It needs the whole path to the element, not just the name. Or you can target items directly by name, regardless of the document structure, using “Page Item.”

  • In order for a text frame to have a “name” that Applescript can read, you need to actually assign it a name in the Layers panel - if it shows in the layers panel with just the text contents, that doesn’t actually show up as the name of the object to Applescript. I can’t tell if you’ve done that and named these text fields “<<user#_1>>”, “<>”, etc. Or if that is just the current text contents of the field.

I don’t know what your document looks like, but say you’ve got a layer, with a nested layer, with a group, with the text frames. I named a text frame in the Layers Panel “User 1”. Then:

set ppUser to "Buckaroo Bansai"

tell application "Adobe Illustrator"
	tell the current document
		tell layer "Layer 1"
			tell layer "Layer 2"
				tell group item 1
					set User1TextFrame to text frame "User 1"
					set the contents of User1TextFrame to ppUser
				end tell
			end tell
		end tell
	end tell
end tell

Alternately, using “page items,”:

set ppUser to "Buckaroo Bansai"

tell application "Adobe Illustrator"
	tell the current document
		set User1TextFrame to page item "User 1"
		set the contents of User1TextFrame to ppUser
	end tell
end tell

So the easiest way to do this is to give each text field you need to manipulate a unique name in the Layers panel, then target them each with “Page Item.”

Note that even after you target an item with “Page Item,” the reference Illustrator sets to it is a relative reference based on it’s location in the hierarchy.

So in the example above, the value of “User1TextFrame” is “text frame 1 of group item 1 of layer 1 of layer 1 of document 1.” So if you start modifying objects in the document, what that reference points to (if anything) will change. So if you’re making changes, each time that object needs to be modified, re-acquire the reference with “page item” again.

Feel free to still post the document if you want more help.

Regarding your last question, Artboards aren’t relevant to the object stack in Illustrator - just layers, groups, etc.

Thanks t.spoon reply,

Ok , Let me upload the file for reference…

test2.ai - 1.6 MB

Right, so in your sample document, the text fields you need to manipulate are not named. This can be seen by checking their name in Applescript, which is blank. Or simply by changing the text contents in Illustrator, and seeing that their names in the Layers panel changes to reflect the contents.

It is possible to target them by their contents, but this is really clunky. Unless there’s some compelling reason why you can’t in your scenario, name them and target them by name.

Since your sample document is flat - one layer, no nested layers, no groups - it doesn’t make much difference if you target them by location or use page items to target them directly.