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

Save for Web with Month & Day Script?

Explorer ,
Jun 11, 2021 Jun 11, 2021

Copy link to clipboard

Copied

Hello:

 

Searching hasn't found this needed workflow.  
Not sure this is the most efficient as it's legacy, but I use Save for Web to output needed images daily. I'd like the "save as" dialog to include month & day followed by and underscore

(ex. 0611_) already included in the file name. It would output a JPG, Quality 50%, Optimized, Convert to sRGB. 

Not sure this is possible, but would appreciate any help/direction given.


Thanks!

TOPICS
Actions and scripting

Views

5.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 1 Correct answer

Community Expert , Jun 23, 2021 Jun 23, 2021

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 cance

...

Votes

Translate

Translate
Adobe
Explorer ,
Jun 19, 2021 Jun 19, 2021

Copy link to clipboard

Copied

Thanks, Stephen, for testing. 
Turns out it wasn't the script, but the fit image command I was using. 
Your script is working great!

 

Thanks again,

 

Rob

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 ,
Jun 19, 2021 Jun 19, 2021

Copy link to clipboard

Copied

Thanks for the feedback and thanks...

 

So, you can use the 1.1 version without issue, just like I was when testing?

 

Or, the 1.2 version that writes/reads the last selected location from text file works better?

 

Please mark the script that works best for you as the "correct answer".

 

EDIT: Please try the new 1.3 (two scripts)...

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
Explorer ,
Jun 22, 2021 Jun 22, 2021

Copy link to clipboard

Copied

The 1.1 script works as tested.
The 1.2 script seems to work just like the 1.1. It asks everytime where to save, instead of remembering last saved.

Rob

Get Outlook for iOS

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 ,
Jun 22, 2021 Jun 22, 2021

Copy link to clipboard

Copied

Thanks for the reply Rob, let's recap:

 

1.1 worked fine for me too, I tested on both Mac and Win with the same results. I couldn't reproduce your issue of the last used folder being forgotten.

 

1.2 was created to use a preference text file to force the folder path and name to use, as on your system the last used folder was not remembered. This was apparently something to do with the fit image command?

 

I had two choices with my limited understanding of scripting:

 

* Create a single script to both set and open the last used folder

 

* Create two separate scripts... The first script would be used to initially set the folder path to the preference file, nothing more. The second script would then do everything that the 1.1 version did, except to look at the pref. file for the path to the last used folder.

 

I decided on the first option as it seemed more practical.

 

When it asks where to save, it is using that location to write to the log file so that next time there is a folder path in the pref file.

 

The issue is that both Photoshop and the 1.2 script both do the same thing, remember the last used folder! The result should be the same on a system that is working correctly. The 1.2 script was developed to solve a problem that shouldn't exist.

 

If you want to verify this as I did many times while developing the script, after running the 1.2 version for the first time, open the pref. text file in your user home directory and change the directory path to another valid path and close/save. Next time you run the script it will default to the folder path that you manually edited, not the last used folder in Photoshop.

 

EDIT: Please try the new 1.3 (two scripts)...

 

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 ,
Jun 22, 2021 Jun 22, 2021

Copy link to clipboard

Copied

At the moment both 1.1 and 1.2 open a dialog at the last save location, which allows you to navigate to a new location if required.

 

I just re-read your post... Do you mean that the expectation is that the script saves to the last folder that the script previously saved to, without opening a dialog?

 

Is your expectation to always save to the same folder without a dialog window? If so, it is easy to just set a hard-coded save location in the script rather than a variable one.

 

Perhaps the two step script would be better for you? Then you can just run the script to log the new save location when needed.

 

EDIT: Please try the new 1.3 (two scripts)...

 

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 ,
Jun 14, 2021 Jun 14, 2021

Copy link to clipboard

Copied

Maybe I'm being daft but your operating system already does this automatically.  Each file has an associated date & time stamp.  You can also sort files by Date Modified or Date Created. See screenshot from Windows File Explorer.

 

image.png

 

Likewise, Dreamweaver has an option to show date & time stamp from the Files Panel (F8) so you can sort your assets accordingly.

image.png

 

