Skip to main content
wckdtall
Inspiring
July 27, 2023
Answered

How do you select plugin items in a document through scripting?

  • July 27, 2023
  • 2 replies
  • 1488 views

I'm trying to write a simple script to select plugin items with Illustrator and then expand them, but when I use "selected = true" it doesn't select the item the same way as the others. Is there a different way to select? I tried referring to the parent thinking that only the children were selected, but that wasn't true either.

Seems simple, but I can't quite get this to work.

 pI = app.activeDocument.pluginItems[0];
 pI.selected = true;
 app.executeMenuCommand("expandStyle");

 

This topic has been closed for replies.
Correct answer wckdtall

Thanks! Your method may work best, I'm guessing because of "hasSelectedArtwork". When items are nested, it's causing it not to select everything even with redraws.


Created a mashup of the 2. I still needed a decoy/marker layer to move the plugin item back to. The redraws have been tested against running in an action and succeeds both ways.

function aiDecoyPath(where) {
    decoyPath = app.activeDocument.pathItems.rectangle(-50, 50, 100, 100);
    decoyPath.name = "DECOY_EMPTY_PATH";
    decoyPath.filled = false;
    decoyPath.stroked = false;
    decoyPath.move(where, ElementPlacement.PLACEBEFORE);
    decoyPath.selected = true;
    return decoyPath;
}
var idoc = app.activeDocument;
var pI = idoc.pluginItems;
var tempLayer = idoc.layers.add();
tempLayer.name = "PROCESSING"
for (var i = pI.length - 1; i >= 0; i--) {
    decoy = aiDecoyPath(pI[i]);
    pI[i].move(tempLayer, ElementPlacement.PLACEATEND);
    activeDocument.activeLayer.hasSelectedArtwork = true;
    redraw();
    app.executeMenuCommand("expandStyle");
    app.activeDocument.selection[0].move(decoy,ElementPlacement.PLACEAFTER)
    redraw();
    app.executeMenuCommand("ungroup");
    decoy.remove();
    app.executeMenuCommand("deselectall");//Adding just to be safe
}
tempLayer.remove();

2 replies

CarlosCanto
Community Expert
Community Expert
July 27, 2023

ok I see what's happening, the problem is with selecting the shape

 

pI.selected = true

 

seems to work, I can see the bounding box around the compound shape and the selection squares next to the target circle in the layers panel...but, it's not really selected, once I hover my mouse over the illustrator window the bounding  box disappears (the targeting squares remain though). Also selection.length returns 0;

 

selecting All works as you show in your previous script, so to avoid deselecting other shapes, I added a temp layer, moved the shape to the layer and selected everything in that layer

 

 

var idoc = app.activeDocument;

var pI = idoc.pluginItems[0];
 
var tempLayer = idoc.layers.add();
pI.move (tempLayer, ElementPlacement.PLACEATEND);

activeDocument.activeLayer.hasSelectedArtwork = true;

app.executeMenuCommand("expandStyle");

/* this works
activeDocument.activeLayer.hasSelectedArtwork = true;
app.executeMenuCommand("expandStyle");
 */

 

wckdtall
wckdtallAuthor
Inspiring
July 27, 2023

Thanks! Any benefit to this method over mine? There may be nested groups sublayers so I'd prefer not to move things around too much.

Running from an action, I had to add a couple more redraws or it still didn't select long enough.

CarlosCanto
Community Expert
Community Expert
July 27, 2023

no, I don't see any major difference. Both scripts do the same, select the Plugin Items by alternative methods other than using selected = true

femkeblanco
Legend
July 27, 2023

The first two lines above work as expected for me, in that they select the first pluginItem in the doc.  So the immediate question is, are you really dealing with pluginItems?  If you select the items in question and run

var a = "";
for (var i = 0; i < app.activeDocument.selection.length; i++) {
    a += app.activeDocument.selection[i].typename + "\n";
}
alert(a);

what does the alert say? 

wckdtall
wckdtallAuthor
Inspiring
July 27, 2023

Alert says PluginItem, as one would expect. This is a "legacy" compound shape coming over from photoshop. I was able to recreate simply by copying a path from photoshop and pasting into Illustrator as a compound Shape. I've looked at the shapes within and they don't have a fillColor attribute, so detecting that way also seems out of the question.

femkeblanco
Legend
July 27, 2023

I copied a compound shape from Photoshop CS4 to Illustrator, and it still worked as expected for me.

 

The only thing I can think of is if Illustrator is not reading it directly as a pluginItem, may be it will read it as a pageItem with a tracing object (pluginItems should be the only pageItems with such a property):

for (var i = 0; i < app.activeDocument.pageItems.length; i++) {
    if (app.activeDocument.pageItems[i].tracing) {
        app.activeDocument.pageItems[i].selected = true;
    }
}