Skip to main content
dublove
Legend
Dec 1, 2026 10:46 AM UTC
Answered

How to pass a set of graphics as a parameter directly into a function?

The following is correct, but my current selection may include  textboxs, so I cannot directly input the graphics into toLayer (graphics)?

var d = app.activeDocument;
var graphics = getGraphics(d.selection);
var items = d.selection;
toLayer(items)

Do I have to convert to the current selection?

But this won't work, how can I directly convert the graphics to the current selection?

var d = app.activeDocument;
var graphics = getGraphics(d.selection);
var items = d.selection;
toLayer(graphics)

More code:

var d = app.activeDocument;
var graphics = getGraphics(d.selection);
var items = d.selection;
toLayer(graphics)

//var items = d.selection;
//toLayer(items)

function toLayer(sel) {
var myLayer = app.activeDocument.layers.add({ name: "AAA" });
try {
//var topLayer = d.layers.firstItem();
for (j = 0; j < sel.length; j++) {
sel[j].itemLayer = myLayer;
}
} catch (e) { };
}

//GetImg by m1b
function getGraphics(items) {
var graphics = [];
if ('Array' === items.constructor.name) {
for (var i = 0; i < items.length; i++)
graphics = graphics.concat(getGraphics(items[i]));
}
else if (
items.hasOwnProperty('allGraphics')
&& items.allGraphics.length > 0
)
graphics = graphics.concat(items.allGraphics);
else if (
items.hasOwnProperty('allPageItems')
&& items.allPageItems.length > 0
)
graphics = graphics.concat(getGraphics(items.allGraphics));
else if ('Image' === items.constructor.name) {
graphics.push(items);
}
return graphics;
};

 

    dublove
    dubloveCorrect answer
    Legend
    Dec 1, 2026 3:33 PM UTC

    I seem to know now, my graphics are image content, not the outermost geometric shape, so I cannot move to the specified layer.

    1 reply

    dublove
    dubloveAuthorCorrect answer
    Legend
    Dec 1, 2026 3:33 PM UTC

    I seem to know now, my graphics are image content, not the outermost geometric shape, so I cannot move to the specified layer.

    Translate