Save for android photoshop javascript

Due to different pixel density of different android devices developer / designer need to create different versions of graphic artifacts.

Recently I've been trying to create my first Android application and got tired of creating image artifacts for different pixel density. As a result I came up with a following photoshop javascript that automates with tedious task.

var docRef = activeDocument;    
function saveAs(path,filename,width,height,dpi) {
    savedState = docRef.activeHistoryState
    docRef.resizeImage (width, height, dpi);
    var folder = new Folder (path);
    if (!folder.exists)
    {
         folder.create();
    }
    pngFile = new File( path + filename)
    pngSaveOptions = new PNGSaveOptions()    
    app.activeDocument.saveAs(pngFile, pngSaveOptions, true, Extension.LOWERCASE)
    docRef.activeHistoryState = savedState    
}

function main(isIcon)  {
    if (documents.length == 0) {
        alert("There are no documents open.");
        return;
    }
    try {        
            Path = app.activeDocument.path;
     }
    catch (exception) {
         docRef.saveAs(File.saveDialog ("Save image",["*.psd","*.png","*.jpg"]));
    } 
    Path = app.activeDocument.path;
    var Name = app.activeDocument.name.replace(/\.[^\.]+$/, '');     
    startRulerUnits = app.preferences.rulerUnits;
    app.preferences.rulerUnits = Units.PIXELS;    
    globSavedState = docRef.activeHistoryState;
    try {
        docRef.mergeVisibleLayers();
    }
    catch (exception) {
        alert("Please select visible layer.");
        return;
    }
    if (isIcon) {
        saveAs(Path + "/drawable-xxhdpi/" , Name + ".png",144,144,480);
        saveAs(Path + "/drawable-xhdpi/"  , Name + ".png",96,96,320);
        saveAs(Path + "/drawable-hdpi/"   ,Name + ".png",72,72,240);
        saveAs(Path + "/drawable-mdpi/"  ,Name + ".png",48,48,160);
        saveAs(Path + "/drawable-ldpi/"   ,Name + ".png",36,36,120);
    }
    else {
        var dpi = 480;
        saveAs(Path + "/drawable-xxhdpi/" , Name + ".png", docRef.width * dpi / docRef.resolution, docRef.height * dpi / docRef.resolution, dpi);
        dpi = 320;
        saveAs(Path + "/drawable-xhdpi/" , Name + ".png", docRef.width * dpi / docRef.resolution,docRef.height * dpi / docRef.resolution, dpi);
        dpi = 240;
        saveAs(Path + "/drawable-hdpi/" , Name + ".png", docRef.width * dpi / docRef.resolution ,docRef.height * dpi / docRef.resolution, dpi);
        dpi = 160;
        saveAs(Path + "/drawable-mdpi/" , Name + ".png", docRef.width * dpi / docRef.resolution,docRef.height * dpi / docRef.resolution, dpi);
        dpi = 120;
        saveAs(Path + "/drawable-ldpi/" , Name + ".png", docRef.width * dpi / docRef.resolution, docRef.height * dpi / docRef.resolution, dpi);
    }
    docRef.activeHistoryState = globSavedState;   
    app.preferences.rulerUnits = startRulerUnits;
    docRef.save();            
}

main(true) generates following:

480dpi 144x144 - drawable-xxhdpi
320dpi 96x96 - drawable-xhdpi
240dpi 72x72 - drawable-hdpi
160dpi 48x48 - drawable-mdpi
120dpi 36x36 - drawable-ldpi

main(false) generates images for android in respective folders, where dimensions are proportional to selected resolution / dpi.

For example:

Resolution: 480dpi
Width:150
Height:80

Becomes:

480dpi 150x80 - drawable-xxhdpi
320dpi 100x53 - drawable-xhdpi
240dpi 75x40 - drawable-hdpi
160dpi 50x27 - drawable-mdpi
120dpi 37x20- drawable-ldpi

See Save4Android github repository for more details.

Posted on Sunday, March 17, 2013 by | Add Comment

How to log a message from t-sql script?

Recently I've been working on a long running script that will be executed directly in PROD enviroment. I needed an easy way to see a progress of t-sql script execution.

I tried to use PRINT, but it doesn’t output anything until script execution is done.

You can use some log table and insert progress status messages there, but there is an easier way:

RAISERROR ('Message', 10, 1) WITH NOWAIT

RAISERROR with severity of 10 or less doesn't trigger try catch block and can be used for log purposes. Here is a little longer example:


DECLARE @i INT = 0

WHILE 1=1
BEGIN                                
      
    IF @i >= 10
                BREAK;    
    WAITFOR DELAY '00:00:01';                                    
    SET @i = @i + 1        
    RAISERROR ('%d sec running', 10, 1, @i) WITH NOWAIT
END

Results:

Raiseerror execution example
Posted on Saturday, January 26, 2013 by | Add Comment

How to get both inserted source table id and target table id?

Lets say we have two tables source and target. We want to copy records from the source to the target table and save mappings between old and new ids in a third table.

DECLARE @source TABLE
(
   id INT IDENTITY(5,1) NOT NULL PRIMARY KEY,
   name VARCHAR(max)  
)

DECLARE @target TABLE
(
   id INT IDENTITY(1,1) NOT NULL PRIMARY KEY,
   name VARCHAR(max)
)

DECLARE @inserted TABLE
(
   id INT NOT NULL,
   clone_id INT NOT NULL  
)

Lets populate the source table:

INSERT INTO @subscription
(name)
VALUES
('Test 1'),
('Test 2'),
('Test 3')

Now we can populate the target table with the following code:

INSERT INTO @target
        (name )
SELECT name + ' - Cloned' FROM @source

You can try to populate inserted table using the following code:

INSERT INTO @target
        (name )
OUTPUT s.id, INSERTED.id INTO @inserted
SELECT name + ' - Cloned' FROM @source s

But you will get a syntax error:

The multi-part identifier "s.id" could not be bound.

This is due to a nature of the INSERT statement. The SELECT statement considered to be apart from the INSERT statement, so you can't reference its tables in the OUTPUT clause. However you can achieve desired outcome using MERGE sql statement, which is a part of SQL standard from 2003, but rarely used by anybody.

MERGE @target t
USING @source s
ON 0=1
WHEN NOT MATCHED THEN 
INSERT (name) VALUES (s.name + ' - Cloned')
OUTPUT s.id, INSERTED.id INTO @inserted;

SELECT * FROM @source
SELECT * FROM @target
SELECT * FROM @inserted

Results:

idname
5Test 1
6Test 2
7Test 3

idname
1Test 1 - Cloned
2Test 2 - Cloned
3Test 3 - Cloned

idclone_id
51
62
73
Posted on Saturday, January 05, 2013 by | Add Comment

Categories

Valid HTML5