jawsware international - mobile app development
logo

856.359.4685

mobile@jawsware.com


SOURCE CODE SHARING


PNG Creation Scripts

DOWNLOAD SOURCE
Updated: 12/2/2011

This project shows how to use the scripting capabilities of Photo Shop to automate the creations of the different icons/graphics needed when developing Android apps.

It takes care of resizing associated effects just as the manual resize in Photo Shop does.

The createpng script is used to actually do the resizing and saving. The rest do various tasks and utilize the first to do them.

The createpng script was created, in-part, with the aid of the The ScriptListener plug-in described here.

createpng.jsx

/*
   Copyright 2011 jawsware international

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/

#target photoshop

var iPath;
var iPathSuffix = "";

app.bringToFront();

function getPathSuffix() {
    iPathSuffix = prompt ("Enter a desired suffix to add the folder name. Entering nothing is allowable", "", "Folder Name Suffix");
}

function confirmFolder(msg) {
    
        return confirm(msg + "\n\n" + iPath.fullName + iPathSuffix, false, "Android PNG Creator")
    
    }

function createPNG(name, width, height, cropWidth, cropHeight, noTransparency) {

    var savedState = app.activeDocument.activeHistoryState;
    
    var idImageSizeAction = charIDToTypeID( "ImgS" );
    var actionDesc = new ActionDescriptor();

     if (width != null) {
         //Size is constrained - use width
        var idWdth = charIDToTypeID( "Wdth" );
        var idPxl = charIDToTypeID( "#Pxl" );
        actionDesc.putUnitDouble( idWdth, idPxl, width );
    } else if (height != null) {
         //Size is constrained - use width
        var idHght = charIDToTypeID( "Hght" );
        var idPxl = charIDToTypeID( "#Pxl" );
        actionDesc.putUnitDouble( idHght, idPxl, height );
    } else {
        //neither width or height was supplied, exit.
        return;
    }

    //check if dest folder exists.  Create if not.  Exit before doing anything if folder cannot be created.
    var dest = new File(iPath.fullName + iPathSuffix + name + ".png");
  
    if (! dest.parent.exists) {
        if (! dest.parent.create()) {
            alert("Unabled to create:\n\n" + dest.fullName);
            return;
            }
        }
    
    var idscaleStyles = stringIDToTypeID( "scaleStyles" );
    actionDesc.putBoolean( idscaleStyles, true );
    
    var idConstrainProportions = charIDToTypeID( "CnsP" );
    actionDesc.putBoolean( idConstrainProportions, true );
    
    var idIntr = charIDToTypeID( "Intr" );
    var idIntp = charIDToTypeID( "Intp" );
    var idBcbc = charIDToTypeID( "Bcbc" );
    actionDesc.putEnumerated( idIntr, idIntp, idBcbc );
    
    executeAction( idImageSizeAction, actionDesc, DialogModes.NO );   

    if  ((cropWidth != null) || (cropHeight != null)) {
        
            if (cropWidth == null) {
                cropWidth = width;
                }
            
            if (cropHeight == null) {
                cropHeight = height;
                }

            app.activeDocument.resizeCanvas (cropWidth, cropHeight);
        
        }
    
    var sfwOptions = new ExportOptionsSaveForWeb(); 
    sfwOptions.format = SaveDocumentType.PNG; 
    sfwOptions.PNG8 = false;
    sfwOptions.includeProfile = false; 
    sfwOptions.interlaced = 0; 
    sfwOptions.optimized = true; 
    
    if (noTransparency) {
        sfwOptions.transparency = false; 
    } else {
        sfwOptions.transparency = true; 
     }

    activeDocument.exportDocument(dest, ExportType.SAVEFORWEB, sfwOptions);    
      
    app.activeDocument.activeHistoryState = savedState;

}

createpng-launch-icons.jsx

/*
   Copyright 2011 jawsware international

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/

// @include "createpng.jsx"

createGoogleIconPNGs();

function createGoogleIconPNGs() {

    var name = app.activeDocument.name;
    
    name = name.substr (0, name.indexOf ("."));

    name = name.toUpperCase();
    
	for ( i = 0;  i < app.documents.length;  i++ )  {

        if (app.documents[i].path.fullName.indexOf("~") == -1) {

            for ( j = 0;  j < app.documents[i].layers.length;  j++ )  {
               
                        if (name.indexOf (app.documents[i].layers[j].name.toUpperCase()) == 0) {
                            iPath = app.documents[i].path;
                            break;
                            }
            }
        }
    }

    if (iPath == null) {
        iPath = Folder.selectDialog ("Select Resource Folder");
            
        if (iPath == null) {
            return;
            }

} else { 

        do {

            iPath = new Folder(iPath.fullName + "/res")

            if (iPath.exists) {
                break;
                } 
            iPath = iPath.parent.parent;      

        } while (iPath != null)

        if (iPath == null) {
                return;
            }

    }

    if (! confirmFolder ("PNGs will be created in the drawable sub folder of:")) {
        return;
        }
    
    iName = prompt ("Enter the desired name of the icon", "ic_launcher", "Icon Name");
   
    if (iName == null) {
        return;
        }
   
    createPNG("/drawable-xhdpi/" + iName, 96);
    createPNG("/drawable-hdpi/" + iName, 72);
    createPNG("/drawable-mdpi/" + iName, 48);
    createPNG("/drawable-ldpi/" + iName, 36);
   
 }

createpng-google-icons.jsx

/*
   Copyright 2011 jawsware international

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/

// @include "createpng.jsx"

createGoogleIconPNGs();

function createGoogleIconPNGs() {

    var name = app.activeDocument.name;
    
    name = name.substr (0, name.indexOf ("."));

    name = name.toUpperCase();
    
	for ( i = 0;  i < app.documents.length;  i++ )  {

        if (app.documents[i].path.fullName.indexOf("~") == -1) {

            for ( j = 0;  j < app.documents[i].layers.length;  j++ )  {
               
                        if (name.indexOf (app.documents[i].layers[j].name.toUpperCase()) == 0) {
                            iPath = app.documents[i].path;
                            break;
                            }
            }
        }
    }

    if (iPath == null) {
        return;
        }

    getPathSuffix();
    
    iPath = new Folder(iPath.fullName + "/google")
        
    if (! confirmFolder ("PNGs will be created in the folder:")) {
        return;
        }
   
    createPNG("/app_icon", 512);

}

createpng-google-promo.jsx

/*
   Copyright 2011 jawsware international

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/

// @include "createpng.jsx"

createGooglePromoPNGs();

function createGooglePromoPNGs() {

    iPath = app.activeDocument.path

    if (iPath == null) {
        return;
        }

    getPathSuffix();

    iPath = new Folder(iPath.fullName + "/google")
        
    if (! confirmFolder ("PNGs will be created in the folder")) {
        return;
        }
   
    createPNG("/promo", null, 120, 180, null, true);

}

createpng-amazon-icons.jsx

/*
   Copyright 2011 jawsware international

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/

// @include "createpng.jsx"

createAmazonIconPNGs();

function createAmazonIconPNGs() {

    var name = app.activeDocument.name;
    
    name = name.substr (0, name.indexOf ("."));

    name = name.toUpperCase();
    
	for ( i = 0;  i < app.documents.length;  i++ )  {

        if (app.documents[i].path.fullName.indexOf("~") == -1) {

            for ( j = 0;  j < app.documents[i].layers.length;  j++ )  {
               
                        if (name.indexOf (app.documents[i].layers[j].name.toUpperCase()) == 0) {
                            iPath = app.documents[i].path;
                            break;
                            }
            }
        }
    }

    if (iPath == null) {
        return;
        }

    getPathSuffix();
    
    iPath = new Folder(iPath.fullName + "/amazon")
        
    if (! confirmFolder ("PNGs will be created in the folder:")) {
        return;
        }
   
    createPNG("/icon", 114);

    createPNG("/thumbnail", 512);

}

createpng-amazon-promo.jsx

/*
   Copyright 2011 jawsware international

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/

// @include "createpng.jsx"

createAmazonPromoPNGs();

function createAmazonPromoPNGs() {

    iPath = app.activeDocument.path

    if (iPath == null) {
        return;
        }

    getPathSuffix();

    iPath = new Folder(iPath.fullName + "/amazon")
        
    if (! confirmFolder ("PNGs will be created in the folder")) {
        return;
        }
   
    createPNG("/promo", null, 140);

}


jawsware international856.359.4685mobile@jawsware.com
AppsCode