• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

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

Enthusiast ,
Oct 06, 2021 Oct 06, 2021

Copy link to clipboard

Copied

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? 

TOPICS
Actions and scripting , Windows

Views

1.1K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 2 Correct answers

Community Expert , Oct 07, 2021 Oct 07, 2021

So maybe this will work. It consist of two scripts. One is to store the name of the doc that you want to return to, on your desktop, in a text file. The second script will read that text file then select the open document that has that name. So either in your action, or before you run your action, you need to have whatever document that you want to return to active and run the first script. Then in your action, place the second script where you want PS to make that the active document again.

 

F

...

Votes

Translate

Translate
Guide , Oct 07, 2021 Oct 07, 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]
                          >>] 
                         >> 
                      >> ]]></ter
...

Votes

Translate

Translate
Adobe
Community Expert ,
Oct 06, 2021 Oct 06, 2021

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 06, 2021 Oct 06, 2021

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Oct 06, 2021 Oct 06, 2021

Copy link to clipboard

Copied

2021-10-06_21-08-17.png2021-10-06_21-08-34.png

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Oct 06, 2021 Oct 06, 2021

Copy link to clipboard

Copied

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 ?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 06, 2021 Oct 06, 2021

Copy link to clipboard

Copied

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){};

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 06, 2021 Oct 06, 2021

Copy link to clipboard

Copied

Adding to Chuck's post, just in case you need to navigate to the last open doc, the code would be:

 

#target photoshop
var lastDoc = app.documents.length -1;
app.activeDocument = app.documents[lastDoc];

 

It can be helpful to add small "helper scripts" into an action to achieve something that an action can't do, while you maintain control and flexibility of the action steps.

 

Downloading and Installing Adobe Scripts

 

 

Additinally, it can be useful at times to create a target with a fixed, known name...

 

If you duplicated to a temp document with a specific target name of:

 

myTempDoc

 

Then you can use the following code to select that specific file, whether or not it is the last doc or not:

#target photoshop
app.activeDocument = app.documents.getByName("myTempDoc");

P.S. Sometimes it is just easier to work with layers or smart layers so that the original/source document retains focus.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Oct 07, 2021 Oct 07, 2021

Copy link to clipboard

Copied

Thanks guys for trying to help .     I like Chuks script , thatnks a lot for it,  still I have to set  number it the script text ech time I work with a specific document   . I guess I can get this number from "window" menu and paste  to the script text .     

 

Still  I wonder if it could be working  by returning to previouce document  that had been in focus before dublcating   with no regard  to what order number it might have?        Or Photoshop just doesn't keept track of it and the  opening order is only way?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 07, 2021 Oct 07, 2021

Copy link to clipboard

Copied

It's a simple task to add a GUI to Chuck's code so that a digit can be typed in when the action runs the script.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Oct 07, 2021 Oct 07, 2021

Copy link to clipboard

Copied

Ideally it should be a kind of action step that remembers  the order number of initial document  to return back later  and Chuk's code get that number somehow

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 07, 2021 Oct 07, 2021

Copy link to clipboard

Copied

It's starting to feel as if the fix is getting more complicated than the problem :]

I'm off to bed, I'll check back tomorrow.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 07, 2021 Oct 07, 2021

Copy link to clipboard

Copied

So maybe this will work. It consist of two scripts. One is to store the name of the doc that you want to return to, on your desktop, in a text file. The second script will read that text file then select the open document that has that name. So either in your action, or before you run your action, you need to have whatever document that you want to return to active and run the first script. Then in your action, place the second script where you want PS to make that the active document again.

 

First script to store the doc name:

#target photoshop

var fileName = app.activeDocument.name
saveFile()
function saveFile(){
    file = new File('~/desktop/docOrder.txt');

    file.encoding = 'UTF8';
    file.open('w');
    file.write(fileName);
    file.close();
    }

 

Second script to read the text file and make the doc active again.

#target photoshop
var str
var docFile = new File('~/desktop/docOrder.txt');
readTextFild (docFile)
try{app.activeDocument = app.documents.getByName (str);}
catch(e){alert(e)}
function readTextFild(textFile){
    if(textFile.exists){
        textFile.encoding = "UTF8";
        textFile.lineFeed = "unix";
        textFile.open("r", "TEXT", "????");
        str = textFile.read();
        textFile.close(); 
        }
    }

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Oct 07, 2021 Oct 07, 2021

Copy link to clipboard

Copied

Thanks Chuck. The solution works  perfectly  .

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 07, 2021 Oct 07, 2021

Copy link to clipboard

Copied

Excellent!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Oct 07, 2021 Oct 07, 2021

Copy link to clipboard

Copied

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);
}

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Oct 08, 2021 Oct 08, 2021

Copy link to clipboard

Copied

LATEST

Thanks jazz-y.   Works perfectly fine too

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines