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

First post :) After a script for saving files

New Here ,
Oct 14, 2019 Oct 14, 2019

Copy link to clipboard

Copied

Hi everyone,

 

This is my first post 🙂 Hoping to find some help.

Im after a script that I can add to an action that will save the opened file in two formats (PSD and JPG) and also add the files dimensions to the end of the filename. So the files will look like this for instance (image01_500x500), and ill have a PSD and JPG version.

 

If anyone is able to help that would be great 🙂

 

 

TOPICS
Actions and scripting

Views

744

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
Adobe
Community Expert ,
Oct 14, 2019 Oct 14, 2019

Copy link to clipboard

Copied

Where should the script save the two new files to?

 

#1: A fixed location such as the Desktop?

#2: In the same directory as the source file? 

#3: In a new sub-folder under the source file directory? If so what name for the folder? #3A: Is this one common sub-folder for both files, or #3B: a separate sub-folder for each file format, under a parent sub-folder?

#4A: In a new, different variable user-selected location? #4B: Optionally writing to sub-folders, as in the previous points #3A or #3B?

 

The PSD save is easy enough, however, for the JPEG, is this the equivalent of File/Save As or File/Export...? What quality level and other settings? Should the JPEG version be converted to the sRGB ICC colour profile for possible online use, maintaining the original ICC colour profile for the PSD and source file?

 

Here is some placeholder code for the naming, to be incorporated into the file saving code once the save location and options are known.

 

 

 

 

 

 

#target photoshop

var savedRuler = app.preferences.rulerUnits;        
app.preferences.rulerUnits = Units.PIXELS;

var d = app.activeDocument;
var w = d.width.toString().replace(' px', '');
var h = d.height.toString().replace(' px', '');
var dname = d.name.replace(/\.[^\.]+$/, '');
var suffix = '_' + w + 'x' + h;

alert(dname + suffix);

app.preferences.rulerUnits = savedRuler;    

 

 

 

 

 

 

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 ,
Oct 15, 2019 Oct 15, 2019

Copy link to clipboard

Copied

I have now created four script variations for the options mentioned in #1, #2, #3A and #4A above... Once I have your reply I'll be able to finish testing and post the final code.

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 ,
Oct 19, 2019 Oct 19, 2019

Copy link to clipboard

Copied

This script saves a PSD and JPEG version with a suffix to the Desktop, there is no error checking for files with the same name, use at your own risk!

 

 

 

 

// First post - After a script for saving files
// community.adobe.com/t5/Photoshop/First-post-After-a-script-for-saving-files/m-p/10669755#M269750

/* This script saves a PSD and JPEG version with a suffix to the Desktop, there is no error checking for files with the same name, use at your own risk! */

#target photoshop

/* Start Open Document Error Check - Part A: Try */
aDoc();
var originalDoc

