Adobe Illustrator experts - need help on a select text frame script

Hi all,

I have a Illustrator document I use as a Master Template which has a text frame object that contains the below dummy text:

Artwork Reference
Supplier
Product Code
Product Description
(carriage return here)
Brand

My below script “seems” to select (highlight) the entirety of this text ok as I look at the document on screen:

tell application "Adobe Illustrator”

activate

–set legendText1 to "Artwork Reference”

tell document 1

select of {text from character 1 to character 66 of story 4 of document 1 of application "Adobe Illustrator”}

–tell application “System Events” to keystroke “v” using command down

end tell

end tell

BUT when the System Events line is invoked it deselects the text frame and pastes the clipboard contents the middle of the document, when I want it to replace the temporary text. It’s almost as if the text isn’t actually selected/highlighted by the native app and just a faux display from Applescript.
Even if I use the paste clipboard line instead of system events I get the same result.

The text that’s going in is coming from Excel and I’ve sorted the scripting and formatting out for that end but this step eludes me.
The only way I can get it to work is if I manually select/highlight the text frame and Command V myself which negates the need for the script.
I can’t see anything of usefulness in the Illustrator dictionary that would help either.

Any help or pointers greatly received.

You don’t need to use System events. Illustrator has all you need.
If you want to change the entire content of a text frame, it’s best to not rely on the characters count.

tell application id "com.adobe.illustrator"
	activate
	set contents of text frame 4 of document 1 to "Some text"
end tell

Or if the text frame is selected:

	set contents of text range of story of selection to "Some text"

Brilliant - thanks for your solution - works as expected!
I modified your text frame line to include where the text is coming from:

set contents of text range of story of selection to the clipboard

Now I just gotta get the pasted text to have the correct carriage returns in the illustrator text frame!

I use a subroutine to replace any characters or text I want to replace before inserting into text frames. I’m sure it originated here sometime long ago, in a place far away…

on replace_chars(this_text, search_string, replacement_string)
	set AppleScript's text item delimiters to the search_string
	set the item_list to every text item of this_text
	set AppleScript's text item delimiters to the replacement_string
	set this_text to the item_list as string
	set AppleScript's text item delimiters to ""
	return this_text
end replace_chars

You’ll want to replace return or linefeed or any character or test string with whatever you want to use.
Example:
set newString to my replace_chars(oldString, linefeed, return)

This is excellent thank you!
I utilised both of your scripts with some tweaks as per below.

set theClipboard to the clipboard as text

– Split each cell into its own line

set AppleScript’s text item delimiters to tab

set theCells to text items of theClipboard

set AppleScript’s text item delimiters to return

set theModifiedText to theCells as text

– Add two line breaks before the last cell

set theLines to paragraphs of theModifiedText

set theLastLineIndex to count of theLines

set item (theLastLineIndex - 1) of theLines to item (theLastLineIndex - 1) of theLines & return

set theModifiedText to theLines as text

set the clipboard to theModifiedText

tell application “Adobe Illustrator”

activate

tell document 1

select of {text from character 1 to character 66 of story 4 of document 1 of application “Adobe Illustrator”}

set contents of text range of story of selection to the clipboard

end tell

end tell

end

This is using 5 cells copied to the clipboard from Excel and rearranging the line breaks so it fits within my text frame seamlessly.
I need to find out how to change the “story” number tho?
The script works fine when using a specific Illustrator template that has it set to “story 4” but other templates I occasionally use (which have the same text frame and all other attributes) the “story” number seems to change from doc to doc. How do I ensure it’s always “story 4”?

FYI I used this AppleScript to locate the Illustrator selected items details:

tell application “Adobe Illustrator”

tell document 1

get properties of selection

end tell

end tell

I can’t find any pointers where “story” is changeable in Illustrator natively and the AppleScript dictionary doesn’t help me either - is the “story” number random?
Any help as ever greatly received.

Regards

@TheBlackBinLiner The Story number you’re referring to is the Index number. The index is not editable. You need to figure out a way to identify the text frame by contents, name or other properties that can help you identify the text frame if you don’t want to select it manually. You won’t get very far with Story. You should check out the Adobe AppleScripting Reference for Illustrator for more info and play with Script Debugger viewing the info or using test scripts to learn more by experimenting with getting properties of different class elements.