SLCFix.js

Discussion of the xtools Toolkit

Moderators: Tom, Kukurykus

xbytor

SLCFix.js

Post by xbytor »

From the attached file...

//
// SLCFix.js
// This script does some minor massaging of ScriptListener output
// primarily by substituting charIDToTypeID and stringIDToTypeID with
// cTID and sTID. In the process of doing this replacement, the
// 'var id##' style declarations are removed in the c/sTID calls
// placed inline. You'll need to copy the c/sTID definitions from
// this file into whatever file you decide to use the converted code.
//
// One other piece of corrective surgery is to canonicalize all
// filename strings to use '/' instead of '\' characters. I didn't bother
// messing with the drive names.
//
// The only thing left that I don't really have a solution for is Actions
/// that invoke scripts. For some reason, the return value of the script
// is placed in the action and gets output to the ScriptingListener log
// file as well. Search for 'jsMs' to see what I mean. Unforunately, in
// many case, the return value is effectively the last piece of textual
// code parsed, I think. There is not an easy way that I have found to
// remove this travesty after the fact, except to do it manually by replacing
// the code with an empty string, "". You can, however, remove it before
// the fact. Make the last line of your script files 'true;' or, like I do,
// the name of the script as a string, e.g. "SLCFix.js"; This has the nice
// added benefit of showing up in the debugger console if you are running
// the script from within the debugger.
//
// I've converted upto 20,000 lines of ScriptingListenerJS.log code in one
// pass with the only problems being the 'jsMs' garbage. That can, as I said
// before, be fixed manually.


and an example:

BEFORE
Code: Select all// =======================================================
var id386 = charIDToTypeID( "save" );
    var desc80 = new ActionDescriptor();
    var id387 = charIDToTypeID( "As  " );
        var desc81 = new ActionDescriptor();
        var id388 = stringIDToTypeID( "maximizeCompatibility" );
        desc81.putBoolean( id388, true );
    var id389 = charIDToTypeID( "Pht3" );
    desc80.putObject( id387, id389, desc81 );
    var id390 = charIDToTypeID( "In  " );
    desc80.putPath( id390, new File( "C:\\work\\Template04.psd" ) );
executeAction( id386, desc80, DialogModes.NO );

// =======================================================
var id391 = charIDToTypeID( "Mk  " );
    var desc82 = new ActionDescriptor();
    var id392 = charIDToTypeID( "Nw  " );
        var desc83 = new ActionDescriptor();
        var id393 = charIDToTypeID( "Md  " );
        var id394 = charIDToTypeID( "RGBM" );
        desc83.putClass( id393, id394 );
        var id395 = charIDToTypeID( "Wdth" );
        var id396 = charIDToTypeID( "#Rlt" );
        desc83.putUnitDouble( id395, id396, 432.000000 );
        var id397 = charIDToTypeID( "Hght" );
        var id398 = charIDToTypeID( "#Rlt" );
        desc83.putUnitDouble( id397, id398, 576.000000 );
        var id399 = charIDToTypeID( "Rslt" );
        var id400 = charIDToTypeID( "#Rsl" );
        desc83.putUnitDouble( id399, id400, 250.000000 );
        var id401 = stringIDToTypeID( "pixelScaleFactor" );
        desc83.putDouble( id401, 1.000000 );
        var id402 = charIDToTypeID( "Fl  " );
        var id403 = charIDToTypeID( "Fl  " );
        var id404 = charIDToTypeID( "Wht " );
        desc83.putEnumerated( id402, id403, id404 );
        var id405 = charIDToTypeID( "Dpth" );
        desc83.putInteger( id405, 8 );
        var id406 = stringIDToTypeID( "profile" );
        desc83.putString( id406, "Adobe RGB (1998)" );
    var id407 = charIDToTypeID( "Dcmn" );
    desc82.putObject( id392, id407, desc83 );
executeAction( id391, desc82, DialogModes.NO );

AFTER
Code: Select all// =======================================================
    var desc80 = new ActionDescriptor();
        var desc81 = new ActionDescriptor();
        desc81.putBoolean( sTID("maximizeCompatibility"), true );
    desc80.putObject( cTID("As  "), cTID("Pht3"), desc81 );
    desc80.putPath( cTID("In  "), new File( "C:/work/Template04.psd" ) );
executeAction( cTID("save"), desc80, DialogModes.NO );

// =======================================================
    var desc82 = new ActionDescriptor();
        var desc83 = new ActionDescriptor();
        desc83.putClass( cTID("Md  "), cTID("RGBM") );
        desc83.putUnitDouble( cTID("Wdth"), cTID("#Rlt"), 432.000000 );
        desc83.putUnitDouble( cTID("Hght"), cTID("#Rlt"), 576.000000 );
        desc83.putUnitDouble( cTID("Rslt"), cTID("#Rsl"), 250.000000 );
        desc83.putDouble( sTID("pixelScaleFactor"), 1.000000 );
        desc83.putEnumerated( cTID("Fl  "), cTID("Fl  "), cTID("Wht ") );
        desc83.putInteger( cTID("Dpth"), 8 );
        desc83.putString( sTID("profile"), "Adobe RGB (1998)" );
    desc82.putObject( cTID("Nw  "), cTID("Dcmn"), desc83 );
executeAction( cTID("Mk  "), desc82, DialogModes.NO );


Enjoy!
hello

SLCFix.js

Post by hello »

Sorry, but... How do you run this? Looks great.
xbytor

SLCFix.js

