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

JS Check if a page has section prefix set

Explorer ,
Jun 29, 2010 Jun 29, 2010

Some layouts are created in sections for example a document may have the following layout sequence 21, 23, 24,25.  I have a process that allows the user to send off pages we need but I hit a bump and need a way to tell if a page has a section prefix.

If I try:

app.activeDocument.pages.itemByName("23").appliedSection.name

I get Object is invalid, but if I try

app.activeDocument.pages.itemByName("Sec1:23").appliedSection.name

I get Sec1: in the console.

So how can I test if there is a prefix?


I realize this will work but I need to check the page by the numbers entered by the user so

app.activeDocument.pages.item(1).appliedSection.name

would return Sec1: but the user would enter 23 as the page option.

Cheers John.

TOPICS
Scripting
2.1K
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

Valorous Hero , Jul 01, 2010 Jul 01, 2010

John, I just forgot to copy-paste a couple of lines at top of the script. From my memory, It should be something like this:

var doc = app.activeDocument:

var pages = doc.pages;

I tested it yesterday and it worked for me -- returned the selected pages in a document with section prefixes.

I guess that you use in you document prefixes in the following form: "Sec" + one or more digit + colon.

The script loops thru the selected pages (unrangedPages array) and searches a regular expression in the name of e

...
Translate
Explorer ,
Jun 29, 2010 Jun 29, 2010

I must be missing something.

If I use either of the options below I get 23 returning in my console.

app.activeDocument.pages.item("Sec1:23").name


app.activeDocument.pages.itemByName("Sec1:23").name

So if the name is 23 shouldn't app.activeDocument.pages.itemByName("23").name work?

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
LEGEND ,
Jun 29, 2010 Jun 29, 2010

page.appliedSection.sectionPrefix

However, unless you specifically need the name, I would generally use the Page.documentOffset to identify a page.

Harbs

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
Explorer ,
Jun 29, 2010 Jun 29, 2010

Thanks Harbs,

The process I have allows the user to enter the page numbers they wish to send off. So for the example here a user may type in the dialog 21-23.  Now the process I'm using to determine the pages is

mySplitResult would be 21,22,23.

