Mobile app version of vmapp.org
Login or Join
Samaraweera207

: How to insert text into an Illustrator file using ExtendScript JavaScript I opened my Illustrator file successfully using the below code: var fileRef = new File("E:/project/AH_Portrait.ai"); var docRef

@Samaraweera207

Posted in: #AdobeIllustrator #IllustratorScripting #Javascript

I opened my Illustrator file successfully using the below code:

var fileRef = new File("E:/project/AH_Portrait.ai");
var docRef = app.open (fileRef)


I want to write some text ("Handgloves") into it, but I'm unable to do so using the following code:

var width = 100;
var height = 100;
var doc = app.documents.add(null,width,height);
var text1 = doc.textFrames.pointText( [20,height-50] );
text1.contents = "Handgloves";


How can I write text in to my document?

10.02% popularity Vote Up Vote Down


Login to follow query

More posts by @Samaraweera207

2 Comments

Sorted by latest first Latest Oldest Best

 

@Yeniel278

You should spend some time on reading what you write:

var doc = app.documents.add(null,width,height);


reads:


from application
take the documents container
add a new document
that we shall call doc from now on


But that's not actually what you want to do you want to:

var f = new File("E:/project/AH_Portrait.ai");
var doc = app.open(f);


reads:


Find file
that we shall call f
open this file in application as document
let use call this document doc from now on


Ok, then the rest of the code works fine. However, there is a few caveats. The handle app is not universally defined to mean Illustrator! So usually you would put the line
#target illustrator


on the first line of your script. Second illustrator height coordinates are positive upwards. So [20,height-50] is actually above your image on most illustrator versions (yeah the 0, 0 used to be in lower left corner back in the day). You should probably do [20,-height+50] instead, but I'm not sure (possibly [20, -50]).

Now then your script becomes:
#target illustrator

var height = 100;

var f = new File("E:/project/AH_Portrait.ai");
var doc = app.open (f)

var text1 = doc.textFrames.pointText( [20,-height+50] );
text1.contents = "Handgloves";


Off course this does not save your result.

10% popularity Vote Up Vote Down


 

@BetL875

I suppose you're following the tutorial from github.com/jtnimoy/scripting-for-illustrator-tutorial
You need to define the document you want to write your text into. Instead, your code is creating a new document and writing the text on that.

You should use

var doc = app.activeDocument; instead of

var doc = app.documents.add(null,width,height);

if you want to write to the currently open document.

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme