Illustrator replace to text contents.....

Hi all

i try to setup Applescript to re-define job name…but when i use “offset” command to find the position of the string…script alret


Below is my Script…

tell application "Adobe Illustrator"
	activate
	set newJobNumber to (display dialog "Job Number is " with title "JOB Number Define" default answer "JB")
	set textFrames to (every text frame of document 1 whose contents contains "<<" and contents contains ">>" and contents contains "JOB" or ¬
		name starts with "<<" and name ends with ">>" and name contains "JOB" or ¬
		name contains "<<" and name contains ">>" and name contains "JOB")
	
	repeat with aFrame in textFrames
		set aText to contents of aFrame
		
	
		set y to the (offset of "<<" in atext) 
		set z to the (offset of ">>" in atext) 
		set newX to ((text 1 thru -y) of aText) & newJobNumber & text (z + 2) thru -1 of aText
		
		display dialog newX
		delay 1
		
	end repeat
	
end tell

But when i separate to tryout this , can work …

set aText to "JOB name : <<Job_1>>_F"

set y to offset of "<<" in aText
set z to offset of ">>" in aText

set newJobNumber to "JB1234567"
set newX to ((text 1 thru -y) of aText) & newJobNumber & text (z + 2) thru -1 of aText

Can anyone help to solve???

Model: Macbook pro
AppleScript: 2.7
Browser: Firefox 71.0
Operating System: macOS 10.14

set textFrames to (every text frame of document 1 whose contents contains "<<" and contents contains ">>" and contents contains "JOB" or ¬
		name starts with "<<" and name ends with ">>" and name contains "JOB" or ¬
		name contains "<<" and name contains ">>" and name contains "JOB")

There’s some redundancy here:[format]name starts with “<<” and name ends with “>>” and name contains “JOB”[/format]
will be true if and only if this:[format]name contains “<<” and name contains “>>” and name contains “JOB”[/format]
is also true. Therefore, you can delete the first one without affecting the result, which will make the filter perform faster.

I don’t know the details of your documents, but one thing you could quickly think about is whether you definitely need to check both the contents and the name properties for the presence of those words. Your script goes on to iterate through the result of this filter, and only ever utilise and edit the contents, which it assumes definitely contains those words in the contents of every item stored in the variable textFrames. Therefore, either the script is going to throw an error if it comes across one where the contents doesn’t contain those words, or it infers that this condition:[format]name contains “<<” and name contains “>>” and name contains “JOB”[/format]
is redundant as well. It’s looking like every thing that comes after the first or in that filter could possibly be taken out.

Regarding the actual issue you mentioned: … (* Snip *) …

You can’t use offset inside the Adobe Illustrator context.

May you try to replace:

		set y to the (offset of "<<" in atext) 
		set z to the (offset of ">>" in atext) 

by

tell me
		set y to the (offset of "<<" in atext) 
		set z to the (offset of ">>" in atext) 
end tell

This way the instructions would not be ‘speaking’ to Illustrator.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) lundi 30 décembre 2019 18:52:42

I have not Adobe Illustrator on my machine, so I tested with Microsoft Excel, which has in the given case similar behaviour. You can fool the AppleScript compiler this way:


set aText to "every text frame of document 1 whose contents contains << and contents contains"

set MicrosoftExcelApp to application "Microsoft Excel" -- here is a trick

tell MicrosoftExcelApp
	set y to (offset of "<<" in aText)
	set z to (offset of ">>" in aText)
end tell

return {y, z}

(facepalm) I. Am. An idiot.

oH , i SEE, But can use another ways to find the target contents in this ??

Man, I don’t know what magic way you are looking for. Why don’t you try what I suggested above and tell us if this works or not for you?

In looking at this again, you’re also not getting the text returned from the dialog, which is further tripping you up. Try this as a starting point.

tell application "Adobe Illustrator"'s document 1
	set newJobNumber to (display dialog "Job Number is " with title "JOB Number Define" default answer "JB")'s text returned  --'text returned' must be here or you'll get records
	repeat with aFrame in (get text frames whose contents contains "<<" and contents contains ">>" and contents contains "JOB")
		set aText to contents of aFrame
		set {y, z} to my setoff(aText)
		display dialog ((text 1 thru -y) of aText) & newJobNumber & text (z + 2) thru -1 of aText
	end repeat
end tell

on setoff(aText)
	{offset of "<<" in aText, offset of ">>" in aText}
end setoff