@Rodan_Shiner
I was delayed by some unexpected errors and debugging when I used your PNG save code, so I went back to my previous code. I also got bogged down a bit in exploring some edge-case error checking, hoping to improve on the code used in earlier scripts. It is all in one script, there is no need for two separate scripts, but that is OK too if you prefer it that way (I used to do that until I found out about keyboardState). The ALT/OPT key has to be depressed while the script is run to set the save location. Thereafter, the save location will continue to be used until the script is run again with the ALT/OPT key held down.
Anyway, please try the following v1.0 code.
/*
Save PNG to Preference Log File Location.jsx
https://community.adobe.com/t5/photoshop-ecosystem-discussions/script-to-save-as-png-file-with-a-specific-name-and-put-it-in-a-specific-folder/td-p/13233189
v1.0 30th September 2022 - Stephen Marsh
*/
#target photoshop
if (app.documents.length) {
try {
/***** HOLD THE ALT/OPT KEY DOWN TO SET THE FOLDER PATH *****/
if (ScriptUI.environment.keyboardState.altKey) {
// Set the save folder path
try {
var prefFilePath = Folder.selectDialog('Select a folder to save the PNG image to:');
} catch (e) {}
// Log text file platform specific line feed options
var os = $.os.toLowerCase().indexOf("mac") >= 0 ? "mac" : "windows";
if (os === "mac") {
prefFileOutLF = "Unix"; // Legacy = "Macintosh"
} else {
prefFileOutLF = "Windows";
}
// Create the preference file in the user home folder
var prefFileOut = new File('~/_Last_PNG_Save_Path.txt');
var dateTime = new Date().toLocaleString();
if (prefFileOut.exists)
prefFileOut.remove();
prefFileOut.open("w");
prefFileOut.encoding = "UTF-8";
prefFileOut.lineFeed = prefFileOutLF;
prefFileOut.write(dateTime + "\r" + app.activeDocument.name + "\r");
prefFileOut.writeln(prefFilePath);
prefFileOut.close();
// Call the main PNG save function directly after setting the path
savePNGfile();
/***** IF THE ALT/OPT KEY WAS NOT HELD DOWN, RUN THE MAIN SCRIPT *****/
} else {
// Call the main PNG save function
savePNGfile();
}
} catch (e) {}
} else {
alert("A document must be open to use this script!");
}
function savePNGfile() {
var prefFileIn = File('~/_Last_PNG_Save_Path.txt');
if (File(prefFileIn).exists && File(prefFileIn).length > 0) {
// Read the preference file
prefFileIn.open('r');
// Read (skip) the first two lines from the log file, a means to an end...
var skipLineOne = prefFileIn.readln(1);
var skipLineTwo = prefFileIn.readln(2);
// Read the 3rd line from the preference file (file path)
var prefFilePathValue = prefFileIn.readln(3);
prefFileIn.close();
if (prefFilePathValue.length === 0) {
alert("The directory path is empty. Run the script again while holding down the ALT/OPT key!");
}
// Save as PNG
var layerName = app.activeDocument.activeLayer.name;
var savePNG = new File(prefFilePathValue + "/" + layerName + '.png');
var saveOptions = new PNGSaveOptions();
saveOptions.compression = 0; // compression value: 0-9
saveOptions.interlaced = false;
activeDocument.saveAs(savePNG, saveOptions, true, Extension.LOWERCASE);
app.beep();
} else {
app.beep();
alert('There is no valid file named "_Last_PNG_Save_Path.txt" in the user home folder!' + '\r' + 'Run the script again while holding down the ALT/OPT key.');
}
}