Skip to main content
jorgeb61989050
Participant
January 8, 2018
Answered

Writing linked files name into AI file

  • January 8, 2018
  • 4 replies
  • 4038 views

Hello. I'm looking for a little help customizing the script below.

I would like the script to:

  1. Not include the file extension of the linked file in the file name
  2. Position the placed text relative to the top left hand corner of the linked file (not relative to the AI artboard)
  3. My linked file names have spaces in them. I would like the placed text to replace the "%20" with an actual space

//var Sel_itemPlaced = app.activeDocument.placedItems[0]; // if nothing is selected - use the first linked file item 

var sel_itemPlaced = app.activeDocument.selection[0]; // be sure that a linked item (and not an embedded) is selected 

//alert (sel_itemPlaced.file.name); 

var aTF = app.activeDocument.textFrames.add(); 

aTF.position = [50,-50]; 

aTF.contents = sel_itemPlaced.file.name;

Many thanks so far to pixxxel schubser​ who has helped me get this far. This request spawned from Linked files name​.

This topic has been closed for replies.
Correct answer Disposition_Dev

cursory testing shows this to do what you're looking for. but beware. there is no error handling

function test()

{

    //var Sel_itemPlaced = app.activeDocument.placedItems[0]; // if nothing is selected - use the first linked file item   

    var sel_itemPlaced = app.activeDocument.selection[0]; // be sure that a linked item (and not an embedded) is selected   

    var selPos = sel_itemPlaced.position;

    var aTF = app.activeDocument.textFrames.add();

    var fileName = sel_itemPlaced.file.name;

    var textContents = fileName.replace(/\%20/g," "); //change %20 to spaces

    textContents = textContents.replace(/\.[^\.]*$/,""); //remove extension

   

    //set the text frame position to 50pt right of left edge of the placed item

    //and 50pt below the top of the placed item

    aTF.position = [selPos[0] + 50,selPos[1] - 50];

    aTF.contents = textContents; //add the textContents to the textFrame.

    }

test();

4 replies

Participating Frequently
March 23, 2018

How do I save and use this script?

Participating Frequently
March 28, 2018

I know how to install the script, but I do not know how to save it. What do I do with this text? Is there a program? A download available?

function test()

{

    //var Sel_itemPlaced = app.activeDocument.placedItems[0]; // if nothing is selected - use the first linked file item  

    var sel_itemPlaced = app.activeDocument.selection[0]; // be sure that a linked item (and not an embedded) is selected  

    var selPos = sel_itemPlaced.position;

    var aTF = app.activeDocument.textFrames.add();

    var fileName = sel_itemPlaced.file.name;

    var textContents = fileName.replace(/\%20/g," "); //change %20 to spaces

    textContents = textContents.replace(/\.[^\.]*$/,""); //remove extension

  

    //set the text frame position to 50pt right of left edge of the placed item

    //and 50pt below the top of the placed item

    aTF.position = [selPos[0] + 50,selPos[1] - 50];

    aTF.contents = textContents; //add the textContents to the textFrame.

    }

test();

Disposition_Dev
Legend
January 8, 2018

Silly-V with the assist. crushed it.

jorgeb61989050
Participant
January 8, 2018

Absolutely marvelous. The script worked like a charm.

Can you point me to someplace where I can learn to format the placed text? For example, say I wanted it to be in a specific font, font size and color.

Disposition_Dev
Legend
January 8, 2018

check out this link, then find your version of illustrator and open the corresponding Javascript Reference guide.

Illustrator Scripting | Adobe Developer Connection

Do a search in the guide for "characterAttributes". There you'll find all of the available properties regarding font, size, color, etc.

Silly-V
Legend
January 8, 2018

You may find use in the decoreURI(File.name) function, to take care of the encoded URI (there could be other characters than the %20).

Disposition_Dev
Disposition_DevCorrect answer
Legend
January 8, 2018

cursory testing shows this to do what you're looking for. but beware. there is no error handling

function test()

{

    //var Sel_itemPlaced = app.activeDocument.placedItems[0]; // if nothing is selected - use the first linked file item   

    var sel_itemPlaced = app.activeDocument.selection[0]; // be sure that a linked item (and not an embedded) is selected   

    var selPos = sel_itemPlaced.position;

    var aTF = app.activeDocument.textFrames.add();

    var fileName = sel_itemPlaced.file.name;

    var textContents = fileName.replace(/\%20/g," "); //change %20 to spaces

    textContents = textContents.replace(/\.[^\.]*$/,""); //remove extension

   

    //set the text frame position to 50pt right of left edge of the placed item

    //and 50pt below the top of the placed item

    aTF.position = [selPos[0] + 50,selPos[1] - 50];

    aTF.contents = textContents; //add the textContents to the textFrame.

    }

test();