Assign Script Label to Odd Pages Trying to Exclude Text Frames in Master page

Engaged ,
Apr 19, 2021 Apr 19, 2021

Copy link to clipboard

Copied

Hi Experts, Im Trying to fix the code so all text frames on the left page will be labeled to "odd" except Text frame of MasterPage like PageNumbers form the master, but my code seems to have problem and need to be fixed, sorry im not so expert in javascript, so i make mistakes and try to learn from them.

//Assign Script Label to Odd Pages Trying to Exclude Text Frames in Master page
var success = AR_testLeftSide ();
alert ("Total frames labeled: " + success);

function AR_testLeftSide () {
var myCounter = 0;
var myDoc = app.activeDocument;
var myPage = myDoc.pages;
var myFrames = myDoc.textFrames;
for (var i=0; i< myFrames.length ; i++) {
var myPage = myFrames[i].parentPage;
if (myPage.side == PageSideOptions.LEFT_HAND) {
myCounter ++;
myFrames[i].label ="odd";
} else {
    //Excluding MasterPage TextFrames Like PageNumbers.. etc
    if(pageitem instanceof TextFrame){
myFrames[i].label ="";
}
}
return myCounter;
}
}

Please can you help me fixing the code?

Thanks in Advance

M.Hasanain

 

Best
M.Hasanain
TOPICS
Scripting

Views

197

Likes

translate

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

correct answers 2 Correct Answers

Adobe Community Professional , Apr 19, 2021 Apr 19, 2021
Hi M.Hasanain, if an item is not positioned on a page, pageItem.parentPage will return not a page object, but value null. Hm. If you want to get all items on document pages only, you could loop the following array of pages:     var allDocPages = app.documents[0].pages.everyItem().getElements();     That would exclude master pages and items on the pasteboards. There are special cases, yes, but I think we do discuss them later if they apply to your sample documents.   Now as a next step you could ...

Likes

translate

Translate

Translate
Adobe Community Professional , Apr 19, 2021 Apr 19, 2021
Alright. Glad it's working for you, but I see some possible issues you could run into. You should loop the allDocPages array and do a loop inside that loop accessing the allPageItems array like that:   var allDocPages = app.documents[0].pages.everyItem().getElements(); // Loop all pages: for( var n=0; n<allDocPages.length; n++ ) { // Test side of page: if( allDocPages[n].side == PageSideOptions.LEFT_HAND ) { // Get all items on the current page: var allPageItemsOfThatPage = allDocPage...

Likes

translate

Translate

Translate
Adobe Community Professional ,
Apr 19, 2021 Apr 19, 2021

Copy link to clipboard

Copied

Hi M.Hasanain,

if an item is not positioned on a page, pageItem.parentPage will return not a page object, but value null.

Hm. If you want to get all items on document pages only, you could loop the following array of pages:

 

 

var allDocPages = app.documents[0].pages.everyItem().getElements();

 

 

That would exclude master pages and items on the pasteboards. There are special cases, yes, but I think we do discuss them later if they apply to your sample documents.

 

Now as a next step you could loop the allPageItems array of every page. Test the first item for parentPage.side and if ok, in your case if the value is PageSideOptions.LEFT_HAND ( Arabic page binding! ) loop that array, check for text frames and assign a value for property label.

 

Why the allPageItems array ?

Because there could be text frames in nested structures like in groups or anchored text frames.

You will not access them if you look for page.textFrames .

 

Regards,
Uwe Laubender

( ACP )

Likes

translate

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
Engaged ,
Apr 19, 2021 Apr 19, 2021

Copy link to clipboard

Copied

Thank you a lot Mr.Uwe Laubender, here is the code after modifications but still label the text frames in master pages :

 

 

 

//Assign Script Label to Odd Pages Excluding Masterpages (Right to Left Document - Arabic and Hebrew)
var success = AR_testLeftSide ();
alert ("Total frames labeled: " + success);

function AR_testLeftSide () {
var myCounter = 0;
var myDoc = app.activeDocument;
//all items on document pages only not master page items
var allDocPages = myDoc.pages.everyItem().getElements();
var myFrames = myDoc.textFrames;
for (var i=0; i< myFrames.length ; i++) {
var allDocPages = myFrames[i].parentPage;
if (allDocPages.side == PageSideOptions.LEFT_HAND) {
myCounter ++;
myFrames[i].label ="odd";
}
}
return myCounter;
}

 

 

 

