Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Dec 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Photoshop Javascript question

    I have a folder with a lot of subfolders, each with a bunch of TIFs and PSD files inside. Some of these have transparency in them while some don't. These files vary massively in size.

    I need all the files to be turned into JPGs, or if they contain transparency, PNGs. I require the files to be 200kb or less and don't really mind how large they are as long as they aren't scaled up.

    Someone on a forum (who I'm insanely thankful for) wrote a fair bit of code for it, which my friend modified to suit exactly what I was asking and we're nearly there now.

    It worked fine, the only problem being that a lot of images came out 1x1 pixel and a solid block of colour.

    We've found this was consistently happening with the same images for some reason, but couldn't work out what exactly it was in these images.

    Now Mr forum blokey ( http://www.photoshopgurus.com/forum/...s/paul-mr.html ) modified the script and it now seems to work fine with PSDs.

    It's working with TIFs with transparency but some of the TIFs with 100% opacity it just won't work on. I can't find much that's consistent with these files other than the colour blue, though this just could be a massive coincidence and probably is (there's a lot of blue in the images I've been dealing with).

    Below is a link to the thread in which the code was first written. Paul MR seems to think the colorsampler bit is a little suspect so perhaps that's what's causing the problems (blueness?).

    http://www.photoshopgurus.com/forum/...-filesize.html

    I wish I could do a little more to try and work this out myself but I've barely a speck of understanding on this stuff, I just know when there's a situation where a bit of scripting could help out.

    Below is the script as it currently stands:
    Code:
        #target PhotoshopString.prototype.endsWith  = function(str) {
         return (this.match(str  + "$")  == str)
        } String.prototype.startsWith  = function(str) {
            return this.indexOf(str)  == 0;
        };
        var desiredFileSize  = 200000;
        app.bringToFront();
        app.displayDialogs  = DialogModes.NO;
        main();
        //app.displayDialogs = DialogModes.YES;
        function main() {
            var topLevelFolder  = Folder.selectDialog("Please select top level folder.");
            if (topLevelFolder  == null)return;
            var FileList  = [];
            getFileList(topLevelFolder);
            var startRulerUnits  = app.preferences.rulerUnits;
            app.preferences.rulerUnits  = Units.PIXELS;
            for (var f in FileList)  {
                app.open(FileList[f]);
                activeDocument.changeMode(ChangeMode.RGB);
                try  {
                    activeDocument.mergeVisibleLayers();
                } catch(e)  {} var Name  = decodeURI(app.activeDocument.name).replace(/.[^.] + $ /, '');
                if (hasTransparency(FileList[f]))  {
                    var saveFile  = File(FileList[f].path  + "/"  + Name  + ".png");
                    SavePNG(saveFile);
                    app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
                } else  {
                    var saveFile  = File(FileList[f].path  + "/"  + Name  + ".jpg");
                    SaveForWeb(saveFile, 80);
                    app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
                } app.preferences.rulerUnits  = startRulerUnits;
            } function getFileList(folder)  {
                var fileList  = folder.getFiles();
                for (var i = 0; i < fileList.length; i++)  {
                    var file  = fileList[i];
                    if (file instanceof Folder)  {
                        getFileList(file);
                    } else  {
                        if ((file.name.endsWith("tiff")  || file.name.endsWith("tif")  || file.name.endsWith("psd"))  &&  ! file.name.startsWith("._"))FileList.push(file);
                    }
                }
            } alert(FileList.length  + " files have been modified.");
        } function hasTransparency(file) {
            if (file.name.endsWith("tiff")  || file.name.endsWith("tif"))  {
                var sample  = app.activeDocument.colorSamplers.add([new UnitValue(1.5, 'px'), new UnitValue(1.5, 'px')]);
                try  {
                    sample.color.rgb.hexValue;
                    sample.remove();
                    return false;
                } catch(e)  {
                    sample.remove();
                    return true;
                }
            } var doc  = activeDocument;
            if (doc.activeLayer.isBackgroundLayer)return false;
            var desc  = new ActionDescriptor();
            var ref  = new ActionReference();
            ref.putProperty(charIDToTypeID("Chnl"), charIDToTypeID("fsel"));
            desc.putReference(charIDToTypeID("null"), ref);
            var ref1  = new ActionReference();
            ref1.putEnumerated(charIDToTypeID("Chnl"), charIDToTypeID("Chnl"), charIDToTypeID("Trsp"));
            desc.putReference(charIDToTypeID("T "), ref1);
            executeAction(charIDToTypeID("setd"), desc, DialogModes.NO);
            var w  = doc.width.as('px');
            var h  = doc.height.as('px');
            var transChannel  = doc.channels.add();
            doc.selection.store(transChannel);
            if (transChannel.histogram[255]  != (h  * w))  {
                transChannel.remove();
                return true;
            } else  {
                transChannel.remove();
                return false;
            }
        };
        function SavePNG(saveFile) {
            pngSaveOptions  = new PNGSaveOptions();
            activeDocument.saveAs(saveFile, pngSaveOptions, true, Extension.LOWERCASE);
            var actualFilesize  = saveFile.length;
            var ratio  = desiredFileSize / actualFilesize;
            if (ratio  < 1)  {
                var imageScale  = Math.sqrt(ratio);
                activeDocument.resizeImage(activeDocument.width  * imageScale, activeDocument.height  * imageScale, activeDocument.resolution, ResampleMethod.BICUBICSMOOTHER);
                activeDocument.saveAs(saveFile, pngSaveOptions, true, Extension.LOWERCASE);
            }
        }
        function SaveForWeb(saveFile, jpegQuality) {
            var sfwOptions  = new ExportOptionsSaveForWeb();
            sfwOptions.format  = SaveDocumentType.JPEG;
            sfwOptions.includeProfile  = false;
            sfwOptions.interlaced  = 0;
            sfwOptions.optimized  = true;
            sfwOptions.quality  = jpegQuality;
            activeDocument.exportDocument(saveFile, ExportType.SAVEFORWEB, sfwOptions);
            var actualFilesize  = saveFile.length;
            var ratio  = desiredFileSize / actualFilesize;
            if (ratio  < 1)  {
                var imageScale  = Math.sqrt(ratio);
                activeDocument.resizeImage(activeDocument.width  * imageScale, activeDocument.height  * imageScale, activeDocument.resolution, ResampleMethod.BICUBICSMOOTHER);
                activeDocument.exportDocument(saveFile, ExportType.SAVEFORWEB, sfwOptions);
            }
        }

  • #2
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,277
    Thanks
    10
    Thanked 581 Times in 562 Posts
    tif files can have many different encodings. tif is more of a container than an images format, though it obviously performs as both. still, it's likely the flexibility of the format that raised diffuculty for you.

    i don't know why it's not working, but you might try change the 1.5px to 1px; you can't have half a pixel...
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/5/28) IE7:0.1, IE8:5.3, IE11:8.4, IE9:3.2, IE10:3.2, FF:18.2, CH:46, SF:7.9, NON-MOUSE:32%


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •