7 Replies Latest reply on Jan 20, 2014 6:06 AM by W_J_T

    How to select needed objects [CS6-JSX]

    Beliakov

      i try to move all particular objects to new layer

      so first i try to select items by strokeWidth.

       

      var mDoc = app.activeDocument;
      var allObject = mDoc.pathItems;
      for(i=0; i<allObject.length; i++){
          if (allObject[i].strokeWidth==0.17599999904633) allObject[i].selected=true;
      

      but receive nothing selected.

      i assign stroke to needede objects as 0.176 pt. OK. Then try to see this parameter of one, being selected, through script

       

      $.writeln(mDoc.selection[0].strokeWidth);
      

      and receive this long number 0.1759999....

      but when i use this number in codeline (see above), nothing is being selected. Why?

       

      After this i tried to select by color

      $.writeln(mDoc.selection[0].strokeColor);
      

      but can't get color, exept "CMYK model". So how can i select objects by stroke color?

       

      thanks!

        • 1. Re: How to select needed objects [CS6-JSX]
          W_J_T Level 4

          Regarding the strokeWidth, I did a quick test and found that you can only seem to conditionally test the following decimal numbers .75 , .5 , .25 by themselves or in conjunction with any whole numbers preceding them. That is pretty odd never noticed that (although those are the 3 listed in the stroke width drop down in the UI). I wonder if this is an actual limitation in AI scripting? However you can return as you stated other decimal numbers via alert(), so perhaps there is a trick or something special needed in such cases via scripting?

           

          Alternatively you can select a single stroke and use the UI and go:

           

          Select (menu) > Same > Stroke Weight

           

          Then once selected do your other requirements, I know its not ideal, maybe someone else will prove otherwise via scripting.

          • 2. Re: How to select needed objects [CS6-JSX]
            pixxxel schubser Level 5
            @Dmitry Beliakov,
            IMHO that's a rounding problem.
            If you cannot use the menu command select the same stroke, try this instead:

            var mDoc = app.activeDocument;
            var allObject = mDoc.pathItems;
            for(i=0; i<allObject.length; i++){
                if (Math.round(allObject[i].strokeWidth*1000) == 176) {
                allObject[i].selected=true;
                }
            }
            

             

            But remember, loop through all items is always slowly.

             

            Have fun

             

            • 3. Re: How to select needed objects [CS6-JSX]
              W_J_T Level 4

              Great call pixxxel schubser, I thought of rounding too but didn't include the *1000 approach, great suggestion. That works fine for anything now!

              • 4. Re: How to select needed objects [CS6-JSX]
                pixxxel schubser Level 5

                Yup,

                and I think this is the better way as e.g. toFixed()

                It seems to be a little bit „buggy“

                 

                Why?

                Look at this example:

                 

                var Wert = 1202.955;
                $.writeln(Math.round(Wert*100)/100);
                var Wert = 1202.958;
                $.writeln(Math.round(Wert*100)/100);
                // the first and the second results are the same
                var Wert = 1202.955;
                $.writeln(Wert.toFixed(2));
                var Wert = 1202.958;
                $.writeln(Wert.toFixed(2));
                // why the third and the fouth results are different???
                
                • 5. Re: How to select needed objects [CS6-JSX]
                  Beliakov Level 1

                  "if (Math.round(allObject[i].strokeWidth*1000) == 176) {"

                  Works! Great!

                   

                  "if you cannot use the menu command select the same stroke"

                  i'd use it with great pleasure (from under the script). But as far as i know, there is not such string for "menuCommandString". Otherwise, i'd create temporary stroke with 0,176pt and select the same strokewidth))

                   

                  Thanks for help!

                  • 6. Re: How to select needed objects [CS6-JSX]
                    pixxxel schubser Level 5

                    I dont't know if something like app.runMenuItem is possible in CS6+ (I doesn't work with CS6/CC)

                     

                    But you can create an action and call this action with

                    Application.doScript (action: string , from: string , dialogs: Boolean )

                     

                    This is definitly possible in CS6+

                     

                    Have fun


                    • 7. Re: How to select needed objects [CS6-JSX]
                      W_J_T Level 4

                      For CS6 and above there does appear to be a Application.executeMenuCommand with a menuCommandString of "Find Stroke Weight menu item" (and "

                      Find Stroke Color menu item") not sure if that's the same as Select (menu) > Same > Stroke Weight , but maybe worth a try.