Scripts to Save & Restore Photoshop Sessions
Inspiration for the following two scripts came from these ideas/feature requests:
The scripts save and close all open files, logging their locations into a text file. Once you have finished with the unexpected task that interrupted the previous session, you can then run the second restore script to reopen all of the files from the previous session and continue. The active document from the previous session will be active when the docs are re-opened.
Window sizes and positions, pan, zoom levels and other properties are not restored, the docs will simply re-open "as is".
/*
Photoshop Session - 1 of 2 - Save.jsx
v1.0 - 11th November 2023, Stephen Marsh
https://community.adobe.com/t5/photoshop-ecosystem-ideas/please-session-saving/idc-p/14169472
https://community.adobe.com/t5/photoshop-ecosystem-ideas/restore-previous-session/idc-p/14189928
*/
#target photoshop
try {
if (app.documents.length > 0) {
if (confirm("Log all open session files and save close all documents?", false)) {
var logFile = new File(app.preferencesFolder + "/" + "Photoshop Session Document Paths.log");
if (logFile.exists)
logFile.remove();
//alert(logFile.fsName);
var actDocLogFile = new File(app.preferencesFolder + "/" + "Photoshop Session Active Document.log");
if (actDocLogFile.exists)
actDocLogFile.remove();
writeActiveDocToLog();
//alert(actDocLogFile.fsName);
while (app.documents.length > 0) {
app.activeDocument = app.documents[0]; // Force the write to log from left to right document tabs, comment out to reverse the open order
try {
activeDocument.path;
if (ExternalObject.AdobeXMPScript === undefined) ExternalObject.AdobeXMPScript = new ExternalObject('lib:AdobeXMPScript');
var xmp = new XMPMeta(activeDocument.xmpMetadata.rawData);
var crsMeta = xmp.getProperty(XMPConst.NS_CAMERA_RAW, "Version");
if ((/\.(RAF|CR2|CR3|NRW|ERF|RW2|NEF|ARW|RWZ|EIP|DNG|BAY|DCR|RAW|CRW|3FR|K25|KC2|MEF|DNG|CS1|ORF|ARI|SR2|MOS|CR3|GPR|SRW|MFW|FFF|SRF|KDC|MRW|J6I|RWL|X3F|PEF|IIQ|CXI|NKSC|MDC)$/i).test(activeDocument.fullName) === true && crsMeta !== undefined) {
saveAsDefault();
writePathToLog();
activeDocument.close(SaveOptions.DONOTSAVECHANGES);
} else if (activeDocument.path) {
writePathToLog();
activeDocument.close(SaveOptions.SAVECHANGES);
}
} catch (e) {
executeAction(stringIDToTypeID("save"), undefined, DialogModes.ALL);
writePathToLog();
activeDocument.close(SaveOptions.SAVECHANGES);
}
}
}
} else {
alert('A document must be open when running this script!');
}
} catch (e) {}
///// FUNCTIONS /////
function writePathToLog() {
var os = $.os.toLowerCase().indexOf("mac") >= 0 ? "mac" : "windows";
if (os === "mac") {
var logFileLF = "Unix";
} else {
logFileLF = "Windows";
}
logFile.open("a");
logFile.encoding = "UTF-8";
logFile.lineFeed = logFileLF;
logFile.writeln(activeDocument.path + "/" + activeDocument.name);
logFile.close();
}
function writeActiveDocToLog() {
var os = $.os.toLowerCase().indexOf("mac") >= 0 ? "mac" : "windows";
if (os === "mac") {
var logActDocLogLF = "Unix";
} else {
logActDocLogLF = "Windows";
}
actDocLogFile.open("a");
actDocLogFile.encoding = "UTF-8";
actDocLogFile.lineFeed = logActDocLogLF;
actDocLogFile.writeln(activeDocument.name);
actDocLogFile.close();
}
function saveAsDefault() {
function s2t(s) {
return app.stringIDToTypeID(s);
}
var descriptor = new ActionDescriptor();
descriptor.putObject(s2t("as"), s2t("photoshop35Format"), descriptor);
descriptor.putPath(s2t("in"), new File("~/Desktop" + "/" + activeDocument.name));
descriptor.putBoolean(s2t("lowerCase"), true);
executeAction(s2t("save"), descriptor, DialogModes.ALL);
}
/*
Photoshop Session - 2 of 2 - Restore.jsx
v1.0 - 11th November 2023, Stephen Marsh
https://community.adobe.com/t5/photoshop-ecosystem-ideas/please-session-saving/idc-p/14169472
https://community.adobe.com/t5/photoshop-ecosystem-ideas/restore-previous-session/idc-p/14189928
*/
#target photoshop
try {
var theLogFilePath = readPref(app.preferencesFolder + "/" + "Photoshop Session Document Paths.log");
if (File(app.preferencesFolder + "/" + "Photoshop Session Document Paths.log").exists && File(app.preferencesFolder + "/" + "Photoshop Session Document Paths.log").length > 0) {
//alert(app.preferencesFolder.fsName + "/" + "Photoshop Session Document Paths.log");
for (var m = 0; m < theLogFilePath.length; m++) {
open(File(theLogFilePath[m]));
}
} else {
alert("The Desktop log file 'Photoshop Session Document Paths.log' doesn't exist or is empty!");
}
} catch (e) { }
//app.runMenuItem(stringIDToTypeID('consolidateAllTabs'));
//app.runMenuItem(stringIDToTypeID('floatAllWindows'));
//app.runMenuItem(stringIDToTypeID('tileVertically'));
//app.runMenuItem(stringIDToTypeID('tileHorizontally'));
try {
if (File(app.preferencesFolder + "/" + "Photoshop Session Active Document.log").exists && File(app.preferencesFolder + "/" + "Photoshop Session Active Document.log").length > 0) {
//alert(app.preferencesFolder + "/" + "Photoshop Session Active Document.log");
var theActDocLogFile = new File(app.preferencesFolder + "/" + "Photoshop Session Active Document.log");
theActDocLogFile.open('r');
var theActiveDocName = theActDocLogFile.readln(1);
theActDocLogFile.close();
activeDocument = documents.getByName(theActiveDocName);
} else {
alert("The Desktop log file 'Photoshop Session Active Document.log' doesn't exist or is empty!");
}
} catch (e) {}
function readPref(thePath) {
/*
Based on:
https://community.adobe.com/t5/photoshop-ecosystem-discussions/script-to-open-multiple-docs-from-a-text-file/td-p/14148443
by c.pfaffenbichler
*/
if (File(thePath).exists === true) {
var logFile = File(thePath);
logFile.open("r");
logFile.encoding = "UTF-8";
var theString = new String;
for (var m = 0; m < logFile.length; m++) {
theString = theString.concat(logFile.readch());
}
logFile.close();
return theString.split("\n");
//return String(theString);
}
}
https://prepression.blogspot.com/2017/11/downloading-and-installing-adobe-scripts.html

