Copy link to clipboard
Copied
I need to call a bat script using the File execute call and would like to be able to detect when the bat script completes. Is there a way to synchronously handle the File.execute() function in the photoshop csInterface?
var filename = File("path_to_bat_script");
filename.execute();
Any ideas would be greatly appreciated.
Thanks!
Copy link to clipboard
Copied
Have the bat file do something that the script can detect the bat command has done so the script knows the bat has finished its processing. Create a file in the script that the bat command will delete when it is done. Pause the script in a pause loop that test the existence of that file. After some time if the file still exists terminate the script for the bat command failed to delete the file something went wrong. If the file gets deleted continue the scripts processing.
I use a program potrace in a Photoshop script. So I can place in a vector smart object svg layer. I make sure the output file Protrace will create does not exists before I run the application and loop the script testing for the file to be created.
/* ==========================================================
// 2014 John J. McAssey (JJMack)
// ======================================================= */
// This script is supplied as is. It is provided as freeware.
// The author accepts no liability for any problems arising from its use.
/* Help Category note tag menu can be used to place script in automate menu
<javascriptresource>
<about>$$$/JavaScripts/potrace/About=JJMack's protrace Script.^r^rCopyright 2014 Mouseprints.^r^rDefine custom shape for the document and add shape layer on top </about>
<category>JJMack's Script</category>
</javascriptresource>
*/
// enable double-clicking from Mac Finder or Windows Explorer
#target photoshop // this command only works in Photoshop CS2 and higher
// bring application forward for double-click events
app.bringToFront();
// ensure at least one document open
if (!documents.length) alert('There are no documents open.', 'No Document');
else {
// declare Global variables
//main(); // at least one document exists proceed
app.activeDocument.suspendHistory('portace','main()');
}
///////////////////////////////////////////////////////////////////////////////
// main function //
///////////////////////////////////////////////////////////////////////////////
function main() {
// declare local variables
var orig_ruler_units = app.preferences.rulerUnits;
app.preferences.rulerUnits = Units.PIXELS; // Set the ruler units to PIXELS
try {code();}
catch(e) {alert(e + ': on line ' + e.line, 'Script Error', true);} // display error message if something goes wrong
app.preferences.rulerUnits = orig_ruler_units; // Reset units to original settings
}
///////////////////////////////////////////////////////////////////////////////
// main function end //
///////////////////////////////////////////////////////////////////////////////
function code() {
var scriptLocation = String(findScript());
var replacewithblanks = "";
while (scriptLocation.indexOf("%20") != -1 ) {
replacewithblanks = replacewithblanks + scriptLocation.substr(0, scriptLocation.indexOf("%20")) + " ";
scriptLocation = scriptLocation.substr(scriptLocation.indexOf("%20") + 3 ,);
}
scriptLocation = replacewithblanks + scriptLocation;
var path = "";
if(scriptLocation.indexOf("/") == 0 ) {
path = path + scriptLocation.substr(1, 1) + ":\\";
scriptLocation = scriptLocation.substr(scriptLocation.indexOf("/") + 3 ,);
}
while (scriptLocation.indexOf("/") != -1 ) {
path= path + scriptLocation.substr(0, scriptLocation.indexOf("/")) + "\\";
scriptLocation = scriptLocation.substr(scriptLocation.indexOf("/") + 1 ,);
}
scriptLocation = path + scriptLocation;
var program = path + "potrace.exe";
var options="-s";
//options=prompt("Enter any Potrace options",options); // Best to default and just work on source image
var Name = app.activeDocument.name; // get name
if (Name.indexOf(".") != -1 ) Name=Name.substr(0,Name.lastIndexOf(".")); // strip off any extension
Name=Name + " " + suffix(); // add a suffix
var testpbm = new File(Folder.temp + "/" + Name +".pbm"); // output pbm files
if (testpbm.exists) testpbm.remove(); // delete if it exists
var testsvg = new File(Folder.temp + "/" + Name +".svg"); // output svg files
if (testsvg.exists) testsvg.remove(); // delete if it exists
app.activeDocument.artLayers.add(); // Add a Layer so save will not switch documents
SavePBM(testpbm); // save active document as a pbm file
app.activeDocument.activeLayer.remove(); // remove added layer
if (!testpbm.exists) {alert('Could not save "' + testpbm + '"'); return;} // return if it did not save as a pbm
var pbmName = $.getenv("USERPROFILE") +"\\AppData\\Local\\Temp\\" + Name + ".pbm"; // develop standard windows file name of pbm file
StartPgm(path,program,options,pbmName); // Create svg file
app.refresh(); // slow down Photoshop with a refresh
for (var i=0;i<100000;i++) { if (!testsvg.exists) break; } // wait a short time before forcing a pause
if (!testsvg.exists) {alert('File "' + testsvg + '" was not cteated yet'); } // force a pause
if (!testsvg.exists) {alert('File "' + testsvg + '" was not cteated'); return;} // failed to create the svg file
/*
//app.load(testsvg); // load it into a document
openSVG(testsvg); // load it into a document use 72 dpi
var sb=DefineShape(Name); // define custom shape
if (sb!= false) { // shape bounds not false
var layers = app.activeDocument.layers; // get layers
app.activeDocument.activeLayer = layers[0]; // target top layer
addShape(Name,sb); // add shape layer to fit canvas
app.activeDocument.activeLayer.name=Name; // tell User the custom shape name is defined
}
else alert('Custom Shape define failed'); // tell User the custom shape define failed
*/
placeImage(testsvg)
}
///////////////////////////////////////////////////////////////////////////////
// Start external Program //
///////////////////////////////////////////////////////////////////////////////
function StartPgm(pth,pgm,opt,file){
var pgmfile = new File(pgm);
if(!pgmfile.exists){alert("Program missing 'protrace.exe'\rMust be with script in folder\r" + pth ); return; }
//alert('app.system(Start "' + pgm + '" /D "' + pth + '" /MIN "' + pgm + '" ' + opt + ' "' + file + '"' );
app.system('Start "' + pgm + '" /D "' + pth + '" /MIN "' + pgm + '" ' + opt + ' "' + file + '"' );
};
function suffix() {
var now = (new Date()).valueOf();
var future = (new Date()).valueOf();
while(future == now){
future = (new Date()).valueOf();
}
return future;
}
///////////////////////////////////////////////////////////////////////////////
// Helper functions mostly scriptlistener Action manager code //
///////////////////////////////////////////////////////////////////////////////
function SavePBM(file){
var idsave = charIDToTypeID( "save" );
var desc10 = new ActionDescriptor();
var idAs = charIDToTypeID( "As " );
desc10.putString( idAs, """Portable Bit Map""" );
var idIn = charIDToTypeID( "In " );
desc10.putPath( idIn, new File( file ) );
var idDocI = charIDToTypeID( "DocI" );
desc10.putInteger( idDocI, 344 );
var idsaveStage = stringIDToTypeID( "saveStage" );
var idsaveStageType = stringIDToTypeID( "saveStageType" );
var idsaveBegin = stringIDToTypeID( "saveBegin" );
desc10.putEnumerated( idsaveStage, idsaveStageType, idsaveBegin );
executeAction( idsave, desc10, DialogModes.NO );
var idsave = charIDToTypeID( "save" );
var desc11 = new ActionDescriptor();
var idAs = charIDToTypeID( "As " );
desc11.putString( idAs, """Portable Bit Map""" );
var idIn = charIDToTypeID( "In " );
desc11.putPath( idIn, new File( file ) );
var idDocI = charIDToTypeID( "DocI" );
desc11.putInteger( idDocI, 344 );
var idsaveStage = stringIDToTypeID( "saveStage" );
var idsaveStageType = stringIDToTypeID( "saveStageType" );
var idsaveSucceeded = stringIDToTypeID( "saveSucceeded" );
desc11.putEnumerated( idsaveStage, idsaveStageType, idsaveSucceeded );
executeAction( idsave, desc11, DialogModes.NO );
}
function openEPS(file) {
var idOpn = charIDToTypeID( "Opn " );
var desc10 = new ActionDescriptor();
var idnull = charIDToTypeID( "null" );
desc10.putPath( idnull, new File( file ) );
var idAs = charIDToTypeID( "As " );
var desc11 = new ActionDescriptor();
var idRslt = charIDToTypeID( "Rslt" );
var idRsl = charIDToTypeID( "#Rsl" );
desc11.putUnitDouble( idRslt, idRsl, 72.000000 );
var idAntA = charIDToTypeID( "AntA" );
desc11.putBoolean( idAntA, true );
var idEPSG = charIDToTypeID( "EPSG" );
desc10.putObject( idAs, idEPSG, desc11 );
var idDocI = charIDToTypeID( "DocI" );
desc10.putInteger( idDocI, 545 );
executeAction( idOpn, desc10, DialogModes.NO );
}
function openSVG(file) {
// =======================================================
var idOpn = charIDToTypeID( "Opn " );
var desc139 = new ActionDescriptor();
var idnull = charIDToTypeID( "null" );
desc139.putPath( idnull, new File( file ) );
var idAs = charIDToTypeID( "As " );
var desc140 = new ActionDescriptor();
var idRslt = charIDToTypeID( "Rslt" );
var idRsl = charIDToTypeID( "#Rsl" );
desc140.putUnitDouble( idRslt, idRsl, 72.000000 );
var idMd = charIDToTypeID( "Md " );
var idClrS = charIDToTypeID( "ClrS" );
var idRGBC = charIDToTypeID( "RGBC" );
desc140.putEnumerated( idMd, idClrS, idRGBC );
var idAntA = charIDToTypeID( "AntA" );
desc140.putBoolean( idAntA, true );
var idsvgFormat = stringIDToTypeID( "svgFormat" );
desc139.putObject( idAs, idsvgFormat, desc140 );
var idDocI = charIDToTypeID( "DocI" );
desc139.putInteger( idDocI, 644 );
executeAction( idOpn, desc139, DialogModes.NO );
}
function DefineShape(name) {
var rc=false; // set return code to false
try {
// Select Current Layers Transparency
var idsetd = charIDToTypeID( "setd" );
var desc40 = new ActionDescriptor();
var idnull = charIDToTypeID( "null" );
var ref12 = new ActionReference();
var idChnl = charIDToTypeID( "Chnl" );
var idfsel = charIDToTypeID( "fsel" );
ref12.putProperty( idChnl, idfsel );
desc40.putReference( idnull, ref12 );
var idT = charIDToTypeID( "T " );
var ref13 = new ActionReference();
var idChnl = charIDToTypeID( "Chnl" );
var idChnl = charIDToTypeID( "Chnl" );
var idTrsp = charIDToTypeID( "Trsp" );
ref13.putEnumerated( idChnl, idChnl, idTrsp );
desc40.putReference( idT, ref13 );
executeAction( idsetd, desc40, DialogModes.NO );
var SB = app.activeDocument.selection.bounds; // Get the bounds of the selection
top=SB[1].value;
left=SB[0].value;
bottom=SB[3].value;
right=SB[2].value;
rc=top + ":" + left + ":" + bottom + ":" + right;
// Convert selection to path
var idMk = charIDToTypeID( "Mk " );
var desc41 = new ActionDescriptor();
var idnull = charIDToTypeID( "null" );
var ref14 = new ActionReference();
var idPath = charIDToTypeID( "Path" );
ref14.putClass( idPath );
desc41.putReference( idnull, ref14 );
var idFrom = charIDToTypeID( "From" );
var ref15 = new ActionReference();
var idcsel = charIDToTypeID( "csel" );
var idfsel = charIDToTypeID( "fsel" );
ref15.putProperty( idcsel, idfsel );
desc41.putReference( idFrom, ref15 );
var idTlrn = charIDToTypeID( "Tlrn" );
var idPxl = charIDToTypeID( "#Pxl" );
desc41.putUnitDouble( idTlrn, idPxl, 2.000000 );
executeAction( idMk, desc41, DialogModes.NO );
// would be good to delete any existing custom shape with this name but Photoshop
// deletes custom shapes by index not name.
// So I add a suffix to the name to make shape names unique.
// define custom shape
var idMk = charIDToTypeID( "Mk " );
var desc42 = new ActionDescriptor();
var idnull = charIDToTypeID( "null" );
var ref16 = new ActionReference();
var idcustomShape = stringIDToTypeID( "customShape" );
ref16.putClass( idcustomShape );
desc42.putReference( idnull, ref16 );
var idUsng = charIDToTypeID( "Usng" );
var ref17 = new ActionReference();
var idPrpr = charIDToTypeID( "Prpr" );
var idfsel = charIDToTypeID( "fsel" );
ref17.putProperty( idPrpr, idfsel );
var idDcmn = charIDToTypeID( "Dcmn" );
var idOrdn = charIDToTypeID( "Ordn" );
var idTrgt = charIDToTypeID( "Trgt" );
ref17.putEnumerated( idDcmn, idOrdn, idTrgt );
desc42.putReference( idUsng, ref17 );
var idNm = charIDToTypeID( "Nm " );
desc42.putString( idNm, name );
executeAction( idMk, desc42, DialogModes.NO );
}
//catch(e) {alert(e + ': on line ' + e.line, 'Script Error', true); rc=false; } // display error message if something goes wrong
catch(e) { rc=false; } // set return code false
app.activeDocument.close(SaveOptions.DONOTSAVECHANGES); //close image without saving changes
return rc ;
}
function addShape(name,bounds) {
top=bounds.substr(0,bounds.indexOf(":")); bounds=bounds.substr(bounds.indexOf(":")+1);
left=bounds.substr(0,bounds.indexOf(":")); bounds=bounds.substr(bounds.indexOf(":")+1);
bottom=bounds.substr(0,bounds.indexOf(":")); right=bounds.substr(bounds.indexOf(":")+1);
// Add a Shape at resolution Black Fill no stroke
var idMk = charIDToTypeID( "Mk " );
var desc139 = new ActionDescriptor();
var idnull = charIDToTypeID( "null" );
var ref69 = new ActionReference();
var idcontentLayer = stringIDToTypeID( "contentLayer" );
ref69.putClass( idcontentLayer );
desc139.putReference( idnull, ref69 );
var idUsng = charIDToTypeID( "Usng" );
var desc140 = new ActionDescriptor();
var idType = charIDToTypeID( "Type" );
var desc141 = new ActionDescriptor();
var idClr = charIDToTypeID( "Clr " );
var desc142 = new ActionDescriptor();
var idRd = charIDToTypeID( "Rd " );
desc142.putDouble( idRd, 0.000000 );
var idGrn = charIDToTypeID( "Grn " );
desc142.putDouble( idGrn, 0.000000 );
var idBl = charIDToTypeID( "Bl " );
desc142.putDouble( idBl, 0.000000 );
var idRGBC = charIDToTypeID( "RGBC" );
desc141.putObject( idClr, idRGBC, desc142 );
var idsolidColorLayer = stringIDToTypeID( "solidColorLayer" );
desc140.putObject( idType, idsolidColorLayer, desc141 );
var idShp = charIDToTypeID( "Shp " );
var desc143 = new ActionDescriptor();
var idNm = charIDToTypeID( "Nm " );
desc143.putString( idNm, name );
var idTop = charIDToTypeID( "Top " );
var idPxl = charIDToTypeID( "#Pxl" );
desc143.putUnitDouble( idTop, idPxl, top );
var idLeft = charIDToTypeID( "Left" );
var idPxl = charIDToTypeID( "#Pxl" );
desc143.putUnitDouble( idLeft, idPxl, left );
var idBtom = charIDToTypeID( "Btom" );
var idPxl = charIDToTypeID( "#Pxl" );
desc143.putUnitDouble( idBtom, idPxl, bottom );
var idRght = charIDToTypeID( "Rght" );
var idPxl = charIDToTypeID( "#Pxl" );
desc143.putUnitDouble( idRght, idPxl, right );
var idcustomShape = stringIDToTypeID( "customShape" );
desc140.putObject( idShp, idcustomShape, desc143 );
var idstrokeStyle = stringIDToTypeID( "strokeStyle" );
var desc144 = new ActionDescriptor();
var idstrokeStyleVersion = stringIDToTypeID( "strokeStyleVersion" );
desc144.putInteger( idstrokeStyleVersion, 2 );
var idstrokeEnabled = stringIDToTypeID( "strokeEnabled" );
desc144.putBoolean( idstrokeEnabled, false );
var idfillEnabled = stringIDToTypeID( "fillEnabled" );
desc144.putBoolean( idfillEnabled, true );
var idstrokeStyleLineWidth = stringIDToTypeID( "strokeStyleLineWidth" );
var idPnt = charIDToTypeID( "#Pnt" );
desc144.putUnitDouble( idstrokeStyleLineWidth, idPnt, 2.000000 );
var idstrokeStyleLineDashOffset = stringIDToTypeID( "strokeStyleLineDashOffset" );
var idPnt = charIDToTypeID( "#Pnt" );
desc144.putUnitDouble( idstrokeStyleLineDashOffset, idPnt, 0.000000 );
var idstrokeStyleMiterLimit = stringIDToTypeID( "strokeStyleMiterLimit" );
desc144.putDouble( idstrokeStyleMiterLimit, 100.000000 );
var idstrokeStyleLineCapType = stringIDToTypeID( "strokeStyleLineCapType" );
var idstrokeStyleLineCapType = stringIDToTypeID( "strokeStyleLineCapType" );
var idstrokeStyleButtCap = stringIDToTypeID( "strokeStyleButtCap" );
desc144.putEnumerated( idstrokeStyleLineCapType, idstrokeStyleLineCapType, idstrokeStyleButtCap );
var idstrokeStyleLineJoinType = stringIDToTypeID( "strokeStyleLineJoinType" );
var idstrokeStyleLineJoinType = stringIDToTypeID( "strokeStyleLineJoinType" );
var idstrokeStyleMiterJoin = stringIDToTypeID( "strokeStyleMiterJoin" );
desc144.putEnumerated( idstrokeStyleLineJoinType, idstrokeStyleLineJoinType, idstrokeStyleMiterJoin );
var idstrokeStyleLineAlignment = stringIDToTypeID( "strokeStyleLineAlignment" );
var idstrokeStyleLineAlignment = stringIDToTypeID( "strokeStyleLineAlignment" );
var idstrokeStyleAlignInside = stringIDToTypeID( "strokeStyleAlignInside" );
desc144.putEnumerated( idstrokeStyleLineAlignment, idstrokeStyleLineAlignment, idstrokeStyleAlignInside );
var idstrokeStyleScaleLock = stringIDToTypeID( "strokeStyleScaleLock" );
desc144.putBoolean( idstrokeStyleScaleLock, false );
var idstrokeStyleStrokeAdjust = stringIDToTypeID( "strokeStyleStrokeAdjust" );
desc144.putBoolean( idstrokeStyleStrokeAdjust, false );
var idstrokeStyleLineDashSet = stringIDToTypeID( "strokeStyleLineDashSet" );
var list13 = new ActionList();
desc144.putList( idstrokeStyleLineDashSet, list13 );
var idstrokeStyleBlendMode = stringIDToTypeID( "strokeStyleBlendMode" );
var idBlnM = charIDToTypeID( "BlnM" );
var idNrml = charIDToTypeID( "Nrml" );
desc144.putEnumerated( idstrokeStyleBlendMode, idBlnM, idNrml );
var idstrokeStyleOpacity = stringIDToTypeID( "strokeStyleOpacity" );
var idPrc = charIDToTypeID( "#Prc" );
desc144.putUnitDouble( idstrokeStyleOpacity, idPrc, 100.000000 );
var idstrokeStyleContent = stringIDToTypeID( "strokeStyleContent" );
var desc145 = new ActionDescriptor();
var idClr = charIDToTypeID( "Clr " );
var desc146 = new ActionDescriptor();
var idRd = charIDToTypeID( "Rd " );
desc146.putDouble( idRd, 85.998688 );
var idGrn = charIDToTypeID( "Grn " );
desc146.putDouble( idGrn, 37.003326 );
var idBl = charIDToTypeID( "Bl " );
desc146.putDouble( idBl, 17.999725 );
var idRGBC = charIDToTypeID( "RGBC" );
desc145.putObject( idClr, idRGBC, desc146 );
var idsolidColorLayer = stringIDToTypeID( "solidColorLayer" );
desc144.putObject( idstrokeStyleContent, idsolidColorLayer, desc145 );
var idstrokeStyleResolution = stringIDToTypeID( "strokeStyleResolution" );
desc144.putDouble( idstrokeStyleResolution, 182.880005 );
var idstrokeStyle = stringIDToTypeID( "strokeStyle" );
desc140.putObject( idstrokeStyle, idstrokeStyle, desc144 );
var idcontentLayer = stringIDToTypeID( "contentLayer" );
desc139.putObject( idUsng, idcontentLayer, desc140 );
executeAction( idMk, desc139, DialogModes.NO );
// Toggle visibility of other layers
var idShw = charIDToTypeID( "Shw " );
var desc694 = new ActionDescriptor();
var idnull = charIDToTypeID( "null" );
var list96 = new ActionList();
var ref359 = new ActionReference();
var idLyr = charIDToTypeID( "Lyr " );
var idOrdn = charIDToTypeID( "Ordn" );
var idTrgt = charIDToTypeID( "Trgt" );
ref359.putEnumerated( idLyr, idOrdn, idTrgt );
list96.putReference( ref359 );
desc694.putList( idnull, list96 );
var idTglO = charIDToTypeID( "TglO" );
desc694.putBoolean( idTglO, true );
executeAction( idShw, desc694, DialogModes.NO );
}
// Find the location where this script resides
function findScript() {
var where = "";
try {
FORCEERROR = FORCERRROR;
}
catch(err) {
// alert(err.fileName);
// alert(File(err.fileName).exists);
where = File(err.fileName);
}
return where ;
}
function placeImage(file) {
/*
// Insure Photoshop Preference is not resize paste place
var idsetd = charIDToTypeID( "setd" );
var desc7 = new ActionDescriptor();
var idnull = charIDToTypeID( "null" );
var ref4 = new ActionReference();
var idPrpr = charIDToTypeID( "Prpr" );
var idGnrP = charIDToTypeID( "GnrP" );
ref4.putProperty( idPrpr, idGnrP );
var idcapp = charIDToTypeID( "capp" );
var idOrdn = charIDToTypeID( "Ordn" );
var idTrgt = charIDToTypeID( "Trgt" );
ref4.putEnumerated( idcapp, idOrdn, idTrgt );
desc7.putReference( idnull, ref4 );
var idT = charIDToTypeID( "T " );
var desc8 = new ActionDescriptor();
var idresizePastePlace = stringIDToTypeID( "resizePastePlace" );
desc8.putBoolean( idresizePastePlace, false );
var idGnrP = charIDToTypeID( "GnrP" );
desc7.putObject( idT, idGnrP, desc8 );
executeAction( idsetd, desc7, DialogModes.NO );
*/
// =======avoid bug in cs2 and maybe CS4 ==================================
var idslct = charIDToTypeID( "slct" );
var desc5 = new ActionDescriptor();
var idnull = charIDToTypeID( "null" );
var ref3 = new ActionReference();
var idChnl = charIDToTypeID( "Chnl" );
var idChnl = charIDToTypeID( "Chnl" );
var idRGB = charIDToTypeID( "RGB " );
ref3.putEnumerated( idChnl, idChnl, idRGB );
desc5.putReference( idnull, ref3 );
var idMkVs = charIDToTypeID( "MkVs" );
desc5.putBoolean( idMkVs, false );
executeAction( idslct, desc5, DialogModes.NO );
// Place in the file
var idPlc = charIDToTypeID( "Plc " );
var desc5 = new ActionDescriptor();
var idnull = charIDToTypeID( "null" );
desc5.putPath( idnull, new File( file ) );
var idFTcs = charIDToTypeID( "FTcs" );
var idQCSt = charIDToTypeID( "QCSt" );
var idQcsa = charIDToTypeID( "Qcsa" );
desc5.putEnumerated( idFTcs, idQCSt, idQcsa );
var idOfst = charIDToTypeID( "Ofst" );
var desc6 = new ActionDescriptor();
var idHrzn = charIDToTypeID( "Hrzn" );
var idPxl = charIDToTypeID( "#Pxl" );
desc6.putUnitDouble( idHrzn, idPxl, 0.000000 );
var idVrtc = charIDToTypeID( "Vrtc" );
var idPxl = charIDToTypeID( "#Pxl" );
desc6.putUnitDouble( idVrtc, idPxl, 0.000000 );
var idOfst = charIDToTypeID( "Ofst" );
desc5.putObject( idOfst, idOfst, desc6 );
executeAction( idPlc, desc5, DialogModes.NO );
// because can't get the scale of a smart object, reset to 100%
activeDocument.activeLayer.resize(100 ,100,AnchorPosition.MIDDLECENTER);
return app.activeDocument.activeLayer;
}