0

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?

3
  • Unable to write - what happens, do you get an error?
    – Luciano
    May 24, 2017 at 12:13
  • no my text(handgloves) opened into new file and illustrator(AH_Portrait.ai) file opened into another file
    – abdul
    May 24, 2017 at 12:17
  • If you want to know more about the site, please read the help center.
    – Luciano
    May 24, 2017 at 12:33

2 Answers 2

2

I suppose you're following the tutorial from https://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.

3
  • i changed my code now like this but the text(handgloves) opened into new file and illustrator opened into another file.. var width = 1280; var height = 700; var doc = app.documents.add(null,width,height); var fileRef = new File("E:/phpbb_project_status/final changes/AH_Portrait.ai"); var docRef = app.open (fileRef) var text1 = doc.textFrames.pointText( [20,height-50] ); text1.contents = "Handgloves";
    – abdul
    May 24, 2017 at 12:27
  • Read your code. Read my answer. You're still using app.documents.add(null,width,height) which will create A NEW document. Read.
    – Luciano
    May 24, 2017 at 12:32
  • ya please change my code and give me exact answer @Luciano
    – abdul
    May 24, 2017 at 12:35
1

You should spend some time on reading what you write:

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

reads:

  1. from application
  2. take the documents container
  3. add a new document
  4. 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:

  1. Find file
  2. that we shall call f
  3. open this file in application as document
  4. 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.

11
  • but illustrator image only opened but the text cannot write over it..this line displays error var text1 = doc.textFrames.pointText( [20,-height+50] );
    – abdul
    May 25, 2017 at 8:01
  • i want to open illustrator image then i neet to write some text over it ..
    – abdul
    May 25, 2017 at 8:02
  • @abdul what version of illustrator are you using? Is it possible that the file does not exist? In anycase we can only show you stuff that works for us. We can not debug for you. Illustrator documents arent images as such.
    – joojaa
    May 25, 2017 at 8:20
  • my illustrator version is cs6 .....actually i want to open a illustrator image then i need to write text over it..my code works fine but my problem is illustrator image and text opening in two documents...
    – abdul
    May 25, 2017 at 9:26
  • @abdul Obviously thats what you told it to do!
    – joojaa
    May 25, 2017 at 10:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.