Skip to main content
Inspiring
September 28, 2023
Question

How to break try/catch when all tasks in try are not completed

  • September 28, 2023
  • 3 replies
  • 237 views

I try to flatten img and to check is there any shadows selection after i contract selection dor 2px, in case that it is true i want to inform that there is a selection, in other case i want to cacth erorr and alert there is no selection. Problem is that even there is no selection script still complete some steps from try. Is it possible to break somehow if result of try is false?

 

There is my code. Thanks in advance

 

try{

// FLATTEN IMAGE
var idFltI = charIDToTypeID( "FltI" );
executeAction( idFltI, undefined, DialogModes.NO );

//// Select Shadows
var idClrR = charIDToTypeID( "ClrR" );
    var desc22 = new ActionDescriptor();
    var idClrs = charIDToTypeID( "Clrs" );
    var idClrs = charIDToTypeID( "Clrs" );
    var idShdw = charIDToTypeID( "Shdw" );
    desc22.putEnumerated( idClrs, idClrs, idShdw );
    var idshadowsFuzziness = stringIDToTypeID( "shadowsFuzziness" );
    desc22.putInteger( idshadowsFuzziness, 20 );
    var idshadowsUpperLimit = stringIDToTypeID( "shadowsUpperLimit" );
    desc22.putInteger( idshadowsUpperLimit, 65 );
    var idcolorModel = stringIDToTypeID( "colorModel" );
    desc22.putInteger( idcolorModel, 0 );
executeAction( idClrR, desc22, DialogModes.NO );

////// Contract selection
var idCntc = charIDToTypeID( "Cntc" );
    var desc25 = new ActionDescriptor();
    var idBy = charIDToTypeID( "By  " );
    var idPxl = charIDToTypeID( "#Pxl" );
    desc25.putUnitDouble( idBy, idPxl, 2.000000 );
    var idselectionModifyEffectAtCanvasBounds = stringIDToTypeID( "selectionModifyEffectAtCanvasBounds" );
    desc25.putBoolean( idselectionModifyEffectAtCanvasBounds, false );
executeAction( idCntc, desc25, DialogModes.NO );

  alert("There is shadows")
}
catch(e){
    alert("There is no selection")
}

 

This topic has been closed for replies.

3 replies

c.pfaffenbichler
Community Expert
Community Expert
September 29, 2023

I am not sure I unerstand – if you want to make sure an active Selection exists in a file you can use a function to check just that. 

alert (hasSelection());
////// check for selection //////
function hasSelection(){
var ref10 = new ActionReference();
ref10.putProperty(stringIDToTypeID("property"), stringIDToTypeID("selection"));
ref10.putEnumerated( charIDToTypeID( "Dcmn" ), charIDToTypeID( "Ordn" ), charIDToTypeID( "Trgt" ) );
var docDesc = executeActionGet(ref10);
return docDesc.hasKey(stringIDToTypeID("selection"));
};
Stephen Marsh
Community Expert
Community Expert
September 28, 2023

Have you tried an if/else block rather than try/catch?

PedroMacro
Participating Frequently
September 28, 2023

I dont know if a break is possible, have you tried to just make it execute an action that does nothing? (like just save and close, or toggle view of a layer), If it has selection, it will execute the other stepts, but if it doesnt have a selection, it will just execute this "empty" action

Remember that some selections may not be visible, but it will detect a selection.