Welcome Dialog

Welcome to the Community!

We have a brand new look! Take a tour with us and explore the latest updates on Adobe Support Community.


Scripting – Get contents from textframe

New Here ,
Nov 05, 2021 Nov 05, 2021

Copy link to clipboard

Copied

Hi all!

 

What I'm trying to achieve is the following: Every page of my document has a text frame outside the document boundaries. This text frame contains the name of the product or design and it has "1" written in the script label. Now, when trying to access the contents of that frame, my code fails me.

 

Currently, it looks like this:

function main() {
    app.scriptPreferences.userInteractionLevel = UserInteractionLevels.INTERACT_WITH_ALL;

var _doc = app.activeDocument;
var _numberOfPages = _doc.pages.count();
var _fileName;

for (var i = 0; i < _numberOfPages; i++) {
    _fileName = _doc.pages[i].textFrames.item("1");
    alert(_fileName.contents);
}
}
main();

 

All I currently want is for the script to alert the contents of all the text frames with the script label value of 1. It doesn't make sense to me that I cannot access the contents of the frames with this method but I'm clearly doing something wrong here.

 

Any help is much appreciated!

TOPICS
Scripting

Views

62

Likes

Translate

Translate

Report

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
Adobe Community Professional ,
Nov 05, 2021 Nov 05, 2021

Copy link to clipboard

Copied

Some points for your help.

  • You mentioned that your textframe lies outside the page boundary then such textframe will not be captured in the page objects textframes collection. So you will have to traverse the textframes collection of either the document or maybe spread object.
  • The item or itemByName methods return object with the specifed name, name is a different property than label. name property is set using the layers panel. If you are really after script label then you will have to use the label property of the object. Now since there is no query method based on label property you will have to check each object of the collection.
  • If you do use the name property then you should check if _fileName is a valid object. Like below

 

if(_fileName.isValid)
	alert(_fileName.contents);

 

 -Manan

Likes

Translate

Translate

Report

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
Adobe Community Professional ,
Nov 05, 2021 Nov 05, 2021

Copy link to clipboard

Copied

Hi nna_top,

script labels refer to the label property of a frame.

item("string") or itemByName("string") refer to the name property.

This is the case for InDesign CS5 version 7 and above.

Before version 7 you could get the label's content with item("string").

 

So you have to do another loop through the frames of every page to find the ones that are labeled with the string "1".

You have ask if textFrames[n].label == "1" and then do the alert.

 

Regards,
Uwe Laubender

( ACP )

Likes

Translate

Translate

Report

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
Adobe Community Professional ,
Nov 05, 2021 Nov 05, 2021

Copy link to clipboard

Copied

I would loop through doc.spreads as Manan suggests. If you have multipage spreads, you'll have to do some work (ie by examining the frame's geometricBounds property) to know which side of the spread it's on. 

Likes

Translate

Translate

Report

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
Adobe Community Professional ,
Nov 05, 2021 Nov 05, 2021

Copy link to clipboard

Copied

LATEST

Every page of my document has a text frame outside the document boundaries.

 

Also, if the text frames are completely on the pasteboard I don’t think your pages loop will get them. You could do a single loop with the code Uwe suggested if you loop thru all page items, something like this:

 

 

 

function main() {
    var api = app.activeDocument.allPageItems
    for (var i = 0; i < api.length; i++){
        if (api[i].label == "1") {
            alert(api[i].contents)
        };
    };   
}

main();

 

 

 

If you select a text frame that is completely on the pasteboard, its parentPage returns null:

 

 

var s = app.documents[0].selection[0];
$.writeln(s.parentPage)
//returns null

 

 

Likes

Translate

Translate

Report

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