Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

116
Vistas
Check if the selection contains graphics

I'm trying to write a script where part of it is to find out if there is an image in the objects selected, or the objects are empty shapes.

I need to check if the files of the selection are all images, all shapes or are mixed.

I've put together this code with alerts (the final code will not be with alerts, it's for understanding purposes):

    if (app.selection.length == 0) {
        alert ("Nothing selected");
        }
    else {
        for (var i = 0; i < app.selection.length; i++) {
            if ((app.selection[i].graphics.length == 0) && (app.selection[i].graphics.length == 1)) {
                alert ("The files are mixed");
                }
            else if (app.selection[i].graphics.length == 1) {
                alert ("The files are images");
                }
            else if (app.selection[i].graphics.length == 0) {
                alert ("The files are shapes");
                }
            }
        }

Obviously the code is not working as intended (it works for the images and the shapes, while I already know the mixed part it's not written correctly), since my searches have been inconclusive.

What I'm looking for is: if nothing is selected (do something), else if the files are shapes and images (do something only to the shapes), else if there are only images (do something), else if there are only shapes (do something).

Can someone give me an idea on how to do it?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Basically the algo can be something like this:

var sel = app.selection;

if (sel.length == 0) alert('Nothing selected');

else {

    var images = sel.pop().graphics.length > 0;
    var mixed = false;

    while (sel.length) {
        if ((sel.pop().graphics.length > 0) != images) {
            mixed = true;
            break;
        }
    }

    if (mixed) alert('Mixed');
    else {
        if (images) alert('Images');
        else alert('Shapes');
    }
}

But it doesn't handle groups.

Update

If you just want to get shapes (and omit images) from the selection it can be done this way:

var sel = app.selection;
if (sel.length == 0) { alert('Nothing selected'); exit() }

var shapes = [];
for (var i=0; i<sel.length; i++)
    if (sel[i].graphics.length == 0) shapes.push(sel[i]);

if (shapes.length > 0) do_something(shapes);

// ----------------------------------------------------------------------
function do_something(shapes) {
    alert('Here we go...');
    app.selection = shapes;

    // do stuff...
}

Or a short variant (just to deselect images):

var sel = app.selection;
for (var i=0; i<sel.length; i++)
    if (sel[i].graphics.length > 0) sel[i].select(SelectionOptions.REMOVE_FROM);

Update 2

Here is the code that does as follows:

  • if nothing selected > creates a white rectangle,
  • if only images selected > creates a white rectangle,
  • if only shapes selected > colors the shapes white,
  • if the selection is mixed > selects only the shapes and colors white.
var sel = app.selection;

if (sel.length == 0) fn_nothing();
else {
    var images = sel.pop().graphics.length > 0;
    var mixed = false;
    while (sel.length) {
        if ((sel.pop().graphics.length > 0) != images) {
            mixed = true;
            break;
        }
    }
    if (mixed) fn_mixed();
    else {
        if (images) fn_images();
        else fn_shapes();
    }
}

function fn_nothing() {
    alert('Nothing');
    create_white_rectangle();
}

function fn_images() {
    alert('Images');
    create_white_rectangle();
}

function fn_shapes() {
    alert('Shapes');
    color_shapes_white();
}

function fn_mixed() {
    alert('Mixed');
    select_shapes();
    color_shapes_white();
}

function select_shapes() {
    var sel = app.selection;
    while (sel.length) {
        var item = sel.pop();
        if (item.graphics.length > 0)
        item.select(SelectionOptions.REMOVE_FROM)
    }
    alert('Select shapes');
}

function color_shapes_white() {
    alert('Color shapes white')
}

function create_white_rectangle() {
    alert('Create a white rectangle');
}
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda