Skip to main content
dublove
Legend
March 31, 2026
Answered

After running the function, how can I ensure that only a specific object remains selected?

  • March 31, 2026
  • 4 replies
  • 54 views

Currently, I have selected an image and a text frame.

 I want only the image to remain selected after the function executes.


Here is the code:

I want only the selected image to remain after executing “exit();”.

Thank you very much.

var doc = app.activeDocument;
var item = doc.selection[0];
var items = doc.selection;
if (items.length > 1) {
var sel = items;
tx = 0
for (i = 0; i < sel.length; i++) {
if ('TextFrame' == sel[i].constructor.name) {
tx = sel[i];
}
if ('TextFrame' != sel[i].constructor.name) {
ob = sel[i]
}
}

if (tx != 0) {
getA();
exit();
}
}

function getA() {
var a = 2 + 3;
}

 

    Correct answer m1b

    Hi ​@dublove you can set the selection to whatever you want. Like this:

    doc.selection = [item];

     

    4 replies

    dublove
    dubloveAuthor
    Legend
    April 1, 2026

    Thanks, I've saved it to my device.

    rob day
    Community Expert
    Community Expert
    March 31, 2026

    or, use == “TextFrame” if you want to leave the image selected

    rob day
    Community Expert
    Community Expert
    March 31, 2026

    Also this deselects any item in the selection array that is not a textFrame

     

    var doc = app.activeDocument;
    var items = doc.selection;
    for (var i = 0; i < items.length; i++){
    if (items[i].constructor.name != "TextFrame") {
    //removes a selected object from the selection (items) if it is not a TextFrame
    doc.select(items[i], SelectionOptions.REMOVE_FROM)
    }
    };

     

    m1b
    Community Expert
    m1bCommunity ExpertCorrect answer
    Community Expert
    March 31, 2026

    Hi ​@dublove you can set the selection to whatever you want. Like this:

    doc.selection = [item];

     

    dublove
    dubloveAuthor
    Legend
    April 1, 2026

    Hi m1b.

    What is the command to bring an element to the top? I want to bring `doc.selection` to the top.

    I'm sorry, I understand now.

                ob.bringForward();

                ob.bringToFront();