Post by xbytor »

Take a look at the 'main' function at the bottom of the file. There is a call to 'fixer.exec' you need to modify. The first parameter is the name of a file that contains the Script Listener code that you want to 'fix'. The second parameter is the name of a javascript file for the new code that will generated. There is no UI. Feel free to add some file selector dialogs so that it fits your work habits. For myself, I just pop open an emacs window and edit the script and run it. I may extend it later but for now it's working fine for me.
hello

SLCFix.js

Post by hello »

Nevermind. Before seeing this, I simply ran it straight through PS, made sure a ScriptingListenerJS.log file was present in the C:\work\ directory - and shazaam.

This is awesome, dude! I was wondering how the heck I could compile my script-listener methods down to be more discernable/compact. This does the trick!

FYI: I even ran this on a ScriptListenerJS.log file that was simply code I pulled from a function in one of my scripts... I already had comment tags over each var id, so I'd be able to trace each one in the nasty list it was amounting to... Happy to say that your script didn't eliminate any of my comments. Nicely done, sir.
xbytor

SLCFix.js

Post by xbytor »

I already have plans for a next rev. Instead of seeing stuff like
Code: Select allcTID("FxCM");
cTID("BrgC");

you'll have:
Code: Select allPSType.FPXCompress
PSClass.BrightnessContrast
xbytor

SLCFix.js

Post by xbytor »

Here's the new rev. This makes the code even easier to read, I would hope. The 'jsMs' problem still exists and looks way to difficult to solve by this script. The PSClass-style symbols can be turned off by manually setting 'SLCFix.usePSConstants' to 'false' in code.

Enjoy!

AFTER
Code: Select all// =======================================================
    var desc80 = new ActionDescriptor();
        var desc81 = new ActionDescriptor();
        desc81.putBoolean( PSString.maximizeCompatibility, true );
    desc80.putObject( PSKey.As, PSClass.Photoshop35Format, desc81 );
    desc80.putPath( PSEnum.StampIn, new File( "C:/work/Template04.psd" ) );
executeAction( PSEvent.Save, desc80, DialogModes.NO );

// =======================================================
    var desc82 = new ActionDescriptor();
        var desc83 = new ActionDescriptor();
        desc83.putClass( PSClass.Mode, PSClass.RGBColorMode );
        desc83.putUnitDouble( PSKey.Width, PSUnit.Distance, 432.000000 );
        desc83.putUnitDouble( PSKey.Height, PSUnit.Distance, 576.000000 );
        desc83.putUnitDouble( PSKey.Resolution, PSUnit.Density, 250.000000 );
        desc83.putDouble( PSString.PixelScaleFactor, 1.000000 );
        desc83.putEnumerated( PSEvent.Fill, PSEvent.Fill, PSEnum.White );
        desc83.putInteger( PSKey.Depth, 8 );
        desc83.putString( PSString.profile, "Adobe RGB (1998)" );
    desc82.putObject( PSKey.New, PSClass.Document, desc83 );
executeAction( PSEvent.Make, desc82, DialogModes.NO );

cTID = function(s) { return app.charIDToTypeID(s); };
sTID = function(s) { return app.stringIDToTypeID(s); };
PSClass = function() {};
PSEnum = function() {};
PSEvent = function() {};
PSForm = function() {};
PSKey = function() {};
PSType = function() {};
PSUnit = function() {};
PSString = function() {};

PSClass.Document = cTID('Dcmn');
PSClass.Mode = cTID('Md  ');
PSClass.Photoshop35Format = cTID('Pht3');
PSClass.RGBColorMode = cTID('RGBM');
PSEnum.StampIn = cTID('In  ');
PSEnum.White = cTID('Wht ');
PSEvent.Fill = cTID('Fl  ');
PSEvent.Make = cTID('Mk  ');
PSEvent.Save = cTID('save');
PSKey.As = cTID('As  ');
PSKey.Depth = cTID('Dpth');
PSKey.Height = cTID('Hght');
PSKey.New = cTID('Nw  ');
PSKey.Resolution = cTID('Rslt');
PSKey.Width = cTID('Wdth');
PSString.PixelScaleFactor = sTID('pixelScaleFactor');
PSString.maximizeCompatibility = sTID('maximizeCompatibility');
PSString.profile = sTID('profile');
PSUnit.Density = cTID('#Rsl');
PSUnit.Distance = cTID('#Rlt');
xbytor

SLCFix.js

Post by xbytor »

A couple notes on the substitutions:

There a some cases where a symid (e.g. 'Fl ') is used as different kinds of things (e.g. PSEvent.Fill or PSKey.Fill). The replacement that is generated is the first one found when searching an internal mapping table. While the mappings are equivalent, they can be misleading.

There are some cases where the replacements look identical, but the symids they map to are different. For instance PSKey.Rotate and PSEnum.Rotate both map to 'Rtte'. However, PSEnum.Rotate maps to 'Rtt '. This works as it should.

The mappings always result in correctly executing code, even if I have to simply map it to a call to c/sTID.
hello

SLCFix.js

Post by hello »

I think perhaps you'll want to include to the newbies that you'll have to manually set your variables with these changes. If not, they'll be receiving errors, not understanding why the 'freshened' code is going wrong.

Just a suggestion.

I would think you could also add the substuted variables at the top of the freshened code to completely automate this useful tool.

Thanks.
xbytor

SLCFix.js

Post by xbytor »

Here's a new rev of SLCFix, complete with full symbol substitution.

Enjoy!

-X