Background

Jeff Tranberry posted an article on installing and using the Photshop ScriptingListener Plug-In ( http://blogs.adobe.com/crawlspace/2006/05/installing_and_1.html ).

It's a good plug-in for leaning for to perform more advaned photoshop animation, but its output can be difficult to decipher.

What I did

For that reason I wrote a Python (www.python.org) script that converts the output of the listner (JavaScript code) into some cleaner JavaScript code that (for my purposes) is easier to read.

How this helps

The primary cleaning consists of:

  • assigning the constants to special values
  • merging duplicate constants
  • calls to execution an action are merged into a function

Getting the code

You can download the script here: clean-photoshop-listener-output-v1.zip

 

An Example

Here's the listener generates the following code to create a new blank document:

var id589 = charIDToTypeID( "Mk  " );
    var desc51 = new ActionDescriptor();
    var id590 = charIDToTypeID( "Nw  " );
        var desc52 = new ActionDescriptor();
        var id591 = charIDToTypeID( "Md  " );
        var id592 = charIDToTypeID( "RGBM" );
        desc52.putClass( id591, id592 );
        var id593 = charIDToTypeID( "Wdth" );
        var id594 = charIDToTypeID( "#Rlt" );
        desc52.putUnitDouble( id593, id594, 800.000000 );
        var id595 = charIDToTypeID( "Hght" );
        var id596 = charIDToTypeID( "#Rlt" );
        desc52.putUnitDouble( id595, id596, 600.000000 );
        var id597 = charIDToTypeID( "Rslt" );
        var id598 = charIDToTypeID( "#Rsl" );
        desc52.putUnitDouble( id597, id598, 72.000000 );
        var id599 = stringIDToTypeID( "pixelScaleFactor" );
        desc52.putDouble( id599, 1.000000 );
        var id600 = charIDToTypeID( "Fl  " );
        var id601 = charIDToTypeID( "Fl  " );
        var id602 = charIDToTypeID( "Wht " );
        desc52.putEnumerated( id600, id601, id602 );
        var id603 = charIDToTypeID( "Dpth" );
        desc52.putInteger( id603, 8 );
        var id604 = stringIDToTypeID( "profile" );
        desc52.putString( id604, "sRGB IEC61966-2.1" );
    var id605 = charIDToTypeID( "Dcmn" );
    desc51.putObject( id590, id605, desc52 );
executeAction( id589, desc51, DialogModes.NO );

This script converts it into: ...

 


function action_0()
{
    var PS_Dcmn = charIDToTypeID ( "Dcmn" );
    var PS_Dpth = charIDToTypeID ( "Dpth" );
    var PS_Fl = charIDToTypeID ( "Fl  " );
    var PS_Hght = charIDToTypeID ( "Hght" );
    var PS_Md = charIDToTypeID ( "Md  " );
    var PS_Mk = charIDToTypeID ( "Mk  " );
    var PS_NUM_Rlt = charIDToTypeID ( "#Rlt" );
    var PS_NUM_Rsl = charIDToTypeID ( "#Rsl" );
    var PS_Nw = charIDToTypeID ( "Nw  " );
    var PS_RGBM = charIDToTypeID ( "RGBM" );
    var PS_Rslt = charIDToTypeID ( "Rslt" );
    var PS_Wdth = charIDToTypeID ( "Wdth" );
    var PS_Wht = charIDToTypeID ( "Wht " );
    var PS_pixelScaleFactor = stringIDToTypeID ( "pixelScaleFactor" );
    var PS_profile = stringIDToTypeID ( "profile" );

    // object-references-start
    // desc51
    //   desc52
    // object-references-end

    var desc51 = new ActionDescriptor ( ) ;
    var desc52 = new ActionDescriptor ( ) ;
    desc52 . putClass ( PS_Md , PS_RGBM ) ;
    desc52 . putUnitDouble ( PS_Wdth , PS_NUM_Rlt , 800.000000 ) ;
    desc52 . putUnitDouble ( PS_Hght , PS_NUM_Rlt , 600.000000 ) ;
    desc52 . putUnitDouble ( PS_Rslt , PS_NUM_Rsl , 72.000000 ) ;
    desc52 . putDouble ( PS_pixelScaleFactor , 1.000000 ) ;
    desc52 . putEnumerated ( PS_Fl , PS_Fl , PS_Wht ) ;
    desc52 . putInteger ( PS_Dpth , 8 ) ;
    desc52 . putString ( PS_profile , "sRGB IEC61966-2.1" ) ;
    desc51 . putObject ( PS_Nw , PS_Dcmn , desc52 ) ;
    executeAction ( PS_Mk , desc51 , DialogModes . NO ) ;
}


// function-end action_0 ----------------------------------------


// actions-start ----------------------------------------

action_0();

// actions-end ----------------------------------------

FAQs

  • How does the parsing work? I cheat. The script tokenizes each line of javascript with the the python tokenizer library. It's good enough to work for most cases.
  • Why are there spaces around the "." character? That case just isn't handled well, but it shouldn't affect the correctness of the cleaned-up JavaScript code.