for(var myCounter = 0; myCounter < mySplitResult.length; myCounter++){


try{
     var  myPageName = myDocument.pages.item(mySplitResult[myCounter]).name;
    }
catch(e){

When the script runs page 21 its fine and works, 22 is fine as there is no page 22 and its captured, but 23 is there and should work but because it has a section prefix it needs to be myDocument.pages.item("Sec1:"+mySplitResult[myCounter]).name; to work.

Is there a way around this?

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
Valorous Hero ,
Jun 30, 2010 Jun 30, 2010

Hi John,

Here is my idea how to solve this:

var pageRange = "21-23, 25, 28"; // user enters page numbers here the same way as in UI
pageRange = pageRange.replace(/\s+/g, "");
var unrangedPages = Unrange(pageRange).split(",");
var selectedPages = [];


for (var i = 0; i < unrangedPages.length; i++) {
     var pageNumber = unrangedPages;
     for (var p = 0; p < pages.length; p++) {
          var thePage = pages

;
          var pageName = thePage.name;
          if (pageName.search("Sec\\d+:" + pageNumber) != -1) {
               selectedPages.push(thePage);
          }
     }
}

for (var j = 0; j < selectedPages.length; j++) {
     var currentPage = selectedPages;
     // Do something here
     $.writeln(currentPage.name);
}

function Unrange(s) {
     return s.replace (/(\d+)-(\d+)/g, Expand);
}

function Expand() {
     var expanded = [];
     var start = Number(arguments[1]);
     var stop = Number(arguments[2]);
     for (var i = start; i <= stop; i++) {
          expanded.push(i);
     }
     return expanded;
}

Kasyan

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
Explorer ,
Jun 30, 2010 Jun 30, 2010

Thanks Kasyan,

I had to change

for (var p = 0; p < pages.length; p++) {
          var thePage = pages

;

to

for (var p = 0; p < app.activeDocument.pages.length; p++) {
          var thePage = app.activeDocument.pages

;

hope this was correct. I did not seem to get anything back in the console unless I changed

if (pageName.search("Sec\\d+:" + pageNumber) != -1) {

to

if (pageName.search("Sec\\d+:" + pageNumber) == -1) {

This would return

21
23
24
25
21
23
24
25
21
23
24
25
21
23
24
25
21
23
24
25
undefined

I can see that you are expanding the pages entered by the user and then doing a search to see if  a Sec prefix has been applied. Is this correct? If that's correct then could you search for just the number and if it is there it doesn't matter what the prefix is as the user may enter any name they wish for the prefix. If I'm understanding this correctly I like the method.

Cheers John.

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
Explorer ,
Jun 30, 2010 Jun 30, 2010

This seems to be working for me:

var pageIsArray = [];
var docArray = [];
var yesVals = [];
var noVals = [];


for(var loopPages = 0; loopPages < myDocument.pages.length; loopPages++){
     docArray.push(myDocument.pages[loopPages].name);
     }
                        
for(var myCounter = 0; myCounter < mySplitResult.length; myCounter++){
     if (docArray.toString().indexOf(mySplitResult[myCounter].toString()) != -1) {
          yesVals.push(mySplitResult[myCounter]);
     }else{
          noVals.push(mySplitResult[myCounter]);
          }
     }
                         
$.write("Doc "+docArray+"\n")         
$.write("page "+pageIsArray+"\n")
$.write("Yes "+yesVals+"\n")
$.write("No "+noVals+"\n")
$.write("Split "+mySplitResult+"\n")

Upon further testing I found that once I hit pg23 it starts giving me all the pages not just pg 23 and 24 and 25.  Back to the drawing board.

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
Valorous Hero ,
Jul 01, 2010 Jul 01, 2010

John, I just forgot to copy-paste a couple of lines at top of the script. From my memory, It should be something like this:

var doc = app.activeDocument:

var pages = doc.pages;

I tested it yesterday and it worked for me -- returned the selected pages in a document with section prefixes.

I guess that you use in you document prefixes in the following form: "Sec" + one or more digit + colon.

The script loops thru the selected pages (unrangedPages array) and searches a regular expression in the name of each   page -- if it's found, the page is added to the selectedPages array.

I suppose that you need to keep the section prefixes -- if not, you can remove them by script like so:

aDoc = app.activeDocument;
aDoc.sections.everyItem().sectionPrefix = "";
aDoc.sections.everyItem().includeSectionPrefix = false;

Regards,

Kasyan

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
Explorer ,
Jul 01, 2010 Jul 01, 2010

Thanks Kasyan,

I think removing the prefix might be the best way to go.  I have a feeling this might be a bug because if I run the following code for page 23:

app.activeDocument.pages.item(1).name

the console returns 23. So I then turn on the "Include prefix when numbering page" option in the numbering & section options panel and run the code again. This time I get Sec1:23.  If I run your code it now returns Sec1:23 and Sec1:25.

The strange part is if I turn off the "Include prefix when numbering page" option and try the following code:

app.activeDocument.pages.itemByName ("23").name

I get Object is Invalid. If I run

app.activeDocument.pages.itemByName ("Sec1:23").name


It returns 23. Turn the option back on and then run the script again I get Sec1:23.

So is this a bug should app.activeDocument.pages.itemByName ("23").name return 23 I've tried both CS3 and CS5 and get the same result. It does not make sense to me. So I think removing the prefix might be the only option.

Cheers, John

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
Valorous Hero ,
Jul 03, 2010 Jul 03, 2010
LATEST

Yes, it's really weird to me that you always have to include section prefix when you are referencing page using itemByName() or item() — despite the fact that data browser shows name without prefix when "Include prefix when numbering page" is off.

Kasyan

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