Nancy O'Shea— Product User, Community Expert & Moderator
Alt-Web Design & Publishing ~ Web : Print : Graphics : Media

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
Explorer ,
Jun 15, 2021 Jun 15, 2021

Copy link to clipboard

Copied

Hi Nancy-My need isn't viewing metadata, it's writing it to the file name that's created. 
I'm just trying to automate the parts of the process that can be, to save some time and accuracy. 
After seeing what Stephen put together, I'm realizing I need to learn JavaScript! 🤔

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 ,
Jun 22, 2021 Jun 22, 2021

Copy link to clipboard

Copied

Please try these 1.3 versions.

 

Step 1:

Run the Step 1 script to write the desired save location to the log preference text file. This location will be used by the second script without offering a dialog for a save location. When you need a new save location, run the Step 1 script again.

 

 

/* 

Export S4W sRGB JPG MMDD_ Prefix - Step 1.jsx
Stephen Marsh, 23rd June 2021 - v1.3

Save for Web with Month & Day Script?
https://community.adobe.com/t5/photoshop/save-for-web-with-month-amp-day-script/td-p/12108287

///// Create the preference log file /////

*/

#target photoshop

// Set the save folder path
var docPath = 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.pref');
// Write the directory path to the pref file
// r = read mode | w = write mode | a = append | e = edit
prefFile.open('w');
prefFile.write(docPath.fsName);
prefFile.close();
prefFile.encoding = 'UTF-8';
// LineFeed options
// prefFile.lineFeed = "Unix";
// prefFile.lineFeed = "Windows";
// prefFile.lineFeed = "Macintosh";
//alert('Preference file path:' + '\r' + docPath.fsName);

alert('Save location set to:' + '\r' + docPath.fsName);

 

 

Step 2:

Run the Step 2 script as required. When a new save location is required, run the Step 1 script again, then use the Step 2 script again until a new save location is required. Repeat as necessary.

 

 

/*

Export S4W sRGB JPG MMDD_ Prefix - Step 2.jsx
Stephen Marsh, 23rd June 2021 - v1.3

Save for Web with Month & Day Script?
https://community.adobe.com/t5/photoshop/save-for-web-with-month-amp-day-script/td-p/12108287

///// Main script to run after first setting the preference file /////

*/

#target photoshop

if (app.documents.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(/\.[^\.]+$/, '');

    // Preference File Step 1 - Read the last save path preference file in the user home folder
    // Pref file location
    var prefFileRead = File('~/_MMDD-Script_Pref-File.pref');
    // Open the pref file: r = read mode | w = write mode | a = append | e = edit
    var openPrefFile = 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 {

    alert('You must have a document open to use this script!');

}

 

 

NOTE: I have not set any error checking to test if the preference log file exists, or if it has content, or if the content is valid. Ensure that Step 1 is run at least once before running Step 2!

 

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
Explorer ,
Jun 22, 2021 Jun 22, 2021

Copy link to clipboard

Copied

Great, thanks, Stephen. 
Yes, I'm only saving to one location each day, that being a dated folder, so after I select the location once, I don't need to select another anymore that day. 
I'll try and load this up tomorrow and test it out. 

Thanks again,

 

Rob

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 ,
Jun 22, 2021 Jun 22, 2021

Copy link to clipboard

Copied

If this is what you are looking for, is running two different scripts going to be painful? If yes, I might be able to get this into a single script that you can use a modifier key such as shift to action. That way if you hold down shift when running the action it will ask you to set the save path, otherwise running the script without the shift key will just run the save part.

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 ,
Jun 23, 2021 Jun 23, 2021

Copy link to clipboard

Copied

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!');

        }
    }
}

 

 

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
Explorer ,
Jun 23, 2021 Jun 23, 2021

Copy link to clipboard

Copied

Hey Stephen-That's a great solution to the workflow!

I'll load it up tomorrow and give it a go. I was too busy today to try out your other version. 

Thanks,

 

Rob

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 ,
Jun 30, 2021 Jun 30, 2021

Copy link to clipboard

Copied

LATEST

@pandemicmonkee 

 

Is there a correct answer somewhere here?

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