Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

121
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!