now its still work on text frames in left hand pages and still affecting text frames in masterpages textframes!

Regards

M.Hasanain

Best
M.Hasanain

Likes

translate

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 ,
Apr 19, 2021 Apr 19, 2021

Copy link to clipboard

Copied

Alright. Glad it's working for you, but I see some possible issues you could run into.

You should loop the allDocPages array and do a loop inside that loop accessing the allPageItems array like that:

 

var allDocPages = app.documents[0].pages.everyItem().getElements();

// Loop all pages:

for( var n=0; n<allDocPages.length; n++ )
{
	// Test side of page:
	if( allDocPages[n].side == PageSideOptions.LEFT_HAND )
	{
	
		// Get all items on the current page:
		var allPageItemsOfThatPage = allDocPages[n].allPageItems;
		
		// Check for text frames and apply label value:
		for( var i = 0; i<allPageItemsOfThatPage.length; i++ )
		{
			if( allPageItemsOfThatPage[i].constructor.name == "TextFrame" )
			{
				allPageItemsOfThatPage[i].label = "odd";
			};
			
		};

	};
	
};

 

Regards,
Uwe Laubender

( ACP )

Likes

translate

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
Engaged ,
Apr 19, 2021 Apr 19, 2021

Copy link to clipboard

Copied

Thank you again, Yes you are right, after testing i discovered the text frames in the left pages still labeled "odd" in master pages, i also tried your new code but nothing happened, no error message and no script labels added.

Regards

M.Hasanain

Best
M.Hasanain

Likes

translate

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 ,
Apr 19, 2021 Apr 19, 2021

Copy link to clipboard

Copied

Strange. It's working on my side.

Could you share a sample document, only a dummy document, for testing this?

Put in on Dropbox or a similar service and share the download link.

 

Thanks,
Uwe Laubender

( ACP )

Likes

translate

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
Engaged ,
Apr 19, 2021 Apr 19, 2021

Copy link to clipboard

Copied

Thank you, 

here is the Documents Link

https://drive.google.com/drive/folders/10nVVy16Qr79thcDD-CI1SzqkIjKyyZCr?usp=sharing 

zip file contain arabic project , the other indd one is english left to right documens

Regards

M.Hasanain

Best
M.Hasanain

Likes

translate

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 ,
Apr 19, 2021 Apr 19, 2021

Copy link to clipboard

Copied

Hi M.Hasanain,

ran my code against your arabic document and it worked as expected.

From my German InDesign 2021 where I selected a text frame after the script run and showing the contents of the script label:

 

TextFrame-Label-Odd-Arabic-1.PNG

 

The label of the other text frame on the even side is untouched:

TextFrame-Label-Odd-Arabic-2.PNG

 

Download the result document from my Dropbox account:

https://www.dropbox.com/s/muwvpk1b97b8yu4/Akhlaq_h_Alquram_End_2P_2021-LABELED.indd?dl=1

 

The other document with that non-arabic page binding needs the test for PageSideOptions.RIGHT_HAND if you are looking for odd pages. Also ran my script looking for PageSideOptions.RIGHT_HAND . Worked as expected.

 

Download the result document from my Dropbox account:

https://www.dropbox.com/s/r43yfx3prle9kpo/Universal%20Declaration%20of%20Human%20Rights_2021-LABELED...

 

 

Regards,
Uwe Laubender

( ACP )

Likes

translate

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 ,
Apr 19, 2021 Apr 19, 2021

Copy link to clipboard

Copied

Hm. What principally could go wrong with looking for PageSideOptions.LEFT_HAND ?

Your document is a non-facing-pages document. However, that's not the case with your two sample documents.

 

I'd suggest you quit InDesign and start anew.

 

Note: The Script Label panel is called Skriptetikett in my German InDesign.

Maybe you have to close the Script Label panel and open it again to see an update of the contents of a label?

 

Regards,
Uwe Laubender

( ACP )

Likes

translate

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
Engaged ,
Apr 19, 2021 Apr 19, 2021

Copy link to clipboard

Copied

Thank you very much, now i figure the problem, i was adding the script with very long name, maybe i exceed the maximum of 255 character so indesign halt, but now everything is working great, Thank you alot Mr.Laubender

 

Best Regards

M.Hasanain

Best
M.Hasanain

Likes

translate

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