6 Replies Latest reply on Oct 7, 2013 10:29 PM by tobias.wantzen

    [JS/ID CC] finding properties of selection

    JeffLambert Level 1

      Hi,

       

      I'm looking for the equivalent of the AS get properties of selection for Javascript.

       

      A bit of background. I'm trying to learn JS and have no programming background except for Applescript which is not considered a "real" programing language. In AS, I found that I can often find how to adress a certain object in ID by creating that type of object and selecting it. Then user the following code to get some info on it.

       

      Tell app "Adobe InDesign CC"

      get properties of selection

      end tell

       

      it takes 2 second to right and I get a lot of info, particularly, the chain of parent-child, from the document, to the character or whatever I have selected. So I'm trying to learn the same way in JS but I can't figure out how to do this. Maybe this is not the way to go at all either, so I'd like to have a few pointers from people who made the switch from AS to JS.

       

      TIA

      Jeff

        • 1. Re: [JS/ID CC] finding properties of selection
          csm_phil Level 4

          Hi JeffLambert,

           

          You try the below JS line, it gives the properties of selection.

           

           

          alert(app.selection[0].reflect.properties);
          

           

          thx,

          csm_phil

          • 2. Re: [JS/ID CC] finding properties of selection
            JeffLambert Level 1

            Thanks csm_phil,

             

            That's not exactly what I was hoping for, but I guess you can't really learn the relationship of ojects in JS as in AS. I found Jongware's version of the object model which is most useful. I guess I'll have to find another way of learning.

             

            Thanks for your time.

             

            Jeff

            • 3. Re: [JS/ID CC] finding properties of selection
              tobias.wantzen Level 1

              Jeff,

               

              if you are asking for an equivalent to AppleScript’s »Event Protocol«, you should try the »JavaScript Console« (»Window › JavaScript Console« to turn it on or of). In your code you can send the properties to the console as follows:

              $.writeln("char properties:\r"+app.selection[0].reflect.properties);  // alternatively: $.write(); which doesn’t add a "\r" to the end
              $.writeln("char properties:\r"+app.selection[0].properties.toSource());  // this command shows the values to each property either
              

               

              If you are looking for an equivalent to AppleScript’s Application’s »Library«, you should try Jongware’s object model or use the ESTK built in »Object Modell Viewer« (»Help › Object Model Viewer«).

               

              Tobias

              • 4. Re: [JS/ID CC] finding properties of selection
                csm_phil Level 4

                Hi JeffLambert,

                 

                Yes, In AS its give all the values without loop. But JS we have to loop it then only get the properties. try the below js line.

                 

                 

                proplist = app.selection[0].reflect.properties;
                toShow = [];
                for (p in proplist)
                 try {
                  toShow.push (proplist[p]+": "+app.selection[0].texts[0][proplist[p]]);
                 } catch(_) {}
                $.write(toShow.sort().join("\n"));
                

                 

                thx,

                csm_phil

                1 person found this helpful
                • 5. Re: [JS/ID CC] finding properties of selection
                  JeffLambert Level 1

                  Thanks csm_phil, this is what I wanted.

                   

                  In Applescript Editor, it's pretty hard to read all the info, but in Script Debugger, it presents the info a bit like the Javascript Console results I get from running your script.

                   

                  @Tobias Thanks, I don't know why you're script doesn't put a return between the results like the one csm_phil does. At least, it should with the /r, but it doesn't.

                   

                  Anyway, thanks to both of you:-)

                   

                  Jeff

                  • 6. Re: [JS/ID CC] finding properties of selection
                    tobias.wantzen Level 1

                    Jeff, csm_phil used

                    .join("\n")
                    

                    to add a line feed at the end of each array item (= join to a single string).

                     

                    You could simply add it to my suggestion too:

                    $.writeln("char properties:\r"+app.selection[0].reflect.properties.join("\r"));  // alternatively: $.write(); which doesn’t add a "\r" to the end
                    $.writeln("char properties:\r"+app.selection[0].properties.toSource().join("\r"));  // this command shows the values to each property either
                    

                     

                    \n = line feed (ASCII 10), \r = carriage return (ASCII 13) – which to use depends on your system.

                     

                    Tobias