Skip to main content
Inspiring
October 6, 2021
Answered

How to change the order of open documents for an action?

  • October 6, 2021
  • 3 replies
  • 2433 views

Is there a way  to make a specific document   "previouce" one for an action execution?   It seems Phs  consider the documents in order of their opening  .   So if I execute an action that create a copy of a documnt and then go to "previous" one  and I happen to have another file  opened some time  after the the original doc where I have started my action execution   the action returns to wrong document.   Is there a way to change  that order somehow  without doing save, close and open again  my initial doc? 

This topic has been closed for replies.
Correct answer jazz-y

another way (all-in-one script):

 

 

#target photoshop

/*
<javascriptresource>
<name>Target document</name>

<eventid>a4a27371-cedc-4af8-a6d1-f25765eca0cb</eventid>
<enableinfo>true</enableinfo>
<terminology><![CDATA[<< /Version 1 
                         /Events << 
                          /a4a27371-cedc-4af8-a6d1-f25765eca0cb [(Target document)<<
                          /select [(Mode) /string]
                          >>] 
                         >> 
                      >> ]]></terminology>
</javascriptresource>
*/

var s2t = stringIDToTypeID,
    isCancelled = false,
    UUID = 'a4a27371-cedc-4af8-a6d1-f25765eca0cb',
    id = null;

const REMEMBER = 'remebmer current document',
    SELECT = "select remembered document";

try { d = getCustomOptions(UUID) } catch (e) { }
if (d != undefined) id = d.getInteger(s2t('documentID'))

if (!app.playbackParameters.count) {
    var w = buildWindow(), result = w.show()
    switch (result) {
        case 0: saveSettings(result, true, true); break;
        case 1: selectDocumentByID(id, true); saveSettings(result, false, true); break;
        default: isCancelled = true;
    }
}
else {
    var d = app.playbackParameters,
        mode = d.getString(s2t('select'));
    if (app.playbackDisplayDialogs == DialogModes.ALL) {
        var w = buildWindow(mode == SELECT), result = w.show()
        if (result == 2) { isCancelled = true } else {
            if (result) {
                selectDocumentByID(id)
                saveSettings(result, false, true)
            } else {
                saveSettings(result, true, true)
            }
        }
    }

    if (app.playbackDisplayDialogs != DialogModes.ALL) {
        if (mode == SELECT) selectDocumentByID(id) else {
            saveSettings(0, true, false)
        }
    }
}

isCancelled ? 'cancel' : undefined

function buildWindow(sel) {
    var u = undefined,
        d = new Window("dialog {text: 'Target document' }"),
        dl = d.add("dropdownlist", u, u, { items: [REMEMBER, SELECT] }),
        g = d.add("group{orientation : 'row'}"),
        bnOk = g.add("button {text:'Ok'}", u, u, { name: "ok" }),
        bnCancel = g.add("button {text : 'Cancel'}", u, u, { name: "cancel" });
    dl.selection = sel != undefined ? sel : 0;
    bnOk.onClick = function () { w.close(dl.selection.index) }
    return d;
}

function saveSettings(mode, renewId, renewMode) {
    if (renewId) putCustomOptions(UUID, getDocumentID(), false);
    if (renewMode) {
        (d = new ActionDescriptor()).putString(s2t('select'), mode ? SELECT : REMEMBER);
        playbackParameters = d;
    }

}

function selectDocumentByID(id, silentMode) {
    if (id) {
        (r = new ActionReference()).putIdentifier(s2t('document'), id);
        (d = new ActionDescriptor()).putReference(s2t('target'), r);
        try { executeAction(s2t('select'), d, DialogModes.NO) } catch (e) { if (!silentMode) alert('Remembered document is not avaliable!') }
    }
    else { alert('No remembered document!') }
}

function getDocumentID() {
    (r = new ActionReference()).putProperty(s2t('property'), p = s2t('documentID'));
    r.putEnumerated(s2t('document'), s2t('ordinal'), s2t('targetEnum'));
    return executeActionGet(r);
}

 

3 replies

jazz-yCorrect answer
Legend
October 7, 2021

another way (all-in-one script):

 

 

#target photoshop

/*
<javascriptresource>
<name>Target document</name>

<eventid>a4a27371-cedc-4af8-a6d1-f25765eca0cb</eventid>
<enableinfo>true</enableinfo>
<terminology><![CDATA[<< /Version 1 
                         /Events << 
                          /a4a27371-cedc-4af8-a6d1-f25765eca0cb [(Target document)<<
                          /select [(Mode) /string]
                          >>] 
                         >> 
                      >> ]]></terminology>
</javascriptresource>
*/

