12 Replies Latest reply on May 18, 2018 7:42 AM by Marc Autret

    jsxbin and avoiding toSource/eval

    Stephan Moebius Level 1

      Hi,

       

      i am trying to save my script as a jsxbin, but it won't work properly as a binary, because i am using toSource/eval on several occasion to store complex objects inside a scriptLabel or a file and i use eval to restore the objects.

       

      For instance i use

       

      app.doScript(function(){ad.insertLabel(_scrLbl, lbl.toSource())}, undefined, undefined, UndoModes.AUTO_UNDO, "Store label");
      

       

      and later to restore the object:

       

      eval(ad.extractLabel(_scrLbl))
      

       

       

      In another example i create an array of sources of paragraphStyles like this

       

      function getSources (a /*arr of parstyles*/, r, i) 
              {
                  r = [];
                  for (i = 0; i < a.length; i++) {r[i] = a[i].properties.toSource(); };
                  return r;
              }
      
      a = getSources(app.activeDocument.allParagraphStyles);
      

       

      I do this at different times and with that and with Marc Autrets operator below, i am able to compare if the array 'a', aka the paragraphStyles did change in any way:

       

      Array.prototype["=="] = function(a) {
            // Make myArray1==myArray2 relevant (by Marc Autret)
             return this.toSource() == a.toSource()
          };
      

       

      This is so very convenient! How do you circumvent toSource/eval? Do you guys all write complex CRUD file-routines, or JSON parsers or something to store data?

       

      with kind regards,

      Stephan

        • 1. Re: jsxbin and avoiding toSource/eval
          Loic.Aigon Adobe Community Professional

          Use either Douglas Crockford JSON2's parse/stringify method:

           

          https://github.com/douglascrockford/JSON-js/blob/master/json2.js

           

          Or this one by Marc Autret which has been optimized for InDesign :

           

          GitHub - indiscripts/IdExtenso: ExtendScript Framework for InDesign Ninjas

           

          Or switch to XML as E4X offers a native to XMLString() methods. The latter string being loaded as object by new XML ( xmlString );

           

          FWIW

          1 person found this helpful
          • 2. Re: jsxbin and avoiding toSource/eval
            Stephan Moebius Level 1

            Thanks a lot, Loic, (Loic is your Surname?) for showing me the options.

             

            I read that JSON2, though some people here stated it works in Extendscript, will NOT work with "cyclical" objects. Does that mean that indesigns objects will not work with it, since they are often cyclical... is that correct? Or will it just ignore/drop the cyclical key/values and silently work?

             

            About Marcs iDExtenso... i fear i am not powerful enough to weild it. I'll try my best. But would that force my script to have 'includes' or is there a way to copy only the ~stringify/evaluating portion out of IDExtenso and put it into my script? I'd prefer it to be a single contained script-file.

             

            Stephan

            • 3. Re: jsxbin and avoiding toSource/eval
              Loic.Aigon Adobe Community Professional

              Yes, that "Loic" indeed (pronounce lo-eek )

               

              I don't know what you mean by cyclical objects but yes JSON.stringify is not intended to work with InDesign objects as toSource() does.

               

              So you have to keep on using toSource() or change your approach using snippets for example.

              • 4. Re: jsxbin and avoiding toSource/eval
                Stephan Moebius Level 1

                Sorry, i wasn't clear. I meant to store Indesign DOM objects. That is what Douglas Crockford means by cyclical doesn't he? Like paragraphs[0].paragraphs[0].paragraphs[0]....?

                 

                I am exctited about that E4X implementation. Will try this!

                • 5. Re: jsxbin and avoiding toSource/eval
                  Loic.Aigon Adobe Community Professional

                  E4X won't let you store DOM objects neither but the point is : what's the point ? toSource() will store the reference to the object as a string. But what if you try to eval something that has been gone in the middle…

                   

                  var sel  =app.selection[0];

                  var s = sel.toSource();

                  sel.remove();

                  alert( eval ( sel ).isValid ); //false;

                   

                  So instead you may want to either store object properties (JSON.stringify ( obj.properties) )to rebuild it or export it as snippet or icml or tagged text given the source object.

                  • 6. Re: jsxbin and avoiding toSource/eval
                    Marc Autret Level 5

                    Hi Stephan,

                     

                    IdExtenso supports #include with no perceptible side effect on your script. It can be jsxbinified as well, and still works fine in a #targetengine.

                    To get the full JSON feature you just need the core (since $$.JSON is a core module.)

                     

                    #targetengine 'YourEngine'
                    
                    #include 'path/to/$$.jsxinc'
                    $$.load();
                    
                    // YOUR SCRIPT -- use $$.JSON(), etc
                    
                    $$.unload();
                    

                     

                    That's it.

                     

                    Now, about stringified DOM objects, the full $$.JSON of a paragraph style is about 2MB large in CC 2018 if you don't prevent DOM objs from being browsed! So don't forget to set the DOM_ACCESS parameter to -1:

                     

                    var myJson = $$.JSON(myParaStyle,1,-1);

                     

                    Here is a typical return (in verbose mode, and removing the outer quotes):

                     

                    {
                        "name" : "[No Paragraph Style]",
                        "imported" : false,
                        "splitDocument" : false,
                        "emitCss" : true,
                        "styleUniqueId" : "",
                        "paragraphShadingLeftOffset" : 0,
                        "paragraphShadingRightOffset" : 0,
                        "paragraphShadingTopOffset" : 0,
                        "paragraphShadingBottomOffset" : 0,
                        "paragraphShadingWidth" : 0x4B6C7764 /* [Klwd] */,
                        "paragraphShadingTopOrigin" : 0x70735461 /* [psTa] */,
                        "paragraphShadingBottomOrigin" : 0x70734264 /* [psBd] */,
                        "paragraphShadingClipToFrame" : false,
                        "paragraphShadingSuppressPrinting" : false,
                        "paragraphShadingOn" : false,
                        "paragraphShadingOverprint" : false,
                        "paragraphShadingTint" : 20,
                        "paragraphShadingColor" : "/document[@id=1]//color[@id=11]",
                        "appliedFont" : "/font[@name=\"Minion Pro\tRegular\"]",
                        "fontStyle" : "Regular",
                        "pointSize" : 12,
                        "leading" : 0x6174696C /* [atil] */,
                        "kerningMethod" : "Metrics",
                        "tracking" : 0,
                        "capitalization" : 0x6E6F726D /* [norm] */,
                        "position" : 0x6E6F726D /* [norm] */,
                        "underline" : false,
                        "strikeThru" : false,
                        "ligatures" : true,
                        "noBreak" : false,
                        "horizontalScale" : 100,
                        "verticalScale" : 100,
                        "baselineShift" : 0,
                        "skew" : 0,
                        "fillTint" : -1,
                        "strokeTint" : -1,
                        "strokeWeight" : 1,
                        "overprintStroke" : false,
                        "overprintFill" : false,
                        "otfFigureStyle" : 0x44666C74 /* [Dflt] */,
                        "otfOrdinal" : false,
                        "otfFraction" : false,
                        "otfDiscretionaryLigature" : false,
                        "otfTitling" : false,
                        "otfContextualAlternate" : true,
                        "otfSwash" : false,
                        "underlineColor" : "Text Color",
                        "underlineGapColor" : "/document[@id=1]//swatch[@id=14]",
                        "underlineTint" : -1,
                        "underlineGapTint" : -1,
                        "underlineOverprint" : false,
                        "underlineGapOverprint" : false,
                        "underlineType" : "/document[@id=1]/stroke-style[@id=23081]",
                        "underlineOffset" : -9999,
                        "underlineWeight" : -9999,
                        "strikeThroughColor" : "Text Color",
                        "strikeThroughGapColor" : "/document[@id=1]//swatch[@id=14]",
                        "strikeThroughTint" : -1,
                        "strikeThroughGapTint" : -1,
                        "strikeThroughOverprint" : false,
                        "strikeThroughGapOverprint" : false,
                        "strikeThroughType" : "/document[@id=1]/stroke-style[@id=23081]",
                        "strikeThroughOffset" : -9999,
                        "strikeThroughWeight" : -9999,
                        "fillColor" : "/document[@id=1]//color[@id=11]",
                        "strokeColor" : "/document[@id=1]//swatch[@id=14]",
                        "appliedLanguage" : "/language-with-vendors[@id=59]",
                        "includeClass" : true,
                        "paragraphBorderOn" : false,
                        "paragraphBorderOverprint" : false,
                        "paragraphBorderTint" : -1,
                        "paragraphBorderColor" : "/document[@id=1]//color[@id=11]",
                        "paragraphBorderGapOverprint" : false,
                        "paragraphBorderGapTint" : -1,
                        "paragraphBorderGapColor" : "/document[@id=1]//swatch[@id=14]",
                        "paragraphBorderType" : "/document[@id=1]/stroke-style[@id=23081]",
                        "paragraphBorderLeftLineWeight" : 1,
                        "paragraphBorderTopLineWeight" : 1,
                        "paragraphBorderRightLineWeight" : 1,
                        "paragraphBorderBottomLineWeight" : 1,
                        "paragraphBorderStrokeEndCap" : 0x62636170 /* [bcap] */,
                        "paragraphBorderStrokeEndJoin" : 0x6D6A6F6E /* [mjon] */,
                        "paragraphShadingTopLeftCornerRadius" : 1,
                        "paragraphShadingTopLeftCornerOption" : 0x6E6F6E65 /* [none] */,
                        "paragraphShadingTopRightCornerRadius" : 1,
                        "paragraphShadingTopRightCornerOption" : 0x6E6F6E65 /* [none] */,
                        "paragraphShadingBottomLeftCornerRadius" : 1,
                        "paragraphShadingBottomLeftCornerOption" : 0x6E6F6E65 /* [none] */,
                        "paragraphShadingBottomRightCornerRadius" : 1,
                        "paragraphShadingBottomRightCornerOption" : 0x6E6F6E65 /* [none] */,
                        "paragraphBorderTopLeftCornerRadius" : 1,
                        "paragraphBorderTopLeftCornerOption" : 0x6E6F6E65 /* [none] */,
                        "paragraphBorderTopRightCornerRadius" : 1,
                        "paragraphBorderTopRightCornerOption" : 0x6E6F6E65 /* [none] */,
                        "paragraphBorderBottomLeftCornerRadius" : 1,
                        "paragraphBorderBottomLeftCornerOption" : 0x6E6F6E65 /* [none] */,
                        "paragraphBorderBottomRightCornerRadius" : 1,
                        "paragraphBorderBottomRightCornerOption" : 0x6E6F6E65 /* [none] */,
                        "paragraphBorderWidth" : 0x4B6C7764 /* [Klwd] */,
                        "paragraphBorderTopOrigin" : 0x70735461 /* [psTa] */,
                        "paragraphBorderBottomOrigin" : 0x70734264 /* [psBd] */,
                        "paragraphBorderLeftOffset" : 0,
                        "paragraphBorderRightOffset" : 0,
                        "paragraphBorderTopOffset" : 0,
                        "paragraphBorderBottomOffset" : 0,
                        "paragraphBorderDisplayIfSplits" : false,
                        "providerHyphenationStyle" : 0x64616C6C /* [dall] */,
                        "mergeConsecutiveParaBorders" : false,
                        "paragraphKashidaWidth" : 2,
                        "alignToBaseline" : false,
                        "firstLineIndent" : 0,
                        "leftIndent" : 0,
                        "rightIndent" : 0,
                        "spaceBefore" : 0,
                        "spaceAfter" : 0,
                        "balanceRaggedLines" : 0x426C4F66 /* [BlOf] */,
                        "justification" : 0x6C656674 /* [left] */,
                        "singleWordJustification" : 0x66756C6C /* [full] */,
                        "autoLeading" : 120,
                        "dropCapLines" : 0,
                        "dropCapCharacters" : 0,
                        "keepLinesTogether" : false,
                        "keepAllLinesTogether" : false,
                        "keepWithNext" : 0,
                        "keepFirstLines" : 2,
                        "keepLastLines" : 2,
                        "startParagraph" : 0x6E62726B /* [nbrk] */,
                        "composer" : "Adobe Paragraph Composer",
                        "minimumWordSpacing" : 80,
                        "maximumWordSpacing" : 133,
                        "desiredWordSpacing" : 100,
                        "minimumLetterSpacing" : 0,
                        "maximumLetterSpacing" : 0,
                        "desiredLetterSpacing" : 0,
                        "minimumGlyphScaling" : 100,
                        "maximumGlyphScaling" : 100,
                        "desiredGlyphScaling" : 100,
                        "ruleAbove" : false,
                        "ruleAboveOverprint" : false,
                        "ruleAboveLineWeight" : 1,
                        "ruleAboveTint" : -1,
                        "ruleAboveOffset" : 0,
                        "ruleAboveLeftIndent" : 0,
                        "ruleAboveRightIndent" : 0,
                        "ruleAboveWidth" : 0x4B6C7764 /* [Klwd] */,
                        "ruleAboveColor" : "Text Color",
                        "ruleAboveGapColor" : "/document[@id=1]//swatch[@id=14]",
                        "ruleAboveGapTint" : -1,
                        "ruleAboveGapOverprint" : false,
                        "ruleAboveType" : "/document[@id=1]/stroke-style[@id=23081]",
                        "ruleBelow" : false,
                        "ruleBelowLineWeight" : 1,
                        "ruleBelowTint" : -1,
                        "ruleBelowOffset" : 0,
                        "ruleBelowLeftIndent" : 0,
                        "ruleBelowRightIndent" : 0,
                        "ruleBelowWidth" : 0x4B6C7764 /* [Klwd] */,
                        "ruleBelowColor" : "Text Color",
                        "ruleBelowGapColor" : "/document[@id=1]//swatch[@id=14]",
                        "ruleBelowGapTint" : -1,
                        "ruleBelowType" : "/document[@id=1]/stroke-style[@id=23081]",
                        "hyphenateCapitalizedWords" : true,
                        "hyphenation" : true,
                        "hyphenateBeforeLast" : 2,
                        "hyphenateAfterFirst" : 2,
                        "hyphenateWordsLongerThan" : 5,
                        "hyphenateLadderLimit" : 3,
                        "hyphenationZone" : 12.7,
                        "hyphenWeight" : 5,
                        "dropCapStyle" : "/document[@id=1]//character-style[@id=118]",
                        "lastLineIndent" : 0,
                        "hyphenateLastWord" : true,
                        "otfSlashedZero" : false,
                        "otfHistorical" : false,
                        "otfStylisticSets" : 0,
                        "gradientFillLength" : -1,
                        "gradientFillAngle" : 0,
                        "gradientStrokeLength" : -1,
                        "gradientStrokeAngle" : 0,
                        "gradientFillStart" : [ 0, 0 ],
                        "gradientStrokeStart" : [ 0, 0 ],
                        "keepWithPrevious" : false,
                        "spanSplitColumnCount" : 0x616C6C20 /* [all ] */,
                        "spanColumnType" : 0x4553636C /* [EScl] */,
                        "splitColumnInsideGutter" : 2.11666666666667,
                        "splitColumnOutsideGutter" : 0,
                        "spanColumnMinSpaceBefore" : 0,
                        "spanColumnMinSpaceAfter" : 0,
                        "ruleBelowOverprint" : false,
                        "ruleBelowGapOverprint" : false,
                        "dropcapDetail" : 1,
                        "hyphenateAcrossColumns" : true,
                        "keepRuleAboveInFrame" : false,
                        "ignoreEdgeAlignment" : false,
                        "otfMark" : true,
                        "otfLocale" : true,
                        "positionalForm" : 0x6E6F6E65 /* [none] */,
                        "paragraphDirection" : 0x4C325264 /* [L2Rd] */,
                        "paragraphJustification" : 0x706A6465 /* [pjde] */,
                        "miterLimit" : 4,
                        "strokeAlignment" : 0x7374414F /* [stAO] */,
                        "endJoin" : 0x6D6A6F6E /* [mjon] */,
                        "otfOverlapSwash" : false,
                        "otfStylisticAlternate" : false,
                        "otfJustificationAlternate" : false,
                        "otfStretchedAlternate" : false,
                        "characterDirection" : 0x44656664 /* [Defd] */,
                        "keyboardDirection" : 0x44656664 /* [Defd] */,
                        "digitsType" : 0x64696465 /* [dide] */,
                        "kashidas" : 0x6B616465 /* [kade] */,
                        "diacriticPosition" : 0x64706F62 /* [dpob] */,
                        "xOffsetDiacritic" : 0,
                        "yOffsetDiacritic" : 0,
                        "tabList" : [],
                        "gridAlignFirstLineOnly" : false,
                        "gridAlignment" : 0x6E6F6E65 /* [none] */,
                        "gridGyoudori" : 0,
                        "autoTcy" : 0,
                        "autoTcyIncludeRoman" : false,
                        "kinsokuSet" : 0x6E616461 /* [nada] */,
                        "kinsokuType" : 0x4A6B6966 /* [Jkif] */,
                        "kinsokuHangType" : 0x6E6F6E65 /* [none] */,
                        "bunriKinshi" : true,
                        "mojikumi" : 0x6E616461 /* [nada] */,
                        "rensuuji" : true,
                        "rotateSingleByteCharacters" : false,
                        "leadingModel" : 0x4A6C6162 /* [Jlab] */,
                        "characterAlignment" : 0x4A616374 /* [Jact] */,
                        "tsume" : 0,
                        "leadingAki" : -1,
                        "trailingAki" : -1,
                        "characterRotation" : 0,
                        "jidori" : 0,
                        "shataiMagnification" : 0,
                        "shataiDegreeAngle" : 4500,
                        "shataiAdjustRotation" : false,
                        "shataiAdjustTsume" : true,
                        "tatechuyoko" : false,
                        "tatechuyokoXOffset" : 0,
                        "tatechuyokoYOffset" : 0,
                        "kentenFillColor" : "Text Color",
                        "kentenStrokeColor" : "Text Color",
                        "kentenTint" : -1,
                        "kentenStrokeTint" : -1,
                        "kentenWeight" : -1,
                        "kentenOverprintFill" : 0x6174696C /* [atil] */,
                        "kentenOverprintStroke" : 0x6174696C /* [atil] */,
                        "kentenKind" : 0x6E6F6E65 /* [none] */,
                        "kentenPlacement" : 0,
                        "kentenAlignment" : 0x4A6B6E63 /* [Jknc] */,
                        "kentenPosition" : 0x4A6B6172 /* [Jkar] */,
                        "kentenFont" : null,
                        "kentenFontStyle" : 0x6E616461 /* [nada] */,
                        "kentenFontSize" : -1,
                        "kentenXScale" : 100,
                        "kentenYScale" : 100,
                        "kentenCustomCharacter" : "",
                        "kentenCharacterSet" : 0x4A636869 /* [Jchi] */,
                        "rubyFill" : "Text Color",
                        "rubyStroke" : "Text Color",
                        "rubyTint" : -1,
                        "rubyWeight" : -1,
                        "rubyOverprintFill" : 0x6174696C /* [atil] */,
                        "rubyOverprintStroke" : 0x6174696C /* [atil] */,
                        "rubyStrokeTint" : -1,
                        "rubyFont" : null,
                        "rubyFontStyle" : 0x6E616461 /* [nada] */,
                        "rubyFontSize" : -1,
                        "rubyOpenTypePro" : true,
                        "rubyXScale" : 100,
                        "rubyYScale" : 100,
                        "rubyType" : 0x4A727063 /* [Jrpc] */,
                        "rubyAlignment" : 0x4A726A73 /* [Jrjs] */,
                        "rubyPosition" : 0x4A6B6172 /* [Jkar] */,
                        "rubyXOffset" : 0,
                        "rubyYOffset" : 0,
                        "rubyParentSpacing" : 0x4A723132 /* [Jr12] */,
                        "rubyAutoAlign" : true,
                        "rubyOverhang" : false,
                        "rubyAutoScaling" : false,
                        "rubyParentScalingPercent" : 66,
                        "rubyParentOverhangAmount" : 0x4A726F31 /* [Jro1] */,
                        "warichu" : false,
                        "warichuSize" : 50,
                        "warichuLines" : 2,
                        "warichuLineSpacing" : 0,
                        "warichuAlignment" : 0x6174696C /* [atil] */,
                        "warichuCharsAfterBreak" : 2,
                        "warichuCharsBeforeBreak" : 2,
                        "otfProportionalMetrics" : false,
                        "otfHVKana" : false,
                        "otfRomanItalics" : false,
                        "scaleAffectsLineHeight" : false,
                        "cjkGridTracking" : false,
                        "glyphForm" : 0x6E6F6E65 /* [none] */,
                        "paragraphGyoudori" : false,
                        "rubyAutoTcyDigits" : 0,
                        "rubyAutoTcyIncludeRoman" : false,
                        "rubyAutoTcyAutoScale" : true,
                        "treatIdeographicSpaceAsSpace" : true,
                        "allowArbitraryHyphenation" : false,
                        "bulletsAndNumberingListType" : 0x4C546E6F /* [LTno] */,
                        "bulletChar" : "/document[@id=1]//paragraph-style[@id=119]/@bullet-char",
                        "bulletsCharacterStyle" : "/document[@id=1]//character-style[@id=118]",
                        "numberingCharacterStyle" : "/document[@id=1]//character-style[@id=118]",
                        "numberingExpression" : "^#.^t",
                        "numberingRestartPolicies" : "/document[@id=1]//paragraph-style[@id=119]/@numbering-restart-policies",
                        "bulletsTextAfter" : "^t",
                        "appliedNumberingList" : "/document[@id=1]//numbering-list[@id=62]",
                        "numberingLevel" : 1,
                        "numberingFormat" : "1, 2, 3, 4...",
                        "numberingContinue" : true,
                        "numberingStartAt" : 1,
                        "numberingApplyRestartPolicy" : true,
                        "bulletsAlignment" : 0x6C656674 /* [left] */,
                        "numberingAlignment" : 0x6C656674 /* [left] */,
                        "id" : 119,
                        "label" : "",
                        "parent" : "/document[@id=1]",
                        "index" : 0
                    }
                    

                     

                    Note that DOM specifiers are not browsed and just coerced into path strings thanks to DOM_ACCESS=-1. You can uneval that json string later—or use $$.JSON.eval(myJsonStr)—and pass the object to a ParagraphStyle.properties = {…} assignment. Of course it's better to package your json strings in a more compact way (SPACED=-1 for one-line strings), and you likely will want to remove non relevant properties, or customize things.

                     

                    Try to get the same job done from the official JSON2 if your prefer… But don't be surprised if you encounter (various!) ExtendScript incompatibilites.

                     

                    If you're really afraid of IdExtenso, a standalone release of the JSON module is also available here:

                    extendscript/JSON at master · indiscripts/extendscript · GitHub

                    However, the official $$.JSON of the framework is maintained and regularly updated through IdExtenso fixes, which I cannot promise about the standalone release.

                    1 person found this helpful
                    • 7. Re: jsxbin and avoiding toSource/eval
                      Laubender Adobe Community Professional & MVP

                      Hi Marc,

                      just a question about $$.JSON() .

                       

                      Why are the enumerations done this way:

                      0x4C546E6F

                      And not in the more readable way?

                      ListType.NO_LIST

                      or in the numeric representation found in DOM documentation:

                      1280601711

                       

                      Ok, /* [LTno] */ gives the right hint to search DOM documentation…

                       

                      Thanks,

                      Uwe

                      • 8. Re: jsxbin and avoiding toSource/eval
                        Marc Autret Level 5

                        Hi Uwe,

                         

                        Laubender  wrote

                         

                        Why are the enumerations done this way:

                        (…)

                         

                        Glad you asked ;-)

                         

                        There are two distinct levels in your question:

                         

                        1. Why does $$.JSON prefer the numeric representation of an enumerator (that is, its valueOf result) over the <CLASS>.<KEY> syntax?

                         

                        Because IdExtenso tends to avoid DOM access and/or DOM-based references everywhere possible. When you assign a list type through a properties object, there is no difference—at least, nothing noticeable—between

                         

                        { "bulletsAndNumberingListType": ListType.NO_LIST }

                         

                        and

                         

                        { "bulletsAndNumberingListType": 0x4C546E6F }

                         

                        However, the latter object is lighweight in terms of storage, as it just carries a Number (0x4C546E6F), while the former object targets a full Enumerator object (ListType.NO_LIST) which involves the InDesign Scripting DOM. Such syntax is not portable in a strict JavaScript namespace, so if you need to interpret your JSON outside of an ID-targeted ExtendScript engine, the eval() will just fail. By contrast, {"bulletsAndNumberingListType": 0x4C546E6F} still works.

                         

                        2. Why does $$.JSON prefer the hexadecimal form of the numeral, 0x4C546E6F, over its decimal form, 1280601711?

                         

                        Because in this particular case, 0x4C546E6F is the most readable expression! Indeed, we know that Enumerator values are in fact built by grouping ASCII code points: 0x4C546E6F represents the four letters "\x4C\x54\x6E\x6F" etc, that is, the string "LTno". No clue is provided at all in the decimal representation 1280601711.

                        OK, that's the one the user can found in the ESTK documentation, but the actual question is, why does ESTK display 1280601711 which is totally meaningless, instead of 0x4C546E6F? (I'm confident that more advanced dev tools provide the hex form.)

                         

                        Best,

                        Marc

                         

                        [Note] For further detail on this topic, see the source code of $$.enum.jsxinc, which $$.JSON is based on.

                        1 person found this helpful
                        • 9. Re: jsxbin and avoiding toSource/eval
                          Laubender Adobe Community Professional & MVP

                          Hi Marc,

                          thank you very much for clarification.

                           

                          You wrote:

                          Such syntax is not portable in a strict JavaScript namespace, so if you need to interpret your JSON outside of an ID-targeted ExtendScript engine, the eval() will just fail. By contrast, {"bulletsAndNumberingListType": 0x4C546E6F} still works.

                          Mostly talking to myself now:

                          In what sense will that work?

                           

                          If I'm outside of ExtendScript and InDesign DOM 0x4C546E6F has no meaning. It's just a hexadecimal form of LTno, I think. eval() would then do what? A number out of this? A string > "LTno" ?

                           

                          So, if "ListType.NO_LIST" would be the value instead, wouldn't that work as well?

                          Hm… One would strip the two "" if the context becomes ExtendScript with InDesign again.

                          And you need not to do that if 0x4C546E6F is used… ?!

                           

                          Does that make sense?

                           

                          Regards,
                          Uwe

                          • 10. Re: jsxbin and avoiding toSource/eval
                            Stephan Moebius Level 1

                            Thanks Marc, as always detailed , for another route. It'll take me a while to follow up those leads.

                             

                            I won't be needing the DOM specifiers browsed, (toSource() doesn't do that either). A stringified "allParagraphStyles" of a document with 20 styles takes about 200 kb. I am ok with that,

                             

                            You know, i am $.writing allParagraphStyles as an array of toSource strings to a file. I later load it and compare it to a just now stringified array of source strings of allParagraphStyles. That's my workaround to check if ANY style has been changed at all in the document. I couldn't find an events based solution.

                            I am also $.writing a small simple settings object to file.

                             

                            I am guessing that either JSON2, iDExtenso and XML will all serve that purpose, pretty close to beeing a drop in replacement for toSource. Will try.

                             

                            Of course if you guys can point me to an example of an events based solution to monitor if paragraphStyles changed at all, that would be great too, (but off-topic).

                             

                            Stephan

                            • 11. Re: jsxbin and avoiding toSource/eval
                              Loic.Aigon Adobe Community Professional

                              Laubender  a écrit

                               

                               

                              If I'm outside of ExtendScript and InDesign DOM 0x4C546E6F has no meaning. It's just a hexadecimal form of LTno, I think. eval() would then do what? A number out of this? A string > "LTno" ?

                              From what I understand, it's because LTno would be a valid enumeration, so that could be loaded as a pure Javascript string and yet be usable with InDesign (unless I am wrong):

                               

                              Capture d’écran 2018-05-18 à 16.33.44.png

                              :

                              • 12. Re: jsxbin and avoiding toSource/eval
                                Marc Autret Level 5

                                Hi again Uwe,

                                 

                                You wrote:

                                Such syntax is not portable in a strict JavaScript namespace, so if you need to interpret your JSON outside of an ID-targeted ExtendScript engine, the eval() will just fail. By contrast, {"bulletsAndNumberingListType": 0x4C546E6F} still works.

                                Mostly talking to myself now:

                                In what sense will that work?

                                 

                                Sorry, I wasn't clear enough.

                                 

                                Keep in mind that $$.JSON() produces a string. At some point of your code you get something like this:

                                 

                                var myJsonStr = "{\"bulletsAndNumberingListType\": 0x4C546E6F}";
                                
                                // . . .
                                // and at some point, later:
                                eval("myObj="+myJsonStr);
                                alert( myObj.bulletsAndNumberingListType ); // 1280601711
                                

                                 

                                This code is portable and does work in the sense that any JS interpreter will properly parse it, even in non-ExtendScript environment. (Just suppose an extra JS library is #included in your code and has to deal with json data.)

                                 

                                Now, replace the first line by:

                                 

                                var myJsonStr = "{\"bulletsAndNumberingListType\": ListType.NO_LIST}";
                                
                                // . . .
                                // same as above
                                

                                 

                                then the eval() will cause a JS error ("Invalid JavaScript" or so) since the ListType object is unknown in that namespace.

                                That's why it is safer to bypass DOM entities when possible, and the Enumerator object is OK with that since you can always coerce it into a pure Number. (By the way, this was the case in CS4, so numbers are more cross-version friendly.)

                                 

                                Best,

                                Marc

                                1 person found this helpful