function aDoc() {
    try {
        originalDoc = app.activeDocument
        /* Finish Open Document Error Check - Part A: Try */

        /* Main Code Start */

        // Save the current ruler units
        var savedRuler = app.preferences.rulerUnits;

        // File Naming Code
        app.preferences.rulerUnits = Units.PIXELS;
        var d = app.activeDocument;
        var w = d.width.toString().replace(' px', '');
        var h = d.height.toString().replace(' px', '');
        var dname = d.name.replace(/\.[^\.]+$/, '');
        var suffix = '_' + w + 'x' + h;
        var dPath = "~/Desktop";

        // File Saving Code
        var saveFilePSD = new File(dPath + '/' + dname + suffix + '.psd');
        SavePSD(saveFilePSD);

        var saveFileJPEG = new File(dPath + '/' + dname + suffix + '.jpg');
        SaveForWeb(saveFileJPEG);

        // Undo Convert to sRGB Step
        /*
        select();
        function select() {
        	var c2t = function (s) {
        		return app.charIDToTypeID(s);
        	};

        	var s2t = function (s) {
        		return app.stringIDToTypeID(s);
        	};

        	var descriptor = new ActionDescriptor();
        	var reference = new ActionReference();

        	reference.putEnumerated(c2t("HstS"), s2t("ordinal"), s2t("previous"));
        	descriptor.putReference(c2t("null"), reference);
        	executeAction(s2t("select"), descriptor, DialogModes.NO);
        }
        */

        // Restore the saved ruler units
        app.preferences.rulerUnits = savedRuler;

        // PSD Options
        function SavePSD(saveFilePSD) {
            psdSaveOptions = new PhotoshopSaveOptions();
            psdSaveOptions.embedColorProfile = true;
            psdSaveOptions.alphaChannels = true;
            psdSaveOptions.layers = true;
            psdSaveOptions.annotations = true;
            psdSaveOptions.spotColors = true;
            activeDocument.saveAs(saveFilePSD, psdSaveOptions, true, Extension.LOWERCASE);
        }

        // JPEG S4W Options
        function SaveForWeb(saveFileJPEG, jpegQuality) {
            /*
            // Convert to sRGB Step
                var doc = app.activeDocument;
                if (doc.colorProfileName.substring(0, 4) != "sRGB")
                doc.convertProfile("sRGB IEC61966-2.1", Intent.RELATIVECOLORIMETRIC, true, false); // BPC, Dither  
            */
            var sfwOptions = new ExportOptionsSaveForWeb();
            sfwOptions.format = SaveDocumentType.JPEG;
            sfwOptions.includeProfile = true;
            sfwOptions.interlaced = 0;
            sfwOptions.optimized = true;
            sfwOptions.quality = 70;
            activeDocument.exportDocument(saveFileJPEG, ExportType.SAVEFORWEB, sfwOptions);
        }

        alert('PSD and JPEG versions saved to the Desktop!');

        /* Main Code Finish */

        /* Start Open Document Error Check - Part B: Catch */
    } catch (err) {
        alert('An image must be open before running this script!')
    }
}
/* Finish Open Document Error Check - Part B : Catch */

 

 

 

 

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 ,
Oct 19, 2019 Oct 19, 2019

Copy link to clipboard

Copied

This script saves a PSD and JPEG version with a suffix to the same location as the original image, there is no error checking for files with the same name, use at your own risk!

 

 

// First post - After a script for saving files
// community.adobe.com/t5/Photoshop/First-post-After-a-script-for-saving-files/m-p/10669755#M269750

/* This script saves a PSD and JPEG version with a suffix to the same location as the original image, there is no error checking for files with the same name, use at your own risk! */

#target photoshop

/* Start Unsaved Document Error Check - Part A: Try */
unSaved ();

function unSaved() {
    try {
        activeDocument.path;
        /* Finish Unsaved Document Error Check - Part A: Try */

        /* Main Code Start */

        // Save the current ruler units
        var savedRuler = app.preferences.rulerUnits;

        // File Naming Code
        app.preferences.rulerUnits = Units.PIXELS;
        var d = app.activeDocument;
        var w = d.width.toString().replace(' px', '');
        var h = d.height.toString().replace(' px', '');
        var dname = d.name.replace(/\.[^\.]+$/, '');
        var suffix = '_' + w + 'x' + h;
        var dPath = d.path;

        // File Saving Code
        var saveFilePSD = new File(dPath + '/' + dname + suffix + '.psd');
        SavePSD(saveFilePSD);

        var saveFileJPEG = new File(dPath + '/' + dname + suffix + '.jpg');
        SaveForWeb(saveFileJPEG);

        // Undo Convert to sRGB Step
        /*
        select();
        function select() {
        	var c2t = function (s) {
        		return app.charIDToTypeID(s);
        	};

        	var s2t = function (s) {
        		return app.stringIDToTypeID(s);
        	};

        	var descriptor = new ActionDescriptor();
        	var reference = new ActionReference();

        	reference.putEnumerated(c2t("HstS"), s2t("ordinal"), s2t("previous"));
        	descriptor.putReference(c2t("null"), reference);
        	executeAction(s2t("select"), descriptor, DialogModes.NO);
        }
        */

        // Restore the saved ruler units
        app.preferences.rulerUnits = savedRuler;

        // PSD Options
        function SavePSD(saveFilePSD) {
            psdSaveOptions = new PhotoshopSaveOptions();
            psdSaveOptions.embedColorProfile = true;
            psdSaveOptions.alphaChannels = true;
            psdSaveOptions.layers = true;
            psdSaveOptions.annotations = true;
            psdSaveOptions.spotColors = true;
            activeDocument.saveAs(saveFilePSD, psdSaveOptions, true, Extension.LOWERCASE);
        }

        // JPEG S4W Options
        function SaveForWeb(saveFileJPEG, jpegQuality) {
            /*
            // Convert to sRGB Step
                var doc = app.activeDocument;
                if (doc.colorProfileName.substring(0, 4) != "sRGB")
                doc.convertProfile("sRGB IEC61966-2.1", Intent.RELATIVECOLORIMETRIC, true, false); // BPC, Dither  
            */
            var sfwOptions = new ExportOptionsSaveForWeb();
            sfwOptions.format = SaveDocumentType.JPEG;
            sfwOptions.includeProfile = true;
            sfwOptions.interlaced = 0;
            sfwOptions.optimized = true;
            sfwOptions.quality = 70;
            activeDocument.exportDocument(saveFileJPEG, ExportType.SAVEFORWEB, sfwOptions);
        }

        alert('PSD and JPEG versions saved to the same folder as the original image!');

        /* Main Code Finish */

        /* Start Unsaved Document Error Check - Part B: Catch */
    } catch (err) {
        alert('An image must be both open and saved before running this script!')
    }
}
/* Finish Unsaved Document Error Check - Part B : Catch */

 

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 ,
Oct 19, 2019 Oct 19, 2019

Copy link to clipboard

Copied

This script saves a PSD and JPEG version with a suffix to a sub-folder named "PSD & JPG Versions" next to the original image, there is no error checking for files with the same name, use at your own risk!

 

 

 

// First post - After a script for saving files
// community.adobe.com/t5/Photoshop/First-post-After-a-script-for-saving-files/m-p/10669755#M269750

/* This script saves a PSD and JPEG version with a suffix to a sub-folder next to the original image, there is no error checking for files with the same name, use at your own risk! */

#target photoshop

/* Start Unsaved Document Error Check - Part A: Try */
unSaved ();

function unSaved() {
    try {
        activeDocument.path;
        /* Finish Unsaved Document Error Check - Part A: Try */

        /* Main Code Start */

        // Save the current ruler units
        var savedRuler = app.preferences.rulerUnits;

        // File Naming Code
        app.preferences.rulerUnits = Units.PIXELS;
        var d = app.activeDocument;
        var w = d.width.toString().replace(' px', '');
        var h = d.height.toString().replace(' px', '');
        var dname = d.name.replace(/\.[^\.]+$/, '');
        var suffix = '_' + w + 'x' + h;
        var dPath = d.path;
        var sFold = new Folder(dPath + '/PSD & JPG Versions/')
        if (!sFold.exists){sFold.create()};

        // File Saving Code
        var saveFilePSD = new File(sFold + '/' + dname + suffix + '.psd');
        SavePSD(saveFilePSD);

        var saveFileJPEG = new File(sFold + '/' + dname + suffix + '.jpg');
        SaveForWeb(saveFileJPEG);

        // Undo Convert to sRGB Step
        /*
        select();
        function select() {
        	var c2t = function (s) {
        		return app.charIDToTypeID(s);
        	};

        	var s2t = function (s) {
        		return app.stringIDToTypeID(s);
        	};

        	var descriptor = new ActionDescriptor();
        	var reference = new ActionReference();

        	reference.putEnumerated(c2t("HstS"), s2t("ordinal"), s2t("previous"));
        	descriptor.putReference(c2t("null"), reference);
        	executeAction(s2t("select"), descriptor, DialogModes.NO);
        }
        */

        // Restore the saved ruler units
        app.preferences.rulerUnits = savedRuler;

        // PSD Options
        function SavePSD(saveFilePSD) {
            psdSaveOptions = new PhotoshopSaveOptions();
            psdSaveOptions.embedColorProfile = true;
            psdSaveOptions.alphaChannels = true;
            psdSaveOptions.layers = true;
            psdSaveOptions.annotations = true;
            psdSaveOptions.spotColors = true;
            activeDocument.saveAs(saveFilePSD, psdSaveOptions, true, Extension.LOWERCASE);
        }

        // JPEG S4W Options
        function SaveForWeb(saveFileJPEG, jpegQuality) {
            /*
            // Convert to sRGB Step
                var doc = app.activeDocument;
                if (doc.colorProfileName.substring(0, 4) != "sRGB")
                doc.convertProfile("sRGB IEC61966-2.1", Intent.RELATIVECOLORIMETRIC, true, false); // BPC, Dither  
            */
            var sfwOptions = new ExportOptionsSaveForWeb();
            sfwOptions.format = SaveDocumentType.JPEG;
            sfwOptions.includeProfile = true;
            sfwOptions.interlaced = 0;
            sfwOptions.optimized = true;
            sfwOptions.quality = 70;
            activeDocument.exportDocument(saveFileJPEG, ExportType.SAVEFORWEB, sfwOptions);
        }

        alert('PSD and JPEG versions saved to a new sub-folder next to the original image!');

        /* Main Code Finish */

        /* Start Unsaved Document Error Check - Part B: Catch */
    } catch (err) {
        alert('An image must be both open and saved before running this script!')
    }
}
/* Finish Unsaved Document Error Check - Part B : Catch */

 

 

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 ,
Oct 19, 2019 Oct 19, 2019

Copy link to clipboard

Copied

LATEST

This script saves a PSD and JPEG version with a suffix to a user selected folder, there is no error checking for files with the same name, use at your own risk!

 

 

// First post - After a script for saving files
// community.adobe.com/t5/Photoshop/First-post-After-a-script-for-saving-files/m-p/10669755#M269750

/* This script saves a PSD and JPEG version with a suffix to a user selected folder, there is no error checking for files with the same name, use at your own risk! */

#target photoshop

/* Start Open Document Error Check - Part A: Try */
aDoc();
var originalDoc

function aDoc() {
    try {
        originalDoc = app.activeDocument
        /* Finish Open Document Error Check - Part A: Try */

        /* Main Code Start */

        // Save the current ruler units
        var savedRuler = app.preferences.rulerUnits;

        // File Naming Code
        app.preferences.rulerUnits = Units.PIXELS;
        var d = app.activeDocument;
        var w = d.width.toString().replace(' px', '');
        var h = d.height.toString().replace(' px', '');
        var dname = d.name.replace(/\.[^\.]+$/, '');
        var suffix = '_' + w + 'x' + h;
        var dPath = Folder.selectDialog('Select a folder to save the PSD and JPEG images to:');

        // File Saving Code
        var saveFilePSD = new File(dPath + '/' + dname + suffix + '.psd');
        SavePSD(saveFilePSD);

        var saveFileJPEG = new File(dPath + '/' + dname + suffix + '.jpg');
        SaveForWeb(saveFileJPEG);

        // Undo Convert to sRGB Step
        /*
        select();
        function select() {
        	var c2t = function (s) {
        		return app.charIDToTypeID(s);
        	};

        	var s2t = function (s) {
        		return app.stringIDToTypeID(s);
        	};

        	var descriptor = new ActionDescriptor();
        	var reference = new ActionReference();

        	reference.putEnumerated(c2t("HstS"), s2t("ordinal"), s2t("previous"));
        	descriptor.putReference(c2t("null"), reference);
        	executeAction(s2t("select"), descriptor, DialogModes.NO);
        }
        */

        // Restore the saved ruler units
        app.preferences.rulerUnits = savedRuler;

        // PSD Options
        function SavePSD(saveFilePSD) {
            psdSaveOptions = new PhotoshopSaveOptions();
            psdSaveOptions.embedColorProfile = true;
            psdSaveOptions.alphaChannels = true;
            psdSaveOptions.layers = true;
            psdSaveOptions.annotations = true;
            psdSaveOptions.spotColors = true;
            activeDocument.saveAs(saveFilePSD, psdSaveOptions, true, Extension.LOWERCASE);
        }

        // JPEG S4W Options
        function SaveForWeb(saveFileJPEG, jpegQuality) {
            /*
            // Convert to sRGB Step
                var doc = app.activeDocument;
                if (doc.colorProfileName.substring(0, 4) != "sRGB")
                doc.convertProfile("sRGB IEC61966-2.1", Intent.RELATIVECOLORIMETRIC, true, false); // BPC, Dither  
            */
            var sfwOptions = new ExportOptionsSaveForWeb();
            sfwOptions.format = SaveDocumentType.JPEG;
            sfwOptions.includeProfile = true;
            sfwOptions.interlaced = 0;
            sfwOptions.optimized = true;
            sfwOptions.quality = 70;
            activeDocument.exportDocument(saveFileJPEG, ExportType.SAVEFORWEB, sfwOptions);
        }

        alert('PSD and JPEG versions saved!');

        /* Main Code Finish */

        /* Start Open Document Error Check - Part B: Catch */
    } catch (err) {
        alert('An image must be open before running this script!')
    }
}
/* Finish Open Document Error Check - Part B : Catch */

 

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