Skip to main content
July 16, 2009
Question

placing image file with placedItem - JS

  • July 16, 2009
  • 1 reply
  • 1453 views

I have a blank CS4 doc. I can place a png image myimage.png into the document no problem using "Place..". I try to place the png image with JS and I get a "Expected file/folder error on line 3" . The script, document and image are all in the same directory.

var myDoc = app.activeDocument;
var mysym = myDoc.placedItems.add();
mysym.file = 'myimage.png';
mysym.position = [200,200];

I try using an absolute path to the image and it still gives me the error message. I tried to place myimage.eps also, with the same problem. Am I missing something obvious with the placeItem operation. Ultimately, I want the image to be embedded and not linked.

Thanks,

Mark

This topic has been closed for replies.

1 reply

July 16, 2009

Ok, I need a file handle.

var myDoc = app.activeDocument;

var myfile = File('myimage.png');
var mysym = myDoc.placedItems.add();
mysym.file = myfile;
mysym.position = [200,200];

This still produces a error in locating the file.

So I do:

alert(myfile);

And I find out that AI is looking in c:\program files\blah blah blah\ (6 folders deep)

So I need to do this: var myfile = File('../../../../../temp/myimage.png');

It works. But there must be a way to tell AI where to look for files? Preferably a solution that works on Mac and Windows.

Thanks,

Mark

Larry G. Schneider
Community Expert
Community Expert
July 16, 2009

Using the Place command, you are browsing for the file. Use the same technique in the .jsx