What's new
Photoshop Gurus Forum

Welcome to Photoshop Gurus forum. Register a free account today to become a member! It's completely free. Once signed in, you'll enjoy an ad-free experience and be able to participate on this site by adding your own topics and posts, as well as connect with other members through your own private inbox!

Reply to thread

Hey there, thanks ever so much for that,


A friend of mine adapted the script to my needs a little and came up with the following:


#target Photoshop


String.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);

   

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

        }

    }

   

    function getFileList(folder) {

        var fileList = folder.getFiles();

       

        for (var i = 0; i < fileList.length; i++) {

            var file = fileList

            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 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;

        }

    } else{

        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){

            return true;

        }

    }

};


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

    }

}


Unfortunately it creates 1x1 pixels randomly out of files that I can't find any common factor of (other than the fact they're PSD/TIF and have no transparency).


It's very consistent with which ones are turned into these annoying 1x1 pixels. Doesn't sound like the end of the world but I've now gotta go through various subfolders, find them and convert them manually, which means this script isn't really saving much time :/


When my friend tried diagnosing this he found Photoshop said something along the lines of File does not exist.


I've attached an image which this happens to.


Thanks again :)


What is our favorite program/app? (Hint - it begins and ends with the letter P)
Back
Top