• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

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

Enthusiast ,
Jul 27, 2023 Jul 27, 2023

Copy link to clipboard

Copied

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");

 

TOPICS
Scripting

Views

473

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Enthusiast , Jul 27, 2023 Jul 27, 2023

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 
...

Votes

Translate

Translate
Adobe
Guide ,
Jul 27, 2023 Jul 27, 2023

Copy link to clipboard

Copied

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? 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Jul 27, 2023 Jul 27, 2023

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Jul 27, 2023 Jul 27, 2023

Copy link to clipboard

Copied

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;
    }
}

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Jul 27, 2023 Jul 27, 2023

Copy link to clipboard

Copied

Unfortunately, still not selecting properly.

Here's a dropbox link to a Test File 

What  I'm getting is the below:
iamwickedtall_0-1690477226757.png

What I should be seeing is this, the key difference being the selection ring higlighted to the right. It is what's indicating that I can expand the appearance.

iamwickedtall_1-1690477273311.png

I've tried seeing if I can put it in a group, but it appears it's a bit untouchable.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Jul 27, 2023 Jul 27, 2023

Copy link to clipboard

Copied

This may seem a bit convoluted, but it works! I was going to create a empty path and select all empty paths and loop through to find out if their parent was a pluginItem/Group, but this may be faster. Deselecting is of course needed for this to work properly.

 

 

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;
}

aD = app.activeDocument;
pI = aD.pluginItems;
//Deselect Everything
app.executeMenuCommand("deselectall");
for (var i = pI.length - 1; i >= 0; i--) {
    pI[i].selected = true;
    var decoy = aiDecoyPath(pI[i]);
    redraw(); //Must redraw or nothing will get selected
    app.executeMenuCommand("Selection Hat 9");//Select Next Object
    redraw(); //EDIT adding redraw running from action will skip
    app.executeMenuCommand("expandStyle");
    redraw(); //EDIT adding redraw running from action will skip
    app.executeMenuCommand("ungroup"); //Might need to test for a single compound path in the future.
    decoy.remove();
    app.executeMenuCommand("deselectall");
}

 

 

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jul 27, 2023 Jul 27, 2023

Copy link to clipboard

Copied

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");
 */

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Jul 27, 2023 Jul 27, 2023

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jul 27, 2023 Jul 27, 2023

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Jul 27, 2023 Jul 27, 2023

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Jul 27, 2023 Jul 27, 2023

Copy link to clipboard

Copied

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();

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jul 27, 2023 Jul 27, 2023

Copy link to clipboard

Copied

good!!

 

do you have lots of items in your documents? perhaps it's easier to select all then deselect the items that are not pluginItems

 

this works, it's only one layer though, the script would have to loop through all layers

var idoc = app.activeDocument;
app.executeMenuCommand ("selectall");

var pgItems = idoc.layers[0].pageItems;

for (var a=pgItems.length-1; a>=0; a--) {
    
    if (pgItems[a].typename != "PluginItem") {
        pgItems[a].selected = false
    }
}

app.executeMenuCommand("expandStyle");

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jul 27, 2023 Jul 27, 2023

Copy link to clipboard

Copied

this way we avoid creating layers and decoys to move items back to where they were

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Jul 27, 2023 Jul 27, 2023

Copy link to clipboard

Copied

LATEST

The files I have can have a thousands of items, but I do like this approach! Thanks again!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines