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

.jsx Script that saves edited .jpg to subfolder with the same filename.

Community Beginner ,
May 24, 2021 May 24, 2021

Copy link to clipboard

Copied

The goal is, preferably in JavaScript, if not possible, seeing as the process is being carried out on a Windows machine VBS is the acceptable alternative, to automate by means of scripting the process of saving a file to relative path, that is, the subfolder within the folder of the original .jpg.

 

Process example is as follows:

 

Opened/active file name: 42.jpg

Save edited file as: 42.jpg

 

Opened/active file folder: ~\jpg

To folder: ~\jpg\Edited

(Folder location not constant which gets absolute path out of the question.)

 

JPEG Options:

Matte: None

Image Options: Quality: 12

Format Options: Baseline ("Standard")

 

Reason for looking into a scripting solution being that, unlike Photoshop's built in Batch processing tool, as far as currently understood, the operation would have to be able to be carried out at certain point, when required, that is, be activated as an Action by keyboard shortcut, and as far as the awareness of capabilities of the Batch tool goes, while it is able to accomplish the task in question where process is fully automated in which case it is extremely useful, when process has to have interuptions for purposes of editing the image or any other kind of user inputto be made, the only remaining option seems that there is perhaps a requirement for a script.

 

Several attempts were made, some of which included copy/pasting a number of whole code sets, with minor changes being made to it for purposes of adaptation, as well as trying, without avail, to make the script for completing the task by crumpling together various snippets of code from multiple sources, though due to heavy limitations of time slot availability and not high enough level of familiarization with JavaScript and coding alltohether as the case may be, such methods were of no use thereby leaving this, that being directly asking for it the only potentially viable option.

 

Thanks in advance.

TOPICS
Actions and scripting , Windows

Views

1.9K

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 2 Correct answers

LEGEND , May 24, 2021 May 24, 2021
(jpg = new JPEGSaveOptions()).quality = 12; aD = activeDocument
if (!File(edtd = aD.path + '/Edited/').exists) Folder(edtd).create()
aD.saveAs(File(edtd + aD.name), jpg)

Votes

Translate

Translate
Valorous Hero , May 24, 2021 May 24, 2021

 

var doc = activeDocument;
var subfolder = "Edited"; 

var file = new File(doc.path.fsName + "/"+ subfolder + "/" + doc.name); // extention will be the same

var opt = new JPEGSaveOptions();

opt.embedColorProfile = true;
opt.mate = MatteType.NONE;
opt.quality = 12;
optformatOptions = FormatOptions.STANDARDBASELINE;

if (create_folder(new Folder(doc.path.fsName + "/"+ subfolder)))
    doc.saveAs(file, opt, false);
else
    alert("Cant create folder!");

function create_folder(f)
    {
    try {
...

Votes

Translate

Translate
Adobe
LEGEND ,
May 24, 2021 May 24, 2021

Copy link to clipboard

Copied

(jpg = new JPEGSaveOptions()).quality = 12; aD = activeDocument
if (!File(edtd = aD.path + '/Edited/').exists) Folder(edtd).create()
aD.saveAs(File(edtd + aD.name), jpg)

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 Beginner ,
May 24, 2021 May 24, 2021

Copy link to clipboard

Copied

Short and on point, simple enough to make sense to the untrained eye. Tested and working flawlessly.

 

Much appreciated.

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
Valorous Hero ,
May 24, 2021 May 24, 2021

Copy link to clipboard

Copied

 

var doc = activeDocument;
var subfolder = "Edited"; 

var file = new File(doc.path.fsName + "/"+ subfolder + "/" + doc.name); // extention will be the same

var opt = new JPEGSaveOptions();

opt.embedColorProfile = true;
opt.mate = MatteType.NONE;
opt.quality = 12;
optformatOptions = FormatOptions.STANDARDBASELINE;

if (create_folder(new Folder(doc.path.fsName + "/"+ subfolder)))
    doc.saveAs(file, opt, false);
else
    alert("Cant create folder!");

function create_folder(f)
    {
    try {
        if (f.parent && !f.parent.exists) if (!create_folder(f.parent)) return false;

        return f.create();
        }
    catch(e) { return false; }  
    }  

 

 

upd.

again a typo 🙂

optformatOptions = FormatOptions.STANDARDBASELINE;

no dot

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 Beginner ,
May 24, 2021 May 24, 2021

Copy link to clipboard

Copied

Well, thanks for giving it a second look and noticing the mistype. Don't know if personally finding any kind of syntax error in the sample above would have been possible. Still, JavaScript grammar aside, this piece of code, although being more complex of the two offered solutions will also be tested and, provided enough time studied more thoroughly as, given it's higher level of complexity when combined with experience in manually completing the operation it was written to automate a hefty number of times is bound to offer quite a bit of insight in the cogworks of JavaScript.

 

Greatly appreciated.

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
LEGEND ,
May 24, 2021 May 24, 2021

Copy link to clipboard

Copied

I read also all your other posts. You have eloquent and wise-kind level of wordings 😉

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 ,
May 24, 2021 May 24, 2021

Copy link to clipboard

Copied

r-bin's code above is "standard" and will be easier to learn from using traditional learning resources. The code from Kukurykus is as he has previously said "in his own style".  :]

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 Beginner ,
May 24, 2021 May 24, 2021

Copy link to clipboard

Copied

Sounds right, as far as a below beginner level code writer can judge something like that, but still, based on the number of code samples seen in the JS Photoshop scripting literature the statement computes since, although being quite a bit longer, and while not being able to put a finger to any specific part of it, r-bin's piece of code seems more familiar.

 

Still, both did the job flawlessly, and will be studied more extensively at the closest opportune moment. 

 

 

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
LEGEND ,
May 24, 2021 May 24, 2021

Copy link to clipboard

Copied

In this case the own style doesn't make it less 'standard' and both codes do the same by using same methods. They check for subfolder existence, also assign variables and saving options.

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 ,
May 24, 2021 May 24, 2021

Copy link to clipboard

Copied

 

@Kukurykus your code style is similar to how Neo from the Matrix sees the world around him... The code may as well be:

 

01101000 01101111 01110111 00100000 01100100 01101111 01100101 01110011 00100000 01001011 01110101 01101011 01110101 01110010 01111001 01101011 01110101 01110011 00100000 01100011 01101111 01100100 01100101 00100000 01101100 01101001 01101011 01100101 00100000 01101000 01100101 00100000 01100100 01101111 01100101 01110011 00111111

 

I'm not having a go at you, I admire your concise coding style. I'm just noting that your style isn't "textbook" as one would find with many "standard" tutorials and not easy for a beginner (such as myself) to learn compared to more verbose "regular" 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
LEGEND ,
May 24, 2021 May 24, 2021

Copy link to clipboard

Copied

LATEST

I didn't say of my style in general, but style of this one code. In this case the style doesn't make it specific. It's concise so easy for me to follow, not like docs codes which always were hard & too extensive for me. Better for learning are small samples without plenty of distracting parts taking your focus from the core.

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 ,
May 24, 2021 May 24, 2021

Copy link to clipboard

Copied

 

Rather than using save as, this version uses export save for web (I know that you have different criteria). I have commented out the code that converts to sRGB.

 

/* This script saves an export save for web JPEG version of a saved file to a sub-folder next to the original image. Existing files will be overwritten without warning! */

#target photoshop

unSaved();

function unSaved() {
    try {
        var d = app.activeDocument;
        var dPath = d.path;

        // Check open image for saved file path
        dPath;

        // File Naming Code
        var dname = d.name.replace(/\.[^\.]+$/, '');
        var sFold = new Folder(dPath + '/Edited')
        if (!sFold.exists) {
            sFold.create()
        }

        // File Saving Code
        var saveFileJPEG = new File(sFold + '/' + dname + '.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);
        }
        */

        // 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('JPEG version saved to a new sub-folder next to the original image!');

    } catch (err) {
        alert('An image must be both open and saved before running this script!');
    }
}

 

 

I know you have marked correct answers, this is just another way of doing things as a learning exercise/example.

 

EDIT: I had to remove the overwrite warning from the original post. I'll update the code again when I get it sorted...

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 Beginner ,
May 24, 2021 May 24, 2021

Copy link to clipboard

Copied

Well, this is definitely getting more than was bargained for. Didn't get to that point yet, but have nevertheless come across that option in Photoshop, Save for Web that is, and, as the processed material is ultimately intended for such purposes, made a mental note to get back to it upon acquiring more free time. However, this is another scheduled coding task taken care of.

Will be testing this portion of code promptly.

 

Thank You.

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