0 Replies Latest reply on Jul 30, 2010 10:40 PM by macian2

    New (AS) for adding "Column Rules", CS3

    macian2 Level 1

      This is an Applescript file. I currently know little of Javascript.

       

      I have created a new way to add "Column Rules" to any single text frame.
      The column rules are perfectly centered in the designated gutters.

       

      • Your text frame must have two or more columns.
      • Your text frame may be placed anywhere on your active page.
      • Your text frame must have a gutter more than 3.0 points.
      • Your text frame can have any variable indents.
      • The default column rule is .5 point, and can be modified through script file.
      • Modified an earlier script found.

       

      I will gladly accept any comments or suggestions to improve the script.

      If anyone who is well versed in javascript, please do so and share!

       

      The script is as follows:

       

      --== START OF SCRIPT  -- "ADD Column Rules"

       

      (* Tested in Adobe InDesign CS3 only *)
      global theDoc
      global oldHorz
      global oldVert

       

      tell application "Adobe InDesign CS3"
      activate
      set theDoc to active document

       

      --GET USERS CURRENT UNIT OF MEASURE SETTINGS
      tell theDoc to set {oldHorz, oldVert} to {horizontal measurement units of view preferences, vertical measurement units of view preferences}

       

      --SET THE NEW UNIT OF MEASURE  >> POINTS   
      tell theDoc to set the properties of view preferences to {horizontal measurement units:points, vertical measurement units:points, ruler origin:page origin}

       

      --CHECK IF DOCUMENT IS OPEN
      if (count documents) = 0 then
      beep
      display dialog "Please open a document and try again!" buttons {"Opps!"} default button 1 with icon 1 giving up after 10
      return
      end if

       

      --CHECK IF ANY SELECTION HAS BEEN MADE
      try
      set theSel to object reference of selection
      on error
      beep
      display alert "No Selection Was Made..." message "Opps... you forgot to make a selection!" & return & "Please try again and select a single \"text frame\"." buttons {"Opps!"} default button 1 giving up after 10
      run resetMeasureUnits
      return
      end try
      set thePage to parent of selection

       

      --CHECK IF MORE THAN ONE ITEM IS SELECTED
      if (count of theSel) ≥ 2 then
      beep
      display alert "More Than One Object Selected!" message "Opps... You have selected more than one item." & return & "Please try again and select a single \"text frame\"." buttons {"Opps!"} default button 1 giving up after 10
      select nothing
      run resetMeasureUnits
      return
      end if

       

      --CHECK IF THE SELECTION IS TEXT BOX
      if content type of theSel is not text type then
      beep
      display alert "The selection is NOT a \"Text\" frame!" message "Opps... Your selection is not a \"text\" box." & return & "Please select a single \"Text Frame\"" & return & "Try again..." buttons {"Opps!"} default button 1 giving up after 10
      select nothing
      run resetMeasureUnits
      return
      end if

       

      --CHECK IF THE SELECTED TEXT BOX CONTAINS MORE THAN ONE COLUMN
      if content type of theSel is not text type then
      beep
      display alert "The selection is NOT a \"Text\" frame!" message "Opps... Your selection is not a \"text\" box." & return & "Please select a single \"Text Frame\"" & return & "Try again..." buttons {"Opps!"} default button 1 giving up after 10
      select nothing
      run resetMeasureUnits
      return
      end if

       

      --CHECK IF THE TEXT COLUMN GUTTER IS GREATER THAN 2.5 POINTS
      if (text column gutter of text frame preferences of theSel is less than 3) then
      beep
      display alert "Text Column Gutter below minimum..." message "Opps... Your selection requires" & return & "the text column gutter to be" & return & "3+ points or greater." & return & "Please try again..." buttons {"Opps!"} default button 1 giving up after 10
      select nothing
      run resetMeasureUnits
      return
      end if

       

      --GET OTHER SPEC'S
      set oldMarginPrefs to properties of margin preferences of thePage
      set oldColCount to (get column count of margin preferences of thePage)
      set oldColGut to (get column gutter of margin preferences of thePage)

       

      tell theDoc
      set theSel to object reference of selection

       

      --GET THE DOCUMENT MARGINS       
      set myPageWidth to get page width of document preferences as points
      set myPageHeight to get page height of document preferences as points

       

      --GET PROPERTIES OF THE SELECTION
      set gb1 to item 1 of (get geometric bounds of theSel) --top
      set gb2 to item 2 of (get geometric bounds of theSel) --left
      set gb3 to item 3 of (get geometric bounds of theSel) --bottom
      set gb4 to item 4 of (get geometric bounds of theSel) --right

       

      set colCount to text column count of text frame preferences of theSel
      set colGut to text column gutter of text frame preferences of theSel

       

      set tfSpace1 to item 1 of (get inset spacing of text frame preferences of theSel) --top 3
      set tfSpace2 to item 2 of (get inset spacing of text frame preferences of theSel) --bottom 6
      set tfSpace3 to item 3 of (get inset spacing of text frame preferences of theSel) --left 9
      set tfSpace4 to item 4 of (get inset spacing of text frame preferences of theSel) --right 12

       

      end tell --theDoc

       

      set properties of margin preferences of thePage to {bottom:((myPageHeight - gb3) + tfSpace3), left:(gb2 + tfSpace2), right:((myPageWidth - gb4) + tfSpace4), top:(gb1 + tfSpace1), column count:colCount, column gutter:colGut}

       

      --==============
      run CenterColumnsRules
      --==============

       

      --RESET THE USERS ORGINAL MARGIN PREFERENCES
      set properties of margin preferences of thePage to oldMarginPrefs

       

      --RESET THE USERS ORIGINAL UNIT OF MEASURE SETTING
      tell theDoc to set the properties of view preferences to {horizontal measurement units:oldHorz, vertical measurement units:oldVert, ruler origin:page origin}

       

      select nothing

       

      end tell --application

       

      --==========
      --  HANDLERS
      --==========

       

      script resetMeasureUnits
      tell application "Adobe InDesign CS3"
      --RESET THE USERS ORIGINAL UNIT OF MEASURE SETTING
      tell theDoc to set the properties of view preferences to {horizontal measurement units:oldHorz, vertical measurement units:oldVert}
      end tell --app
      end script

       

      script CenterColumnsRules
      property strokeWeight : ".5" --points only
      --property strokeWeight : missing value

       

      --Environment
      try
      set theDoc to getDocRef()
      --call to handler
      set {pageWid, pageHgt, topmar, leftMar, botmar, rightMar, colCount, colGut} to getPageParams(theDoc)
      if colCount < 2 then
      error "Requires page with columns"
      end if
      --makeLayer requires doc reference, name, visible and locked
      set theLayer to makeLayer(theDoc, "Column Rules", true, false)
      set x1 to pageWid - rightMar
      set cGutList to GetcGutterXList(leftMar, x1, colCount, colGut)
      on error errStr
      fatalMessage(errStr)
      end try

       

      --Input
      if strokeWeight is missing value then
      set strokeWeight to measureEditbox()
      end if

       

      --Process
      tell application "Adobe InDesign CS3"
      tell theDoc
      set lineProperties to {stroke alignment:center alignment, stroke color:swatch "Black", stroke type:"Solid", stroke weight:strokeWeight, item layer:theLayer, label:"ColumnRule"}
      end tell
      tell active page of layout window 1
      repeat with i from 1 to count of cGutList
      set thisx to item i of cGutList
      set theLine to make graphic line with properties {geometric bounds:{topmar, thisx, (pageHgt - botmar), thisx}} & lineProperties
      end repeat
      end tell --page
      end tell --application
      beep 1

       

      --===HANDLERS

       

      (* Returns center x coordinate for each gutter on page. *)
      on GetcGutterXList(x0, x1, numCol, gutWid)
      set colXList to {}
      set colwid to ((x1 - x0) - (gutWid * (numCol - 1))) / numCol
      set stepValue to colwid + gutWid
      set thiscolX to x0 + colwid + gutWid / 2
      repeat with i from 2 to numCol
      set end of colXList to thiscolX
      set thiscolX to thiscolX + stepValue
      end repeat
      return colXList
      end GetcGutterXList

       

      (* Returns list of essential page parameters. *)
      on getPageParams(theDoc)
      tell application "Adobe InDesign CS3"
      set thePage to active page of layout window 1
      tell document preferences of theDoc
      set {pageWid, pageHgt} to {page width, page height}
      end tell
      tell margin preferences of thePage
      set {topmar, leftMar, botmar, rightMar, colCount, colGut} to {top, left, bottom, right, column count, column gutter}
      end tell --margin prefs
      end tell --app
      return {pageWid, pageHgt, topmar, leftMar, botmar, rightMar, colCount, colGut}
      end getPageParams

       

      (*returns reference to active document. Will generate error if no document. *)
      on getDocRef()
      tell application "Adobe InDesign CS3"
      if not (exists document 1) then
      error "Requires active document with selected object"
      else
      return active document
      end if
      end tell --app
      end getDocRef

       

      (*Creates layer if one by name is not in document;
      moves it to bottom of layers. *)
      on makeLayer(docRef, theName, theVisible, theLocked)
      tell application "Adobe InDesign CS3"
      tell docRef
      if exists layer theName then
      set theLayer to layer theName
      if locked of theLayer is true then
      set locked of theLayer to false
      end if
      else
      set theLayer to make layer with properties {visible:theVisible, locked:theLocked, name:theName}
      --In CS2 you need to move the layer to after layer -1 rather than end of layers
      move theLayer to after layer 1
      end if
      end tell --docRef
      end tell --app
      return theLayer
      end makeLayer

       


      --Gives user a message and cancels out of script
      on fatalMessage(theMessage)
      activate
      display dialog theMessage buttons {"OK"} default button 1
      error "User Error" number -128
      end fatalMessage

       

      (* Creates measurement editbox in custom dialog *)
      on measureEditbox()
      set theContents to ""
      tell application "Adobe InDesign CS3"
      activate
      set dialogRef to make dialog with properties {name:"Enter Rule Width", can cancel:true}
      tell dialogRef
      tell (make dialog column)
      tell (make border panel)
      tell (make dialog column)
      make static text with properties {static label:"Enter width for rule (points)"}
      make static text with properties {static label:"Rule Width:"}
      end tell
      tell (make dialog column)
      make static text with properties {static label:""}
      set txtRef to (make measurement editbox with properties {edit contents:".5", edit units:points, minimum value:0.25, maximum value:12})
      end tell --dialog column
      end tell --border panel
      end tell --dialog  column
      show
      end tell --dialog ref
      set dialogResult to result
      if dialogResult is true then
      set theResult to edit contents of txtRef
      end if
      --You will want to add the statement to destroy the dialog to remove it from memory as part of your script
      destroy dialogRef
      end tell
      return theResult
      end measureEditbox

       

      end script

       

      --== END OF SCRIPT  -- "ADD Column Rules"