Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Script for placed text on linked item position

New Here ,
Mar 28, 2018 Mar 28, 2018

I now have this script installed and working. Is there a way to set the text directly below the placed item, rather than by pts? I have many objects sized differently, and would just like the text to sit below the placed item every time.

Thanks all for the assistance in this previous thread: https://forums.adobe.com/message/10275599#10275599

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();

TOPICS
Scripting
2.6K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Mentor , Mar 28, 2018 Mar 28, 2018

try this:

function test()

{

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

    var myItemBounds = myItem.visibleBounds;

    var myItemHeight = Math.abs(myItemBounds[3] - myItemBounds[1]);

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

    var fileName = myItem.file.name;

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

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

    aTF.pos

...
Translate
Adobe
Mentor ,
Mar 28, 2018 Mar 28, 2018

try this:

function test()

{

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

    var myItemBounds = myItem.visibleBounds;

    var myItemHeight = Math.abs(myItemBounds[3] - myItemBounds[1]);

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

    var fileName = myItem.file.name;

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

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

    aTF.position = [myItemBounds[0], myItemBounds[1] - myItemHeight];

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

}

test();

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Mar 28, 2018 Mar 28, 2018

thank you!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 28, 2018 Mar 28, 2018

williamadowling  schrieb

try this:

function test() {     var myItem = app.activeDocument.selection[0]; // be sure that a linked item (and not an embedded) is selected      var myItemBounds = myItem.visibleBounds;     var myItemHeight = Math.abs(myItemBounds[3] - myItemBounds[1]);     var aTF = app.activeDocument.textFrames.add();     var fileName = myItem.file.name;     var textContents = fileName.replace(/\%20/g, " "); //change %20 to spaces     textContents = textContents.replace(/\.[^\.]*$/, ""); //remove extension     aTF.position = [myItemBounds[0], myItemBounds[1] - myItemHeight];     aTF.contents = textContents; //add the textContents to the textFrame. } test();

Hi williamadowling

not bad. And yes, there are dozens of variants possible - but why so complicated?

And it is not needed to escape the dot in the class in your regex.

test ();

function test() {

    var aDoc = app.activeDocument;

    var myItem = aDoc.selection[0];

    var myItemBounds = myItem.visibleBounds;

    var aTF = aDoc.textFrames.add();

    var textContents = decodeURI(myItem.file.name).replace(/\.[^.]+$/, "");

    aTF.position = [myItemBounds[0], myItemBounds[3]];

    aTF.contents = textContents;

}

and maybe a little error management could be useful here

var aDoc = app.activeDocument;

if (aDoc.selection.length == 1) {

    var myItem = aDoc.selection[0];

    if (myItem.typename == "PlacedItem") { test ();

    } else { alert ("wrong selection"); }

} else {alert ("no selection"); }

function test() {

    var myItemBounds = myItem.visibleBounds;

    var aTF = aDoc.textFrames.add();

    var textContents = decodeURI(myItem.file.name).replace(/\.[^.]+$/, "");

    aTF.position = [myItemBounds[0], myItemBounds[3]];

    aTF.contents = textContents;

}

Have fun

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Mar 28, 2018 Mar 28, 2018

Hey this is great but does not allow for multiple selections?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 28, 2018 Mar 28, 2018

Hi ,

simply change the 'query for selection length' and add a loop eg like this

var aDoc = app.activeDocument;

if (aDoc.selection.length > 0) {

    var aSel = aDoc.selection;

    for (i=0; i<aSel.length; i++) {

    if (aSel.typename == "PlacedItem") { test (aSel);

        } else { alert ("wrong selection"); } }

} else {alert ("no selection"); }

function test(myItem) {

    var myItemBounds = myItem.visibleBounds;

    var aTF = aDoc.textFrames.add();

    var textContents = decodeURI(myItem.file.name).replace(/\.[^.]+$/, "");

    aTF.position = [myItemBounds[0], myItemBounds[3]];

    aTF.contents = textContents;

}

Have fun

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Mar 28, 2018 Mar 28, 2018

You sir are legend lol

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Mentor ,
Mar 28, 2018 Mar 28, 2018

For additional explanation and clarity for the sake of posterity, here's some more in depth info.

the placement of a given object can be handled in a couple different ways.

You can either input two values into the object's 'position' array, as shown above. The format of the position array is as follows:

myObject.position = [x coordinate, y coordinate]; //these values are in points and are measured from the document origin.

The other way you can position objects is to explicitly set their "left" and "top" properties. Functionally this is the same as the above, but if you only needed to set one coordinate instead of both, you can do so without having to input both values.

myObject.left = x coordinate; //value in points measured from the document origin

myObject.top = y coordinate; //value in points measured from the document origin

in both of the examples above "x coordinate" and "y coordinate" should either be integers or floats.

hope this was helpful

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Mar 29, 2018 Mar 29, 2018

I know I'd requested align left, but what about centered? Thanks in advance

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Mentor ,
Mar 29, 2018 Mar 29, 2018

So you just need to calculate the center point of the target object. Then set the "left" property of your text frame to that center point minus half the width of the textFrame. let's have a look:

//the horizontal centerpoint is the left edge of the object, plus half of the width

var centerPointOfTargetObject = myItem.left + myItem.width/2;

//now, we explicitly set the left coordinate of the textFrame to the centerpoint of the targetObject minus half the width of the textFrame

aTF.left = centerPointOfTargetObject - aTF.width/2

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 31, 2018 Mar 31, 2018
LATEST

Hi

A good explanation. But any beginner will fail with it! Because of:

var aDoc = app.activeDocument;

var aTF = aDoc.textFrames.add();

alert (aTF.width);

It is imperative at first to fill the text frame with contents and only afterwards to calculate the midpoint.

Hi

replace in the script snippet in my post #6 Re: Script for placed text on linked item position

line #13 and #14

aTF.position = [myItemBounds[0], myItemBounds[3]];

aTF.contents = textContents;

with:

aTF.contents = textContents;

aTF.position = [myItemBounds[0] + myItem.width/2 - aTF.width/2, myItemBounds[3]];

That should help (there are other options as well).

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines