Skip to main content
Inspiring
January 18, 2014
Question

How to select needed objects [CS6-JSX]

  • January 18, 2014
  • 2 replies
  • 1290 views

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.strokeWidth==0.17599999904633) allObject.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!

This topic has been closed for replies.

2 replies

pixxxelschubser
Community Expert
Community Expert
January 18, 2014
@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.strokeWidth*1000) == 176) {

    allObject.selected=true;

    }

}

But remember, loop through all items is always slowly.

Have fun

BeliakovAuthor
Inspiring
January 20, 2014

"if (Math.round(allObject.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!

Inspiring
January 20, 2014

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.

Inspiring
January 18, 2014

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.