Skip to main content
tokuredit
Inspiring
February 19, 2020
Answered

Script to open the last closed file.

  • February 19, 2020
  • 3 replies
  • 2045 views

Hello! How to recover "open" only the last closed document.
It's possible?
Thanks.

This topic has been closed for replies.
Correct answer SuperMerlin

You could try....

#target photoshop;
app.bringToFront();

var f = File(recentFilePaths()[0]);
if(f.exists) open(f); else alert("Sorry " + f.fsName + " does not now exit");

function recentFilePaths(){
var names = [];
var ref = new ActionReference();
ref.putProperty( charIDToTypeID("Prpr"), stringIDToTypeID("recentFiles") );
ref.putEnumerated( charIDToTypeID("capp"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") ); 
var desc = executeActionGet(ref).getList(stringIDToTypeID("recentFiles"));
for(var a = 0;a<desc.count;a++){
    names.push(desc.getPath(a));
    }
return names;
};

3 replies

tokuredit
tokureditAuthor
Inspiring
February 21, 2020

Man, that's all I need! SuperMerlin you were phenomenal. How nice to know that you still go to and share your knowledge with this modified community. Eternally grateful.

SuperMerlin
SuperMerlinCorrect answer
Inspiring
February 20, 2020

You could try....

#target photoshop;
app.bringToFront();

var f = File(recentFilePaths()[0]);
if(f.exists) open(f); else alert("Sorry " + f.fsName + " does not now exit");

function recentFilePaths(){
var names = [];
var ref = new ActionReference();
ref.putProperty( charIDToTypeID("Prpr"), stringIDToTypeID("recentFiles") );
ref.putEnumerated( charIDToTypeID("capp"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") ); 
var desc = executeActionGet(ref).getList(stringIDToTypeID("recentFiles"));
for(var a = 0;a<desc.count;a++){
    names.push(desc.getPath(a));
    }
return names;
};
Chuck Uebele
Community Expert
Community Expert
February 20, 2020

You would most likely have to write a script to keep track of what was last closed. So you would run the script to close the file, then it would write the file's info to a csv file, which could then be accessed to open it with a different script.

tokuredit
tokureditAuthor
Inspiring
February 20, 2020

This is a topic never addressed, never seen in any community.
Interesting your idea Chuck Uebele, but I would never be able to do it alone, I know the least about scripting, it would be a great impolite of me to abuse your kindness to achieve this!
A good alternative would be:
If I open a file and then close it, it has been registered in the menu list "Open Recent". I would only need the first item on that list.


I entered this modified script: It didn't work for me: Window 10 Photoshop 21.0.0

 

 

#target photoshop
app.bringToFront();
var maxFiles = 1;
var recentItems = [];
var len = recentFiles.length;
for (var i = 0; i < len; i++)
{
    recentFile = recentFiles;
    if (recentFile.exists)
    {
        recentItems.push(recentFile);
        if (recentItems.length == maxFiles+1)
        {
            break;
        }
        app.open (File(recentItems));
    }
}

 

 

Source:
https://community.adobe.com/t5/photoshop/script-for-quot-open-recent-quot/td-p/7378756?page=1

Legend
February 20, 2020

The first item on this list is not the last closed file.