5 Replies Latest reply on Jan 30, 2011 3:18 PM by mckayk_777-PN6kWE

    Select object with script after creation

    mckayk_777-PN6kWE Level 2

      Hopefully a nice easy one for someone. Working in Indesign CS4 Mac OSX using JSX

      I am trying to set up a script that will Make 4 layers, create a border, select that border and add crop marks around it.

       

      I have this part already...

       

      I have a border on my page and need the script to just select the box on the page it will be the only graphic on the page.

      A select all would do it but prefer the script to do it cause it will then continue on with another script to put crop marks around the page border ie:

       

      cropmarks.applescript

       

      Also at the end of a script is it possible to make it call another script to run straight after the one just run.

        • 1. Re: Select object with script after creation
          Kasyan Servetsky Level 5

          You can add a label to your border:

          myBorder.label = "border";

           

          Later you can select it like so:

          app.select( app.activeDocument.pageItems.item("border") );

           

          You can run an AS from JS like this:

          app.doScript( new File("/pathToYourScript/cropmarks.applescript"), ScriptLanguage.applescriptLanguage);
          
          • 2. Re: Select object with script after creation
            Harbs. Level 6

            Kasyan Servetsky wrote:


            Later you can select it like so:

            app.select( app.activeDocument.pageItems.item("border") );

            Just be forewarned that this code will not work in CS5 or later...

             

            In CS5 you'd need to loop through all the pageItems until you find the object with your label.

             

            Harbs

            • 3. Re: Select object with script after creation
              mckayk_777-PN6kWE Level 2

              Thanks for trying to help... but I must admit that I know less about  programming then politicians know about running a country.

              (imagine that  failed lawyers change over to politics and they get to run the country  could only happen in a demonocracy)

               

              This is the first script: It is called "1 Make Layers Page Border.jsx" it needs to select the border it makes and then call the next script.

               

              I will put that script into another post to save confusion

               

               

              #target indesign

               

              app.activeDocument.layers.add ({name: 'Guides', layerColor: UIColors.blue});
              app.activeDocument.layers.add ({name: 'CropMarksLayer', layerColor: UIColors.green});
              app.activeDocument.layers.add ({name: 'ColourBars', layerColor: UIColors.red});
              app.activeDocument.layers.item('Layer 1').remove();

               

              #targetengine 'pageborder125'

               

              var    scriptName = "CropMarksLayer",
                  scriptVersion = "1.25",
                  layerName = scriptName;

               

              var    alignStrings = ['Inside','Outside','Center'],
                  ptBorder = ptBorder||.1,                        // default border weight (pts)
                  pgMode = pgMode||1,                                // -1=active page | 1=all pages
                  align = align||0,                                // default alignment index
                  jDots = jDots||0,                                // Japanese dots flag,
                  jDotsStyleName = false,
                  solidStyleName = false;

               


              var createBorder = function(/*Layer*/layer)
              //------------------------------------------------
              // this: Page [collective allowed]
              {
                  var pages = this.getElements(),
                      alignMode = StrokeAlignment[alignStrings[align].toLowerCase()+'Alignment'],
                      pg;
                 
                  var recProps = {
                      fillColor: 'None',
                      strokeColor: 'Black',
                      strokeTint: 100,
                      strokeWeight: ptBorder,
                      strokeAlignment: alignMode,
                      strokeType: (jDots && jDotsStyleName) || solidStyleName,
                      };

               

                  while( pg=pages.pop() )
                      {
                      recProps.geometricBounds = pg.bounds;
                      pg.rectangles.add(layer,undefined,undefined,recProps);
                      }
              };

               

              var pageBorderMain = function()
              //------------------------------------------------
              {
                  var doc = app.documents.length&&app.activeDocument;
                 
                  if( !doc ) throw Error("Please open a document before running " + scriptName + ".");
                 
                  var vwPrefs = doc.viewPreferences,
                      strokeUnits = ('strokeMeasurementUnits' in vwPrefs)?
                          vwPrefs.strokeMeasurementUnits:
                          MeasurementUnits.points;

               

                  jDotsStyleName = (function()
                      {
                      try{return doc.strokeStyles.itemByName("$ID/Japanese Dots").name;}
                      catch(_){}
                      return false;
                      })();

               

                  solidStyleName = (function()
                      {
                      try{return doc.strokeStyles.itemByName("$ID/Solid").name;}
                      catch(_){}
                      return false;
                      })();

               

                  if( !solidStyleName ) throw Error("Unable to find the 'Solid' stroke style in InDesign!");

               

                 
                  var canRemove = (function()
                      {
                      var r = false;
                      try{r=!!doc.layers.itemByName(layerName).id;}
                      catch(_){}
                      return r;
                      })();
                 
                  var dlgRet = (function()
                      {
                      var dlgTitle = ' ' + scriptName + ' ' + scriptVersion + "  |  \u00A9Indiscripts.com",
                          d = app.dialogs.add({name:dlgTitle, canCancel:true}),
                         
                          pn = d.dialogColumns.add().borderPanels.add(),
                          dc = pn.dialogColumns.add(),
                          dr = dc.dialogRows.add(),
                         
                          // Weight
                          sWeight = dr.dialogColumns.add().
                              staticTexts.add({
                              staticLabel: "Weight:",
                              minWidth: 80,
                              }),
                          meWeight = dr.dialogColumns.add().
                              measurementEditboxes.add({
                              editValue: ptBorder,
                              editUnits: strokeUnits,
                              minimumValue: .1,
                              maximumValue: 5,
                              smallNudge: .25,
                              largeNudge: .1,
                              }),
                         
                          // Alignment
                          sAlign = (dr=dc.dialogRows.add()).dialogColumns.add().
                              staticTexts.add({
                              staticLabel: "Alignment:",
                              minWidth: 80,
                              }),
                          ddAlign = dr.dialogColumns.add().
                              dropdowns.add({
                              stringList: alignStrings,
                              selectedIndex: align,
                              }),
                         
                          // All Pages flag
                          cbAllPages = (dc=pn.dialogColumns.add()).dialogRows.add().dialogColumns.add().
                              checkboxControls.add({
                              staticLabel: "All Pages",
                              checkedState: pgMode==1,
                              }),
                             
                          // Dots flag
                          cbDots = jDotsStyleName?
                              (dr=dc.dialogRows.add()).dialogColumns.add().
                              checkboxControls.add({
                              staticLabel: "Dotted Stroke",
                              checkedState: !!jDots,
                              }):
                              {checkedState:false},
                         
                          // Remove
                          cbRemove = canRemove?
                              d.dialogColumns.add().
                              checkboxControls.add({
                              staticLabel: "Remove the border",
                              checkedState: false,
                              }):
                              {checkedState:false};

               

                     
                      var ret = d.show()&&{
                          ptBorder: meWeight.editValue,
                          align: ddAlign.selectedIndex,
                          pgMode: (cbAllPages.checkedState)?1:-1,
                          jDots: !!cbDots.checkedState,
                          removeBorder: cbRemove.checkedState,
                          }
                      d.destroy();
                      return ret;
                      })();
                 
                  if( !dlgRet ) return false;
                  ptBorder = dlgRet.ptBorder;
                  pgMode = dlgRet.pgMode;
                  align = dlgRet.align;
                  jDots = dlgRet.jDots;
                 
                  // [fix100914]
                  var activeLayer = (function()
                      {
                      var al = doc.activeLayer;
                      return ( al.name == layerName )?
                          (doc.layers.length==1&&doc.layers.add()):
                          al.getElements()[0];
                      })();
                  // [/fix100914]
                 
                 
                  var removeBorder = dlgRet.removeBorder;
                  var borderLayer = (function()
                      {
                      var layers = doc.layers;
                      try{layers.itemByName(layerName).remove();}catch(_){};
                      return ( removeBorder ) ? null :
                          layers.add({name: layerName, printable: true}).
                          move(LocationOptions.atBeginning); // [fix100916]
                      })();
                  if( removeBorder ) return;
                 
                  // [fix100914]
                  var ro = vwPrefs.rulerOrigin;
                  vwPrefs.rulerOrigin = RulerOrigin.spreadOrigin;
                  // [/fix100914]

               

                  // Main process
                  createBorder.call(
                      (pgMode==1)?doc.pages.everyItem():app.activeWindow.activePage,
                      borderLayer
                      );
                 
                  borderLayer.locked = false;

               

                  // [fix100914]
                  if( activeLayer ) doc.activeLayer = activeLayer;
                  vwPrefs.rulerOrigin = ro;
                  // [/fix100914]
              };

               

              app.scriptPreferences.enableRedraw = false;
              try    {pageBorderMain();}catch(_){alert(_);}
              app.scriptPreferences.enableRedraw = true;
              app.activeDocument.layers.add ({name: 'Artwork', layerColor: UIColors.lightBlue});

              • 4. Re: Select object with script after creation
                mckayk_777-PN6kWE Level 2

                This script is called: "2 AddGuides.applescript"

                 

                Then at the end of this script it needs to call one more script called "3 CropMarks.applescript"

                These actions I do for every new job that comes into work which can be many times a day.

                 

                --AddGuides.applescript
                --An InDesign CS4 AppleScript
                --
                --Adds guides around the selected object(s).
                --
                --For more on InDesign/InCopy scripting, go to
                --http://www.adobe.com/products/indesign/scripting/index.html
                --or visit the InDesign Scripting forum at http://www.adobeforums.com
                --
                tell application "Adobe InDesign CS4"
                    --Set the user interaction level to allow InDesign to display dialog boxes and alerts.
                    set user interaction level of script preferences to interact with all
                    set myObjectTypeList to {rectangle, oval, polygon, graphic line, text frame}
                    if (count documents) is not equal to 0 then
                        set mySelection to selection
                        if (count mySelection) is not equal to 0 then
                            set myClass to class of item 1 of mySelection
                            if myClass is in myObjectTypeList then
                                my myDisplayDialog()
                            else
                                display dialog "Please select a rectangle, oval, polygon, graphic line, or text frame, and try again."
                            end if
                        else
                            display dialog "No objects are selected. Please select an object and try again."
                        end if
                    else
                        display dialog "No documents are open. Please open a document, select an object, and try again."
                    end if
                end tell
                --handler for displaying the AddGuides dialog box
                on myDisplayDialog()
                    tell application "Adobe InDesign CS4"
                        set myDialog to make dialog with properties {name:"AddGuides"}
                        tell myDialog
                            tell (make dialog column)
                                tell (make border panel)
                                    tell (make dialog column)
                                        make static text with properties {static label:"Add Guides At:"}
                                    end tell
                                    tell (make dialog column)
                                        set myTopCheckbox to make checkbox control with properties {static label:"Top", checked state:true}
                                        set myLeftCheckbox to make checkbox control with properties {static label:"Left", checked state:true}
                                        set myBottomCheckbox to make checkbox control with properties {static label:"Bottom", checked state:true}
                                        set myRightCheckbox to make checkbox control with properties {static label:"Right", checked state:true}
                                        set myXCenterCheckbox to make checkbox control with properties {static label:"Horizontal Center"}
                                        set myYCenterCheckbox to make checkbox control with properties {static label:"Vertical Center"}
                                        set myXPointCheckbox to make checkbox control with properties {static label:"Path Point Horizontal Anchor"}
                                        set myYPointCheckbox to make checkbox control with properties {static label:"Path Point Horizontal Anchor"}
                                    end tell
                                end tell
                                tell (make border panel)
                                    tell (make dialog column)
                                        make static text with properties {static label:"Add Guides Around:"}
                                    end tell
                                    tell (make dialog column)
                                        set myRangeButtons to make radiobutton group
                                        tell myRangeButtons
                                            make radiobutton control with properties {static label:"Each Object"}
                                            make radiobutton control with properties {static label:"Entire Selection", checked state:true}
                                        end tell
                                    end tell
                                end tell
                                tell (make border panel)
                                    tell (make dialog column)
                                        make static text with properties {static label:"Guides Based On:"}
                                    end tell
                                    tell (make dialog column)
                                        set myBasedOnButtons to make radiobutton group
                                        tell myBasedOnButtons
                                            make radiobutton control with properties {static label:"Geometric Bounds", checked state:true}
                                            make radiobutton control with properties {static label:"Visible Bounds"}
                                        end tell
                                    end tell
                                end tell
                            end tell
                        end tell
                        set myResult to show myDialog
                        if myResult is true then
                            --Get the control settings from the dialog box.
                            set myTopAlign to checked state of myTopCheckbox
                            set myLeftAlign to checked state of myLeftCheckbox
                            set myBottomAlign to checked state of myBottomCheckbox
                            set myRightAlign to checked state of myRightCheckbox
                            set myXCenterAlign to checked state of myXCenterCheckbox
                            set myYCenterAlign to checked state of myYCenterCheckbox
                            set myXPoint to checked state of myXPointCheckbox
                            set myYPoint to checked state of myYPointCheckbox
                            set myRange to selected button of myRangeButtons
                            set myBasedOn to selected button of myBasedOnButtons
                            destroy myDialog
                            if (myTopAlign or myLeftAlign or myBottomAlign or myRightAlign or myXCenterAlign or myYCenterAlign or myXPoint or myYPoint) then
                                my myAddGuides(myTopAlign, myLeftAlign, myBottomAlign, myRightAlign, myXCenterAlign, myYCenterAlign, myRange, myBasedOn, myXPoint, myYPoint)
                            else
                                display dialog "No guides were selected."
                            end if
                        else
                            destroy myDialog
                        end if
                    end tell
                end myDisplayDialog
                on myAddGuides(myTopAlign, myLeftAlign, myBottomAlign, myRightAlign, myXCenterAlign, myYCenterAlign, myRange, myBasedOn, myXPoint, myYPoint)
                    tell application "Adobe InDesign CS4"
                        set myDocument to active document
                        set mySelection to selection
                        set myGuidesLayerName to "Guides"
                        try
                            --Get the layer on which to place the guides.
                            set myLayer to layer myGuidesLayerName of myDocument
                        on error
                            --Create the layer if it didn't already exist.
                            tell myDocument
                                set myLayer to make layer with properties {name:myGuidesLayerName}
                            end tell
                        end try
                        --Save the current ruler origin setting.
                        set myOldRulerOrigin to ruler origin of view preferences of myDocument
                        --Set the ruler origin to spread origin. If we don't do this,
                        --guides will be positioned incorrectly when the selection
                        --contains objects on multiple pages.
                        set ruler origin of view preferences of myDocument to spread origin
                        --Get a reference to the current spread.
                        set mySpread to active spread of active window
                        --If myRange is 0, draw crop marks around each item in the selection;
                        --if it's 1, draw crop marks around the bounding box of the selection.
                        if myRange is 0 then
                            repeat with myCounter from 1 to (count mySelection)
                                set myPageItem to item myCounter of mySelection
                                if myXPoint is true or myYPoint is true then
                                    my myDrawGuidesAtPoints(myPageItem, myXPoint, myYPoint, myLayer, mySpread)
                                end if
                                --Get the bounds of the item (using geometric bounds if myBasedOn is 0, or visible bounds if it's 1)
                                if myBasedOn = 0 then
                                    set myBounds to geometric bounds of myPageItem
                                else
                                    set myBounds to visible bounds of myPageItem
                                end if
                                set myY1 to (item 1 of myBounds)
                                set myX1 to (item 2 of myBounds)
                                set myY2 to (item 3 of myBounds)
                                set myX2 to (item 4 of myBounds)
                                --Calculate the horizontal center and vertical center of the object.
                                set myXCenter to myX1 + ((myX2 - myX1) / 2)
                                set myYCenter to myY1 + ((myY2 - myY1) / 2)
                                --Draw guides at the specified locations.
                                if myTopAlign is true then
                                    my myDrawGuide(myY1, horizontal, myLayer, mySpread)
                                end if
                                if myLeftAlign is true then
                                    my myDrawGuide(myX1, vertical, myLayer, mySpread)
                                end if
                                if myBottomAlign is true then
                                    my myDrawGuide(myY2, horizontal, myLayer, mySpread)
                                end if
                                if myRightAlign is true then
                                    my myDrawGuide(myX2, vertical, myLayer, mySpread)
                                end if
                                if myXCenterAlign is true then
                                    my myDrawGuide(myXCenter, vertical, myLayer, mySpread)
                                end if
                                if myYCenterAlign is true then
                                    my myDrawGuide(myYCenter, horizontal, myLayer, mySpread)
                                end if
                            end repeat
                        else
                            repeat with myCounter from 1 to (count mySelection)
                                --Get the bounds of the item (using geometric bounds if myBasedOn is 0, or visible bounds if it's 1)
                                set myPageItem to item myCounter of mySelection
                                if myBasedOn = 0 then
                                    set myBounds to geometric bounds of myPageItem
                                else
                                    set myBounds to visible bounds of myPageItem
                                end if
                                --The following routine calculates the bounds of the entire selection by comparing
                                --the bounds of individual objects.
                                if myCounter = 1 then
                                    set myY1 to (item 1 of myBounds)
                                    set myX1 to (item 2 of myBounds)
                                    set myY2 to (item 3 of myBounds)
                                    set myX2 to (item 4 of myBounds)
                                else
                                    if item 1 of myBounds < myY1 then
                                        set myY1 to (item 1 of myBounds)
                                    end if
                                    if item 2 of myBounds < myX1 then
                                        set myX1 to (item 2 of myBounds)
                                    end if
                                    if item 3 of myBounds > myY2 then
                                        set myY2 to (item 3 of myBounds)
                                    end if
                                    if item 4 of myBounds > myX2 then
                                        set myX2 to (item 4 of myBounds)
                                    end if
                                end if
                            end repeat
                            --Calculate the horizontal center and vertical center of the objects.
                            set myXCenter to myX1 + ((myX2 - myX1) / 2)
                            set myYCenter to myY1 + ((myY2 - myY1) / 2)
                            --Draw guides at the specified locations.
                            if myTopAlign is true then
                                my myDrawGuide(myY1, horizontal, myLayer, mySpread)
                            end if
                            if myLeftAlign is true then
                                my myDrawGuide(myX1, vertical, myLayer, mySpread)
                            end if
                            if myBottomAlign is true then
                                my myDrawGuide(myY2, horizontal, myLayer, mySpread)
                            end if
                            if myRightAlign is true then
                                my myDrawGuide(myX2, vertical, myLayer, mySpread)
                            end if
                            if myXCenterAlign is true then
                                my myDrawGuide(myXCenter, vertical, myLayer, mySpread)
                            end if
                            if myYCenterAlign is true then
                                my myDrawGuide(myYCenter, horizontal, myLayer, mySpread)
                            end if
                        end if
                        --Restore the ruler origin.
                        set ruler origin of view preferences of myDocument to myOldRulerOrigin
                    end tell
                end myAddGuides
                on myDrawGuidesAtPoints(myPageItem, myXPoint, myYPoint, myLayer, mySpread)
                    tell application "Adobe InDesign CS4"
                        repeat with myPathCounter from 1 to (count paths of myPageItem)
                            repeat with myPointCounter from 1 to (count path points of path myPathCounter of myPageItem)
                                set myAnchor to anchor of path point myPointCounter of path myPathCounter of myPageItem
                                if myXPoint is true then
                                    my myDrawGuide(item 1 of myAnchor, vertical, myLayer, mySpread)
                                end if
                                if myYPoint is true then
                                    my myDrawGuide(item 2 of myAnchor, horizontal, myLayer, mySpread)
                                end if
                            end repeat
                        end repeat
                    end tell
                end myDrawGuidesAtPoints
                on myDrawGuide(myLocation, myGuideOrientation, myLayer, mySpread)
                    tell application "Adobe InDesign CS4"
                        tell mySpread
                            set myGuide to make guide with properties {orientation:myGuideOrientation, location:myLocation, item layer:myLayer}
                        end tell
                    end tell
                end myDrawGuide

                • 5. Re: Select object with script after creation
                  mckayk_777-PN6kWE Level 2

                  Hope this is not all to confusing, Thank you in advance for help in this matter.

                   

                  Basically what I am trying to do is to be able to run one script that will call two other scripts to create all the layers I will use in each job add guides and crop marks.