21 Replies Latest reply on Jun 4, 2008 2:34 AM by Peter Kahrel

    CS3 autoPageNumber returns PB in slug

    John.Kordas Level 1
      I found a post where it was suggested to extend the text box to into the page area to get the autoPageNumber to show the page number. Has anyone been able to get around this?

      Is there a way to get the text to appear at the bottom of the text box?

      Would it also be possible to create a dialog that allows the user to enter the first page number then loop through the number of pages and add them that way?

      Also it's been a while since I've put a script together for ID and noticed that when you use extendscript toolkit 2 you get:

      SpecialCharacters.AUTO_PAGE_NUMBER instead of SpecialCharacters.autoPageNumber which should I use any suggestions?
        • 1. Re: CS3 autoPageNumber returns PB in slug
          Level 1
          re: SpecialCharacters.AUTO_PAGE_NUMBER instead of SpecialCharacters.autoPageNumber which should I use any suggestions?

          They're synonyms. I strongly prefer the second but the OMV delivers the first.

          I wrote this script:
          //DESCRIPTION: Converts Cap form of Enum names to initial caps
          
          if (app.documents.length > 0 &&
                app.selection.length > 0 &&
                  app.selection[0].hasOwnProperty("baseline")) {
            app.selection[0].contents = initialCap(app.selection[0].contents);
          }

          function initialCap(theEnum) {
            var myParts = theEnum.split("_");
            myParts[0] = myParts[0].toLowerCase();
            for (var j = 1; myParts.length > j; j++) {
              myParts[j] = myParts[j].toLowerCase();
              myLetters = myParts[j].split("");
              myLetters[0] = myLetters[0].toUpperCase();
              myParts[j] = myLetters.join("");
            }
            return myParts.join("");
          }
          to convert the one form to the other, but it only works in InDesign, which isn't a lot of use. I wonder if ESTK itself can be scripted to do this -- probably.

          Dave
          • 2. Re: CS3 autoPageNumber returns PB in slug
            John.Kordas Level 1
            Thanks Dave,

            I also prefer the second. I might try to create a for loop to count the pages and apply the number that way but it might be easier to extend the text box to the page. I'm having a problem getting the text frame to align to the bottom of the frame.

            var myLeftFooter=textFrames.add( {geometricBounds:[myPageHeight-5, 10,myPageHeight+10, myPageWidth-10]})
            myLeftFooter.parentStory.insertionPoints.item(0).contents = SpecialCharacters.sectionMarker;
            myLeftFooter.parentStory.insertionPoints.item(0).contents = SpecialCharacters.emSpace;
            myLeftFooter.parentStory.insertionPoints.item(0).contents = SpecialCharacters.AUTO_PAGE_NUMBER;
            myLeftFooter.parentStory.paragraphs.item(0).justification = Justification.leftAlign;
            b myLeftFooter.parentStory.textFramePreferences.item(0).verticalJustification = VerticalJustification.BOTTOM_ALIGN;
            }

            Am I close?
            • 3. Re: CS3 autoPageNumber returns PB in slug
              Peter Kahrel Adobe Community Professional & MVP
              John,

              You need to apply v. justification to the textframe, not the story:

              myLeftFooter.textFramePreferences.item(0).verticalJustification = VerticalJustification.BOTTOM_ALIGN;

              Peter
              • 4. Re: CS3 autoPageNumber returns PB in slug
                Peter Kahrel Adobe Community Professional & MVP
                Dave,

                >I wonder if ESTK itself can be scripted to do this -- probably.

                Strangely, it looks as if you can't. In ESTK 1 you could do this:

                #target estoolkit-1.0
                documents[0].text

                To show in the console the contents of whatever documents[0] contains, or documents[0].selectedText to show whatever is selected in documents[0]. Other properties can be seen in the databrowser, or displayed using document.reflect.properties

                But in ESTK2 all this appears not to work. The databrowser doesn't reveal anything. When you select ESTK and do app.name it duly says "ExtendScript Toolkit", and app.reflect.properties tells you just "name, version, activeScript, reflect". Not a lot to go by.

                By the way, here's an alternative to your initialCap():

                function toJS (s)
                
                    {
                    return s.toLowerCase().replace (/_(.)/g,
                        function() {return arguments[1].toUpperCase()})
                    }


                Peter
                • 5. Re: CS3 autoPageNumber returns PB in slug
                  John.Kordas Level 1
                  In CS3 you have a Numbering & Section Option which allows you to set the number of the first page. For example you might be working on page 44 to 48 of a mag so you can set the first page as 44.

                  Is this available in JS?

                  Using app.activeDocument.pages.length; will give me the number of pages but how do I find what the first page number is?

                  *Edit*
                  After getting onto Dave Saunders blog I found app.activeDocument.pages[0].name will give me 44. Now if I have a text box that has this as it's value and the user wants to change this to 54 how do I apply it back?

                  Also does the user option change according to the log-in on the mac or PC? So if I set the User (found under File/User..) to John and in a JS call app.userName; it will give me John. If some one else log onto my PC would it still come up with John?

                  Cheers.
                  • 6. Re: CS3 autoPageNumber returns PB in slug
                    John.Kordas Level 1
                    I've gotten a little further:

                    The following code if you have a doc open will change the first page number to 44 and so on 45, 46, etc.

                    The problem I'm getting is if change 44 to 46 and try the script a second time I get an add error. Now I'm guessing because the script adds a section the first time you can not add again I should be just changing the value correct?

                    i //start by getting an a alert of the first page no.
                    myStartpage = app.activeDocument.pages[0].name;
                    alert (myStartpage);

                    i //change the page no to 44.
                    var newDocSection = app.activeDocument.sections.add(app.activeDocument.pages[0]);
                    newDocSection.continueNumbering = false;
                    newDocSection.pageNumberStart = 44;

                    i //alert the first page no again it should be 44.
                    var myPage = app.activeDocument.pages[0]
                    alert(myPage.name);

                    If someone could also give me an answer on the user option in the previous post that would be appreciated.
                    • 7. Re: CS3 autoPageNumber returns PB in slug
                      John.Kordas Level 1
                      Don't know if this is the best way around it but I had to remove the section then recreate it to apply a new number.

                      Can anyone tell me if it is possible to convert a string to an integer?

                      This is the process:

                      I have a dialog that reads the first page number and presents a text box to the user.

                      b var myPageNoField = integerEditboxes.add({editContents:app.activeDocument.pages[0].name, minWidth:40});

                      I pass this to my slug function.

                      b if(myResult == true){
                      b var myPageNo = myPageNoField.editContents;
                      b slugDetails(myTitle,myOperator,myPageNo)

                      Then in the slug function I add the page number.

                      b function slugDetails(myTitle,myOperator,myPageNo){
                      b myDocument = app.activeDocument;
                      b myDocument.pages[0].appliedSection.remove();
                      b var newDocSection = myDocument.sections.add(myDocument.pages[0]);
                      b newDocSection.continueNumbering = false;
                      b newDocSection.pageNumberStart = myPageNo;

                      When it gets to the last line where I have myPageNo the error states:

                      b Invalid value for set property 'pageNumberStart'. Expected Long Integer (1-999999), but received "44".

                      Can I make myPageNo read as an integer?
                      • 8. Re: CS3 autoPageNumber returns PB in slug
                        Peter Kahrel Adobe Community Professional & MVP
                        For
                        >pageNumberStart = myPageNo

                        use
                        >pageNumberStart = Number (myPageNo)

                        Peter
                        • 9. Re: CS3 autoPageNumber returns PB in slug
                          John.Kordas Level 1
                          Thanks Peter,

                          Here is what I'm doing so far:

                          if (myDocument.pages[0].appliedSection.marker == "slug"){
                          //alert("yes");
                          myDocument.pages[0].appliedSection.remove();
                          var newDocSection = myDocument.sections.add(myDocument.pages[0]);
                          newDocSection.continueNumbering = false;
                          newDocSection.marker = "slug";
                          newDocSection.pageNumberStart = parseInt(myPageNo);
                          }else{
                          //alert("no");
                          var newDocSection = myDocument.sections.add(myDocument.pages[0]);
                          newDocSection.continueNumbering = false;
                          newDocSection.marker = "slug";
                          newDocSection.pageNumberStart = parseInt(myPageNo);
                          }

                          I don't know why but when you open the Numbering options in CS3 the Start Section is ticked and grayed out. If I run the script above the option is still ticked but no longer grayed out. To get around the problem I was having I check to see if a section marker "slug" exists and if it does delete it then add it again with the new page number.

                          I'm also finding that in the numbering option under Section Prefix Sec1:is being added for some reason. Is this default of the sections.add()?
                          • 10. Re: CS3 autoPageNumber returns PB in slug
                            John.Kordas Level 1
                            Unfortunately removing or adding the section isn't an option as I've found that some docs have their own sections defined.

                            Can anyone tell me if you are able to edit a section?

                            I'm trying to see if I can tell if a section has been applied if it has then just edit, do not add.

                            alert(app.activeDocument.sections[0].sectionPrefix.name); //gives undefined yet in numbering pallet it is set to 0.

                            Any suggestions would be appreciated.
                            • 11. Re: CS3 autoPageNumber returns PB in slug
                              Peter Kahrel Adobe Community Professional & MVP
                              John,

                              Maybe this code answers your last two posts (in #9 and #10):
                              newDocSection = myDocument.pages[0].appliedSection;
                              
                              if (newDocSection.marker != "slug")
                                 var newDocSection = myDocument.sections.add(myDocument.pages[0]);
                              newDocSection.continueNumbering = false;
                              newDocSection.pageNumberStart = parseInt(myPageNo);


                              It is a replacement for your code in #10, and I think it answers your question in #11: by assigning an applied section to a variable, you can check its marker name. If it doesn't exist, create a new section marker; if it does exist, just change it. (The name "newDocSection" is now a bit confusing, but I didn't want to change it.)

                              >the Start Section is ticked and grayed out.

                              This is the case in one-page documents. Seems reasonable: when you create a new document a section is created as well (because each document must have at least one section), and while the document has just one page, you can't add a section (because it already has one). When you add a page, the "Start Section" is no longer grayed out.

                              >I'm also finding that in the numbering option under Section Prefix Sec1:is being added for some reason. Is this default of the sections.add()?

                              It is. But you can disable that in the Pages panel.

                              Peter
                              • 12. Re: CS3 autoPageNumber returns PB in slug
                                John.Kordas Level 1
                                Thanks Peter,

                                That has done the trick.

                                If anyone out there want to test the section details I used he following:

                                alert(app.activeDocument.pages[0].appliedSection.name);
                                alert(app.activeDocument.pages[0].appliedSection.sectionPrefix);
                                alert(app.activeDocument.pages[0].appliedSection.pageNumberStart);

                                I'm also finding that designers here are creating the master pages differently form one designer to the next. Now when you apply the slug you just use:

                                with(myDocument.documentPreferences){
                                //Slug
                                slugBottomOffset = "40";
                                slugTopOffset = "0";
                                slugInsideOrLeftOffset = "0";
                                slugRightOrOutsideOffset = "0";
                                }

                                Would it be possible to add a text frame to all the master templates?

                                The following code only adds the text-frame to the first master page.

                                with(textFrames.add()){
                                geometricBounds = [myPageHeight-5, 10,myPageHeight+22, myPageWidth-10];
                                label = "Title";
                                insertionPoints.everyItem().contents = myDetails;
                                paragraphs.everyItem().justification = Justification.leftAlign;
                                paragraphs.everyItem().fillColor = myDocument.swatches.item("Paper");
                                paragraphs.everyItem().pointSize = 12;
                                textFramePreferences.verticalJustification = VerticalJustification.bottomAlign;
                                }
                                • 13. Re: CS3 autoPageNumber returns PB in slug
                                  Peter Kahrel Adobe Community Professional & MVP
                                  >Would it be possible to add a text frame to all the master templates?

                                  You'd have to cycle through the pages on the master spreads:

                                  mp = app.activeDocument.masterSpreads.everyItem().pages.everyItem().getElements();
                                  for (i = 0; i < mp.length; i++)
                                  //add textframe to mp[i]

                                  Peteer
                                  • 14. Re: CS3 autoPageNumber returns PB in slug
                                    John.Kordas Level 1
                                    Thanks again Peter,

                                    This works well.

                                    I've noticed in a few examples there are a couple of way you can add a text frame.

                                    you can either:

                                    var myTitle = mp[i].textFrames.add(myDocument.layers.item("Slug"), undefined, undefined, {geometricBounds:[myPa..........

                                    or

                                    with(textFrames.add()){
                                    geometricBounds = [myPage........

                                    Is one better than the other?

                                    I went with

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

                                    var myTitle = mp[i].textFrames.add(myDocument.layers.item("Slug"), undefined, undefined, {geometricBounds:[myPageHeight-5, 10,myPageHeight+22, myPageWidth-10], contents:myDetails});
                                    myTitle.label = "Title";
                                    myTitle.parentStory.fillColor ="WhiteBack";
                                    myTitle.textFramePreferences.verticalJustification = VerticalJustification.bottomAlign;
                                    myTitle.paragraphStyles.item("SlugPar", true);

                                    This is working ok until I get to the paragraphStyles then I get object does not support property or method 'paragraphStyles'.

                                    also tried

                                    myTitle.parentStory.paragraphs.item(0).applyStyle(myDocument.paragraphStyles.item("SlugPar ", false));

                                    I gather you can apply a paragraph style to a text frame correct?
                                    • 15. Re: CS3 autoPageNumber returns PB in slug
                                      John.Kordas Level 1
                                      Not sure if this is correct but it seems to work:

                                      myTitle.parentStory.paragraphs[0].appliedCharacterStyle = "SlugChar";
                                      myTitle.parentStory.paragraphs[0].appliedParagraphStyle = "SlugPar";

                                      I'm trying to check if the content in the textframe is more than one line. If it is I make the back ground block slightly higher.

                                      var mycount = app.activeDocument.textFrames.item ("Title");
                                      alert(mycount.lines.length);
                                      if (mycount.lines.length != 1){
                                      boxHeight = myPageHeight+10;
                                      }else{
                                      boxHeight = myPageHeight+15;
                                      }

                                      var myBlueBlock = mp[i].textFrames.add(myDocument.layers.item("Slug"), undefined, undefined, {geometricBounds:[boxHeight, 8,myPageHeight+33, myPageWidth-8]});
                                      myBlueBlock.label = "BlueBlock";

                                      The first loop the alert gives me 1 and on the second loop I get 2. So the left hand page is in the correct position but the right hand page is wrong. I'm guessing the loop is adding the first line it finds in the text frame 1 and adds it to the line in text frame 2 giving me a count of 2 instead of one.

                                      Is there a better way of doing this.
                                      • 16. Re: CS3 autoPageNumber returns PB in slug
                                        John.Kordas Level 1
                                        This seem to fix the count:

                                        var mycount = app.activeDocument.masterSpreads.item(0).pages.item(0).textFrames.item("Title");
                                        • 17. Re: CS3 autoPageNumber returns PB in slug
                                          Peter Kahrel Adobe Community Professional & MVP
                                          The three methods you describe all do the job. This one:
                                          >var myTitle = mp[i].textFrames.add(myDocument.layers.item("Slug"), {geometricBounds:[myPa..........

                                          allegedly is the quickest: you create an object with all the specified properties in place (there's no need to use "undefined" as a place filler, by the way). The two other methods are equivalent. I guess the choice for one or the other is partly a matter of taste. Some people avoid "with" statements as they can cause problems (apparently with referencing but I don't know the details). They are a bit slower than the first method, but realistically, the difference will show only when you create a coupke of hundred frames. You won't see any difference when you place a handful of frames.

                                          Peter
                                          • 18. Re: CS3 autoPageNumber returns PB in slug
                                            Level 1
                                            Creating a complex object with a set-up record rather than using many steps is great when it works, but if it doesn't work, you end up having to deconstruct the statement to find out which bit is going wrong.

                                            On balance, I'd still rather use just the single statement, but there are times when a dumb error just refuses to leap off the page at me until I deconstruct it into a sequence to find out just which part I got wrong.

                                            Dave
                                            • 19. Re: CS3 autoPageNumber returns PB in slug
                                              John.Kordas Level 1
                                              Thanks guys,

                                              At the moment I'm only creating 4 text frames pre master page for the slug process.

                                              Every time I think I've finished, I try another mag and then I find myself solving another problem. The process is working apart from one swatch problem. I decided to create a custom color for the background block. This is the code I'm using:

                                              try{
                                              myDocument.colors.item("BlueBack").name;
                                              }
                                              catch (myError){
                                              var myBlueSwatch = myDocument.colors.add({name:"BlueBack", model:ColorModel.process, colorSpace:ColorSpace.cmyk, colorValue:[100, 60, 0, 0]});
                                              }

                                              It was working fine until I open a doc that must of had the last color created as an RGB. For some reason instead of a blue I'm getting a brown RGB color. Should the "colorSpace:ColorSpace.cmyk" ensure the color is CMYK?

                                              Also I thought it would be worth having a remove script that the user could undo the process if they wanted. To do this I used a dialog that told the user that the layer that holds the slug details and anything else on that layer would be removed. This was straight forward but I can not seem to find any info that tells me how to align my static text. At the moment all the static text is aligned to the right which does not look correct. Is there a way to align static text to the left?

                                              with(dialogColumns.add()){
                                              staticTexts.add({staticLabel:"Please Note:"});
                                              staticTexts.add({staticLabel:"If you remove the slug layer all items on that layer will also be removed."});
                                              staticTexts.add({staticLabel:"Are you sure you want to remove the slug layer and details?"});
                                              }

                                              The other option was to do an alert but you only get a OK no Cancel button is this correct?
                                              • 20. Re: CS3 autoPageNumber returns PB in slug
                                                John.Kordas Level 1
                                                Thanks guys,

                                                At the moment I'm only creating 4 text frames pre master page for the slug process.

                                                Every time I think I've finished, I try another mag and then I find myself solving another problem. The process is working apart from one swatch problem. I decided to create a custom color for the background block. This is the code I'm using:

                                                try{
                                                myDocument.colors.item("BlueBack").name;
                                                }
                                                catch (myError){
                                                var myBlueSwatch = myDocument.colors.add({name:"BlueBack", model:ColorModel.process, colorSpace:ColorSpace.cmyk, colorValue:[100, 60, 0, 0]});
                                                }

                                                It was working fine until I open a doc that must of had the last color created as an RGB. For some reason instead of a blue I'm getting a brown RGB color. Should the "colorSpace:ColorSpace.cmyk" ensure the color is CMYK?

                                                Also I thought it would be worth having a remove script that the user could undo the process if they wanted. To do this I used a dialog that told the user that the layer that holds the slug details and anything else on that layer would be removed. This was straight forward but I can not seem to find any info that tells me how to align my static text. At the moment all the static text is aligned to the right which does not look correct. Is there a way to align static text to the left?

                                                with(dialogColumns.add()){
                                                staticTexts.add({staticLabel:"Please Note:"});
                                                staticTexts.add({staticLabel:"If you remove the slug layer all items on that layer will also be removed."});
                                                staticTexts.add({staticLabel:"Are you sure you want to remove the slug layer and details?"});
                                                }

                                                The other option was to do an alert but you only get a OK no Cancel button is this correct?
                                                • 21. Re: CS3 autoPageNumber returns PB in slug
                                                  Peter Kahrel Adobe Community Professional & MVP
                                                  Static texts in dialogs always align right, which is a shame. I sometimes countered that by using radiobuttons instead of static text. Not ideal, but better than right-aligning text. But in scriptUI, static text can be aligned left, so maybe you could consider that. As to Alert, that indeed gives you just an OK button.

                                                  I don't know about the colour problem.

                                                  Peter