Advertisement
Guest User

Untitled

a guest
Jan 19th, 2019
1,164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2. * @@@BUILDINFO@@@ tomaxxiVARIprocessor [1.0.5].jsx !Version! Tue Aug 13 2013 22:53:37 GMT+0200
  3. */
  4. /***********************************************************************************************************
  5.         tomaxiVARIprocessor
  6.             - Convert variable to text based on applied paragraph/character style
  7.             - Apply Object Style to variable's parent text frame
  8.             - Clear Overrides when applying Object Style
  9.         Version : 1.0.5
  10.         Type : Script
  11.         InDesign : CS5-CC
  12.         Author : Marijan Tompa (tomaxxi) | Subotica [Serbia]
  13.         Date : 13/08/2013
  14.         Contact : me (at) tomaxxi (dot) com
  15.         Twitter: @tomaxxi
  16.         Web : http://tomaxxi.com
  17. ***********************************************************************************************************/
  18.  
  19. var
  20.     _u = undefined,
  21.     appName = "tomaxxiVARIprocessor",
  22.     appVer = "1.0.5",
  23.     appShortName = appName + " | " + appVer,
  24.     appFullName = appName + " | " + appVer + " | by tomaxxi™";
  25.  
  26. main ();
  27.  
  28. function main () {
  29.     if ( app.documents.length )
  30.         app.doScript ( "tomaxxiVARIprocessor()", _u, _u, UndoModes.ENTIRE_SCRIPT, appName );
  31.     else
  32.         _alert ( "No Documents Opened!", appShortName );
  33. }
  34.  
  35. function tomaxxiVARIprocessor () {
  36.     var
  37.         mPar = getList ( app.activeDocument.allParagraphStyles ),
  38.         mChr = getList ( app.activeDocument.allCharacterStyles ),
  39.         mVar = app.activeDocument.textVariables.everyItem ().name,
  40.         mObj = getList ( app.activeDocument.allObjectStyles );
  41.         mPar.unshift ( "-- Any Paragraph Style --" );
  42.         mChr.unshift ( "-- Any Character Style --" );
  43.         mVar.shift (), mVar.shift (); mVar.unshift ( "-- Any Variable --" );
  44.     var mD = new Window ( "dialog", appFullName ), mP = mD.add ( "panel" ),
  45.             mG = mD.add ( "group" ), mG1 = mP.add ( "group" ), mG2 = mP.add ( "group" ), mG2a = mP.add ( "group" ),
  46.             mG3 = mP.add ( "panel" ), mG4 = mP.add ( "panel" );
  47.             mG1.alignChildren = [ "fill", "left" ], mG2.alignChildren = mG2a.alignChildren = mG3.alignChildren = [ "fill", "left" ];
  48.             mG4.alignChildren = [ "fill", "center" ]; mP.orientation = "column",  mP.alignChildren = [ "fill", "left" ];
  49.             mG4.orientation = "row";
  50.             mG1.add ( "statictext", _u, "Select Variable :" );
  51.             var mVr = mG1.add ( "dropdownlist", _u, mVar );
  52.                 mVr.selection = 0, mVr.minimumSize = [ 150, 20 ];
  53.             mG2.add ( "statictext", _u, "Applied Paragraph Style :" );
  54.             var mDr = mG2.add ( "dropdownlist", _u, mPar );
  55.                 mDr.selection = 0;
  56.             mG2a.add ( "statictext", _u, "Applied Character Style :" );
  57.             var mDr2 = mG2a.add ( "dropdownlist", _u, mChr );
  58.                 mDr2.selection = 0;
  59.            
  60.            
  61.            
  62.             var
  63.                 c2t = mG3.add ( "checkbox", _u, "Convert to Text" ),
  64.                 mG3_ = mG3.add ( "group" ), aOs = mG3_.add ( "checkbox", _u, "Apply Object Style :" ),
  65.                 objS = mG3_.add ( "dropdownlist", _u, mObj ),
  66.                 mG3__ = mG3.add ( "group"), cOr = mG3__.add ( "checkbox", _u, "Clear Overrides" );
  67.                 objS.enabled = false; objS.selection = 0; mG3__.alignChildren = [ "right", "right" ]; cOr.enabled = false; cOr.value = app.clearOverridesWhenApplyingStyle;
  68.                 aOs.onClick = function () { objS.enabled = cOr.enabled = this.value; mOK.enabled = ( c2t.value == true || this.value == true ); };
  69.                 c2t.onClick = function () { mOK.enabled = ( aOs.value == true || this.value == true ); };
  70.             var uF = mG4.add ( "checkbox", _u, "Include Footnotes" ),
  71.                 uM = mG4.add ( "checkbox", _u, "Include Master Pages" );
  72.                 uF.value = app.findChangeGrepOptions.includeFootnotes;
  73.                 uM.value = false;
  74.                 uM.onClick = function () { if ( this.value == true ) _alert ( "By using this option, all Master Page items that\rcontain targeted Text Variable will be overridden to the page.\r\rText Variables inside grouped, anchored items will NOT be processed.", appFullName, true ); };
  75.             var mOK = mG.add ( "button", _u, "OK" ); mG.add ( "button", _u, "Cancel" ); var _txt = mG.add ( "statictext", _u, "http://tomaxxi.com/" );
  76.             _txt.onClick = function () { gotoLink ( "http://tomaxxi.com" ); };
  77.             _txt.onDraw = function () {
  78.                 var
  79.                     linePen = mD.graphics.newPen ( mD.graphics.PenType.SOLID_COLOR, [ 0, 0, 0 ], 1 ),
  80.                     gx = this.graphics, sz = this.preferredSize, y = sz [ 1 ] - 1;
  81.                     gx.drawOSControl (); gx.newPath (); gx.moveTo ( 0, y );
  82.                     gx.lineTo ( sz [ 0 ], y ); gx.strokePath ( linePen );
  83.             };
  84.             mOK.enabled = false;
  85.             mOK.onClick = function () { if ( aOs.value == true || c2t.value == true ) mD.close ( 1 ); };
  86.     mD.center ();
  87.     var mA = mD.show ();
  88.     if ( mA == true ) {
  89.         app.findGrepPreferences = app.changeGrepPreferences = NothingEnum.nothing;
  90.         var oldPrefs = [ app.findChangeGrepOptions.includeFootnotes, app.findChangeGrepOptions.includeMasterPages ];
  91.         app.findChangeGrepOptions.includeFootnotes = uF.value;
  92.         app.findChangeGrepOptions.includeMasterPages = uM.value;
  93.         app.findGrepPreferences.findWhat = "~v";
  94.         app.findGrepPreferences.appliedParagraphStyle = mDr.selection.index != 0 ? app.activeDocument.allParagraphStyles [ mDr.selection.index - 1 ] : "";
  95.         app.findGrepPreferences.appliedCharacterStyle = mDr2.selection.index != 0 ? app.activeDocument.allCharacterStyles [ mDr2.selection.index - 1 ] : "";
  96.         var myF = app.activeDocument.findGrep (), count = Number ( 0 ), i = myF.length - 1, countOver = 0;
  97.         if ( uM.value == true ) {
  98.             for ( i ; i >= 0 ; i-- ) {
  99.                 if ( myF [ i ].textVariableInstances.length > 0 && ( mVr.selection.index == 0 || myF [ i ].textVariableInstances [ 0 ].name == String ( mVr.selection ) ) ) {
  100.                     if ( myF [ i ].parentTextFrames [ 0 ].parent.hasOwnProperty ( "baseName" ) ) {
  101.                         var
  102.                             pO = getPagesMaster ( myF [ i ].parentTextFrames [ 0 ].parent ), p = 0,
  103.                             tF = myF [ i ].parentTextFrames [ 0 ];
  104.                         for ( p ; p < pO.length ; p++ )
  105.                             doOverride ( app.activeDocument, pO [ p ], tF ), countOver++;
  106.                     }
  107.                 }
  108.             }
  109.             var myF = app.activeDocument.findGrep (), count = Number ( 0 ), i = myF.length - 1;
  110.         }
  111.         for ( i ; i >= 0 ; i-- ) {
  112.             if ( myF [ i ].textVariableInstances.length > 0 && ( mVr.selection.index == 0 || myF [ i ].textVariableInstances [ 0 ].name == String ( mVr.selection ) ) ) {
  113.                 if ( c2t.value == true )
  114.                     myF [ i ].textVariableInstances [ 0 ].convertToText ();
  115.                 if ( aOs.value == true )
  116.                     if ( myF [ i ].parent.overflows )
  117.                         myF [ i ].parentStory.textContainers [ myF [ i ].parentStory.textContainers.length - 1 ].applyObjectStyle ( app.activeDocument.allObjectStyles [ objS.selection.index ], cOr.value, _u );
  118.                     else
  119.                         myF [ i ].parentTextFrames [ 0 ].applyObjectStyle ( app.activeDocument.allObjectStyles [ objS.selection.index ], cOr.value, _u );
  120.                 count++;
  121.             }
  122.         }
  123.         count > 0 ? _alert ( "Number of Processed Variables: " + count + ( uM.value == true && countOver > 0 ? "\rSome Master Page items were overridden during the process." : "" ), appShortName ) : _alert ( "No Variables Processed.", appShortName );
  124.         app.findGrepPreferences = app.changeGrepPreferences = NothingEnum.nothing;
  125.         app.findChangeGrepOptions.includeFootnotes = oldPrefs [ 0 ];
  126.         app.findChangeGrepOptions.includeMasterPages = oldPrefs [ 1 ];
  127.     }
  128.     function getPagesMaster ( fMaster ) {
  129.         var
  130.             mP = [], i = 0,
  131.             dP = app.activeDocument.pages,
  132.             dPl = dP.length;
  133.         for ( i ; i < dPl ; i++ )
  134.             if ( dP [ i ].appliedMaster == fMaster )
  135.                 mP.push ( dP [ i ].documentOffset );
  136.         return mP;
  137.     }
  138.     function doOverride ( doc, page, item ) {
  139.       var
  140.         aI = doc.pages [ page ].appliedMaster.pageItems.everyItem ().getElements (),
  141.         i = 0;
  142.       for ( i ; i < aI.length ; i++ ) {
  143.             try {
  144.                 if ( aI [ i ] == item ) aI [ i ].override ( doc.pages [ page ] ) }
  145.             catch ( _ ) { };
  146.         }
  147.     }
  148.     function getList ( obj ) {
  149.         var
  150.             arr = [], i = 0;
  151.         for ( i ; i < obj.length ; i++ )
  152.             arr.push ( obj [ i ].name );
  153.         return arr;
  154.     }
  155. }
  156.  
  157. ////////////////// FUNCTIONS //////////////////
  158. function _alert ( message, title, errorIcon ) {
  159.     if ( isMac () )
  160.         alert ( title + "\n\n" + message );
  161.     else
  162.         alert ( message, title, errorIcon );
  163. }
  164. function isMac () {
  165.     return ( File.fs == "Macintosh" );
  166. }
  167. function gotoLink ( url ) {
  168.     var
  169.         body, language;
  170.     url = url || "http://tomaxxi.com";
  171.     if ( isMac () )
  172.         body = 'tell application "Finder"\ropen location "' + url + '"\rend tell',
  173.         language = ScriptLanguage.APPLESCRIPT_LANGUAGE;
  174.     else
  175.         body = 'dim objShell\rset objShell = CreateObject("Shell.Application")\rstr = "' + url + '"\robjShell.ShellExecute str, "", "", "open", 1 ',
  176.         language = ScriptLanguage.VISUAL_BASIC;
  177.     app.doScript ( body, language );
  178. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement