I like how this script is coming together, it is giving me the opportunity to implement some new ideas. This 1.4 version is back to a single script, there is no need for a two part script.
Hold down SHIFT when running the script to set the save location to the preference file.
Run the script WITHOUT shift to save to the location saved in the preference file.
There is now an error check if the preference file exists (it does not check if the content is valid). I have noticed that if one cancels the script while selecting the file path, it will leave the content of the preference file blank and or remove the previous file path.
/*
Export S4W sRGB JPG MMDD_ Prefix - SHIFT Key.jsx
Stephen Marsh, 23rd June 2021 - v1.4
Save for Web with Month & Day Script?
https://community.adobe.com/t5/photoshop/save-for-web-with-month-amp-day-script/td-p/12108287
Features:
* Run the script with the shift key depressed to set the save path to the preference file
* Run the script without the shift key depressed to save the JPEG file
* Previous save location written/read from a custom preference text file '_MMDD_Pref-File.txt' in the user home folder
* Check for overwriting an existing JPEG file, validation requires that the file name contains spaces, not hyphens
* Conditional conversion to sRGB ICC profile
* Removal of photoshop:DocumentAncestors metadata that may build up and bloat the template
* NOTE: JPEG Quality value = 50%
*/
#target photoshop
main();
function main() {
//////// HOLD THE SHIFT KEY DOWN TO SET THE FOLDER PATH ////////
if (ScriptUI.environment.keyboardState.shiftKey) {
// Set the save folder path
var prefFileValue = Folder.selectDialog('Select a folder to save the JPEG image to...');
// Write the last save path preference file in the user home folder
var prefFile = new File('~/_MMDD-Script_Pref-File.txt');
// Write the directory path to the pref file
// r = read mode | w = write mode | a = append | e = edit
prefFile.open('w');
prefFile.write(prefFileValue.fsName);
prefFile.close();
prefFile.encoding = 'UTF-8';
// LineFeed options
// prefFile.lineFeed = "Unix";
// prefFile.lineFeed = "Windows";
// prefFile.lineFeed = "Macintosh";
alert('Save location set to:' + '\r' + prefFileValue.fsName);
////////// IF THE SHIFT KEY WAS NOT HELD DOWN, RUN THE MAIN SCRIPT //////////
} else {
if (app.documents.length !== 0) {
if (File('~/_MMDD-Script_Pref-File.txt').exists && File('~/_MMDD-Script_Pref-File.txt').length > 0) {
// Date & time variables, this script only uses the month and date (day) variables
// community.adobe.com/t5/photoshop/photoshop-script-save-as-jpg-with-current-date-and-time/td-p/11115756
var dateObj = new Date();
//var year = dateObj.getFullYear().toString().substr(-2);
var month = ("0" + (dateObj.getMonth() + 1)).slice(-2);
var date = ("0" + dateObj.getDate()).slice(-2);
//var time = dateObj.getTime();
//var hours = dateObj.getHours();
//var minutes = dateObj.getMinutes();
//var seconds = dateObj.getSeconds();
//var completeTime = hours.toString() + minutes.toString() + seconds.toString();
//var completeDate = year.toString() + month.toString() + date.toString();
// MMDD_ prefix
var mmDDprefix = month.toString() + date.toString() + '_';
// Set the active document variable
var doc = app.activeDocument;
// Name options
// Depending on S4W filename compatibility settings, you may need to replace the space with a hyphen:
// .replace(/ /g, '-');
var docName = doc.name.replace(/\.[^\.]+$/, '').replace(/ /g, ' '); // See above
var docNameInput = prompt('Enter a filename (without extension):', mmDDprefix + docName);
var docNameOutput = docNameInput.replace(/\.[^\.]+$/, '');
// Read the last save path preference file in the user home folder
var prefFileRead = File('~/_MMDD-Script_Pref-File.txt');
// Open the pref file: r = read mode | w = write mode | a = append | e = edit
prefFileRead.open('r');
// Read the value for the save path
var prefFileValue = prefFileRead.readln();
//alert('Preference file path:' + '\r' + prefFileValue);
// JPEG S4W Options
function SaveForWeb(saveFileJPEG) {
var sfwOptions = new ExportOptionsSaveForWeb();
sfwOptions.format = SaveDocumentType.JPEG;
sfwOptions.includeProfile = true;
sfwOptions.optimized = true;
sfwOptions.quality = 50;
doc.exportDocument(saveFileJPEG, ExportType.SAVEFORWEB, sfwOptions);
}
// File path & naming
var saveFileJPEG = new File(prefFileValue + '/' + docNameOutput + '.jpg');
// Confirm overwrite - name validation is dependent on SFW filename compatibility settings
if (saveFileJPEG.exists) {
// true = 'No' as default active button
if (!confirm("File exists, overwrite: Yes or No?", true))
// Optional - swap the throw alert for " throw null; " to remove the alert message
throw alert("Script cancelled!");
}
// Conditional colour space handling
// Check for untagged image
if (doc.colorProfileType === ColorProfile.NONE) {
// alert("Untagged");
// Call the function
sRGBprocessing();
// Check for any profile name which does not contain sRGB
} else if (doc.colorProfileName.match(/\bsRGB\b/) === null) {
// else if (doc.colorProfileName !== "sRGB IEC61966-2.1")
// alert("sRGB = False");
// Call the function
sRGBprocessing();
// Check for any profile name which contains sRGB
} else if (doc.colorProfileName.match(/\bsRGB\b/) !== null) {
// else if (doc.colorProfileName === "sRGB IEC61966-2.1")
// alert("sRGB = True");
// Export
SaveForWeb(saveFileJPEG);
} else { }
function sRGBprocessing() {
// Convert to profile - sRGB (profile, intent, BPC, dither)
doc.convertProfile("sRGB IEC61966-2.1", Intent.RELATIVECOLORIMETRIC, true, false);
// Export
SaveForWeb(saveFileJPEG);
// Delete convert to sRGB history step
deleteLastHistoryStep();
function deleteLastHistoryStep() {
var iddelete = stringIDToTypeID("delete");
var desc1497 = new ActionDescriptor();
var idnull = stringIDToTypeID("null");
var ref22 = new ActionReference();
var idhistoryState = stringIDToTypeID("historyState");
var idordinal = stringIDToTypeID("ordinal");
var idlast = stringIDToTypeID("last");
ref22.putEnumerated(idhistoryState, idordinal, idlast);
desc1497.putReference(idnull, ref22);
executeAction(iddelete, desc1497, DialogModes.NO);
}
}
// As this is a template, remove photoshop:DocumentAncestors metadata that may build up and bloat the file
// https://prepression.blogspot.com/2017/06/metadata-bloat-photoshopdocumentancestors.html
deleteDocumentAncestorsMetadata();
function deleteDocumentAncestorsMetadata() {
// https://forums.adobe.com/message/8456985#8456985
if (ExternalObject.AdobeXMPScript === undefined) ExternalObject.AdobeXMPScript = new ExternalObject("lib:AdobeXMPScript");
var xmp = new XMPMeta(activeDocument.xmpMetadata.rawData);
xmp.deleteProperty(XMPConst.NS_PHOTOSHOP, "DocumentAncestors");
app.activeDocument.xmpMetadata.rawData = xmp.serialize();
}
// Optional end of script message, comment out or remove as required
alert(docNameOutput + '.jpg' + '\r' + 'Saved to:' + '\r\r' + prefFileValue);
// Optional end of script alert, comment out or remove as required
// app.beep();
} else {
app.beep();
alert("Save location doesn't exist! \r First hold down SHIFT while executing this script, then run the script again WITHOUT the shift key being held");
}
} else {
app.beep();
alert('You must have a document open to use this script!');
}
}
}