var s2t = stringIDToTypeID,
    isCancelled = false,
    UUID = 'a4a27371-cedc-4af8-a6d1-f25765eca0cb',
    id = null;

const REMEMBER = 'remebmer current document',
    SELECT = "select remembered document";

try { d = getCustomOptions(UUID) } catch (e) { }
if (d != undefined) id = d.getInteger(s2t('documentID'))

if (!app.playbackParameters.count) {
    var w = buildWindow(), result = w.show()
    switch (result) {
        case 0: saveSettings(result, true, true); break;
        case 1: selectDocumentByID(id, true); saveSettings(result, false, true); break;
        default: isCancelled = true;
    }
}
else {
    var d = app.playbackParameters,
        mode = d.getString(s2t('select'));
    if (app.playbackDisplayDialogs == DialogModes.ALL) {
        var w = buildWindow(mode == SELECT), result = w.show()
        if (result == 2) { isCancelled = true } else {
            if (result) {
                selectDocumentByID(id)
                saveSettings(result, false, true)
            } else {
                saveSettings(result, true, true)
            }
        }
    }

    if (app.playbackDisplayDialogs != DialogModes.ALL) {
        if (mode == SELECT) selectDocumentByID(id) else {
            saveSettings(0, true, false)
        }
    }
}

isCancelled ? 'cancel' : undefined

function buildWindow(sel) {
    var u = undefined,
        d = new Window("dialog {text: 'Target document' }"),
        dl = d.add("dropdownlist", u, u, { items: [REMEMBER, SELECT] }),
        g = d.add("group{orientation : 'row'}"),
        bnOk = g.add("button {text:'Ok'}", u, u, { name: "ok" }),
        bnCancel = g.add("button {text : 'Cancel'}", u, u, { name: "cancel" });
    dl.selection = sel != undefined ? sel : 0;
    bnOk.onClick = function () { w.close(dl.selection.index) }
    return d;
}

function saveSettings(mode, renewId, renewMode) {
    if (renewId) putCustomOptions(UUID, getDocumentID(), false);
    if (renewMode) {
        (d = new ActionDescriptor()).putString(s2t('select'), mode ? SELECT : REMEMBER);
        playbackParameters = d;
    }

}

function selectDocumentByID(id, silentMode) {
    if (id) {
        (r = new ActionReference()).putIdentifier(s2t('document'), id);
        (d = new ActionDescriptor()).putReference(s2t('target'), r);
        try { executeAction(s2t('select'), d, DialogModes.NO) } catch (e) { if (!silentMode) alert('Remembered document is not avaliable!') }
    }
    else { alert('No remembered document!') }
}

function getDocumentID() {
    (r = new ActionReference()).putProperty(s2t('property'), p = s2t('documentID'));
    r.putEnumerated(s2t('document'), s2t('ordinal'), s2t('targetEnum'));
    return executeActionGet(r);
}

 

kirkr5689Author
Inspiring
October 8, 2021

Thanks jazz-y.   Works perfectly fine too

Legend
October 6, 2021

kirkr5689Author
Inspiring
October 6, 2021

That doesn't work because in action panel  Photoshop  always record  previous document  or document-2 or -3  etc  in order those documents were opened.     

I basically want to make a collapsed/merged down  copy of a document  then go back to original  and save selection as new channel to that copy   without adding same channel in the oiginal.           It works only if you didn't open any third file  after you opened the original .

I also don't want it to use specifically named documents    since I need it to work for any file . 

 

 As of using script you mean writing  a javascript , right?  I am affraid it's beyond my knowlege.      I just hoped there is a way  to change  the order Photoshop counts  open document .  Like the order  docs are stacked  in Phs window instead ?

Chuck Uebele
Community Expert
Community Expert
October 7, 2021

If you want to go back to the first document open, use this script. Just save it as a plain text file with the extension .jsx. Then you can create an action and record playing this script, in that action. If you want the second document, replace the app.documents[0] with [1].

 

#target photoshop
try {app.activeDocument = app.documents[0]}
catch(e){};
JJMack
Community Expert
Community Expert
October 6, 2021

In Actions you have found out you need to know what is going on and be careful using previous and next document when you add document into Photoshop. With scripting you can verify you are in the right document be by checking the document's name. 

JJMack
Chuck Uebele
Community Expert
Community Expert
October 6, 2021

I would agree with JJ on using scripts. You can open doc, assign them to a variable that will be easy to refer to. However, if you're doing lots of work between needing to run an action or script, you would need to write the variables to a text file, for later reference. Maybe a global variable might work, but I haven't tried that.