/***  Copyright 2015 Tony Kuyper  */

// example default settings if you wanted to run the script below just in the ESTK to make sure its working. 
//runSharpen(100,50, "radio3")

ext_runSharpen = runSharpen;   // this sets the JSX function up so the Extension will read it. 

// Here is your main function.
function runSharpen(inPixelDimension,inOpacitySetting,selectedRadio)
{

        // bring values in from panel 
        var pixelDimension = inPixelDimension;  // brings in pixel dimension
        var opacitySetting = inOpacitySetting;  // brings in opacity setting
        // set all the radios to default to false,  except the one that was selected in the UI 
        var radio1 = false; var radio2 = false; var radio3 = false; 
        if  (selectedRadio == "radio1") { radio1 = true; }
        if  (selectedRadio == "radio2") { radio2 = true; }
        if  (selectedRadio == "radio3") { radio3 = true; }       
    
// below is the code from your script, with a few minor changes in the variable names to match whats comming out of the HTML instead of scriptUI 

        // Determine if dimension entered is between 50 and 9999 inclusive
        if ((pixelDimension >=50 && pixelDimension <= 9999)&&(opacitySetting>0 && opacitySetting<=100)){
            //Make sure string captured for pixelDimension.text is a number value, and not a string value. pixelDimension becomes the second down-sizing dimension that will be inserted into ScriptListener script
            var secondNumber = Number(pixelDimension);
            //Make sure string captured for opacitySetting.text is a number value, and not a string value. layerOpacity becomes becomes the sharpening value of the Sharpen #2 layer.
            var layerOpacity = Number(opacitySetting);
            // Calculate the first down-sizing value to insert into the ScriptListener script.
            var firstNumber = secondNumber * 1.667;
            //Evaluate if radio1 button is selected, which is for down-sizing using a vertical dimension, and if it is, close window and run ScriptListener script to downsize and sharpen.
            if (radio1 == true) 
            {
     
    // Start ScriptListener capture           
// ===================================================
var idDplc = charIDToTypeID( "Dplc" );
    var desc2 = new ActionDescriptor();
    var idnull = charIDToTypeID( "null" );
        var ref1 = new ActionReference();
        var idDcmn = charIDToTypeID( "Dcmn" );
        var idOrdn = charIDToTypeID( "Ordn" );
        var idFrst = charIDToTypeID( "Frst" );
        ref1.putEnumerated( idDcmn, idOrdn, idFrst );
    desc2.putReference( idnull, ref1 );
executeAction( idDplc, desc2, DialogModes.NO );

    app.activeDocument.suspendHistory("Resize / Sharpen", "tkaction()");

function tkaction() {

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

try {

// ===================================================
var idCnvM = charIDToTypeID( "CnvM" );
    var desc4 = new ActionDescriptor();
    var idT = charIDToTypeID( "T   " );
    var idRGBM = charIDToTypeID( "RGBM" );
    desc4.putClass( idT, idRGBM );
executeAction( idCnvM, desc4, DialogModes.NO );  }

catch (e) {}

// ===================================================
var idImgS = charIDToTypeID( "ImgS" );
    var desc3 = new ActionDescriptor();
    var idHght = charIDToTypeID( "Hght" );
    var idPxl = charIDToTypeID( "#Pxl" );
    desc3.putUnitDouble( idHght, idPxl, firstNumber );
    var idRslt = charIDToTypeID( "Rslt" );
    var idRsl = charIDToTypeID( "#Rsl" );
    desc3.putUnitDouble( idRslt, idRsl, 72.000000 );
    var idscaleStyles = stringIDToTypeID( "scaleStyles" );
    desc3.putBoolean( idscaleStyles, true );
    var idCnsP = charIDToTypeID( "CnsP" );
    desc3.putBoolean( idCnsP, true );
    var idIntr = charIDToTypeID( "Intr" );
    var idIntp = charIDToTypeID( "Intp" );
    var idBcbc = charIDToTypeID( "Bcbc" );
    desc3.putEnumerated( idIntr, idIntp, idBcbc );
executeAction( idImgS, desc3, DialogModes.NO );

// ===================================================
var idDplc = charIDToTypeID( "Dplc" );
    var desc4 = new ActionDescriptor();
    var idnull = charIDToTypeID( "null" );
        var ref2 = new ActionReference();
        var idLyr = charIDToTypeID( "Lyr " );
        var idOrdn = charIDToTypeID( "Ordn" );
        var idTrgt = charIDToTypeID( "Trgt" );
        ref2.putEnumerated( idLyr, idOrdn, idTrgt );
    desc4.putReference( idnull, ref2 );
    var idNm = charIDToTypeID( "Nm  " );
    desc4.putString( idNm, """Sharpen #1""" );
    var idVrsn = charIDToTypeID( "Vrsn" );
    desc4.putInteger( idVrsn, 5 );
executeAction( idDplc, desc4, DialogModes.NO );

// ===================================================
var idShrp = charIDToTypeID( "Shrp" );
executeAction( idShrp, undefined, DialogModes.NO );

// ===================================================
var idDplc = charIDToTypeID( "Dplc" );
    var desc5 = new ActionDescriptor();
    var idnull = charIDToTypeID( "null" );
        var ref3 = new ActionReference();
        var idLyr = charIDToTypeID( "Lyr " );
        var idOrdn = charIDToTypeID( "Ordn" );
        var idTrgt = charIDToTypeID( "Trgt" );
        ref3.putEnumerated( idLyr, idOrdn, idTrgt );
    desc5.putReference( idnull, ref3 );
    var idNm = charIDToTypeID( "Nm  " );
    desc5.putString( idNm, """Sharpen #2""" );
    var idVrsn = charIDToTypeID( "Vrsn" );
    desc5.putInteger( idVrsn, 5 );
executeAction( idDplc, desc5, DialogModes.NO );

// ===================================================
var idImgS = charIDToTypeID( "ImgS" );
    var desc6 = new ActionDescriptor();
    var idHght = charIDToTypeID( "Hght" );
    var idPxl = charIDToTypeID( "#Pxl" );
    desc6.putUnitDouble( idHght, idPxl, secondNumber );
    var idscaleStyles = stringIDToTypeID( "scaleStyles" );
    desc6.putBoolean( idscaleStyles, true );
    var idCnsP = charIDToTypeID( "CnsP" );
    desc6.putBoolean( idCnsP, true );
    var idIntr = charIDToTypeID( "Intr" );
    var idIntp = charIDToTypeID( "Intp" );
    var idBcbc = charIDToTypeID( "Bcbc" );
    desc6.putEnumerated( idIntr, idIntp, idBcbc );
executeAction( idImgS, desc6, DialogModes.NO );

// ===================================================
var idShrp = charIDToTypeID( "Shrp" );
executeAction( idShrp, undefined, DialogModes.NO );

// ===================================================
var idMk = charIDToTypeID( "Mk  " );
    var desc7 = new ActionDescriptor();
    var idNw = charIDToTypeID( "Nw  " );
    var idChnl = charIDToTypeID( "Chnl" );
    desc7.putClass( idNw, idChnl );
    var idAt = charIDToTypeID( "At  " );
        var ref4 = new ActionReference();
        var idChnl = charIDToTypeID( "Chnl" );
        var idChnl = charIDToTypeID( "Chnl" );
        var idMsk = charIDToTypeID( "Msk " );
        ref4.putEnumerated( idChnl, idChnl, idMsk );
    desc7.putReference( idAt, ref4 );
    var idUsng = charIDToTypeID( "Usng" );
    var idUsrM = charIDToTypeID( "UsrM" );
    var idRvlA = charIDToTypeID( "RvlA" );
    desc7.putEnumerated( idUsng, idUsrM, idRvlA );
executeAction( idMk, desc7, DialogModes.NO );

// ===================================================
var idAppI = charIDToTypeID( "AppI" );
    var desc8 = new ActionDescriptor();
    var idWith = charIDToTypeID( "With" );
        var desc9 = new ActionDescriptor();
        var idT = charIDToTypeID( "T   " );
            var ref5 = new ActionReference();
            var idChnl = charIDToTypeID( "Chnl" );
            var idChnl = charIDToTypeID( "Chnl" );
            var idRGB = charIDToTypeID( "RGB " );
            ref5.putEnumerated( idChnl, idChnl, idRGB );
            var idLyr = charIDToTypeID( "Lyr " );
            var idOrdn = charIDToTypeID( "Ordn" );
            var idMrgd = charIDToTypeID( "Mrgd" );
            ref5.putEnumerated( idLyr, idOrdn, idMrgd );
        desc9.putReference( idT, ref5 );
        var idInvr = charIDToTypeID( "Invr" );
        desc9.putBoolean( idInvr, true );
        var idPrsT = charIDToTypeID( "PrsT" );
        desc9.putBoolean( idPrsT, true );
    var idClcl = charIDToTypeID( "Clcl" );
    desc8.putObject( idWith, idClcl, desc9 );
executeAction( idAppI, desc8, DialogModes.NO );

// ===================================================
var idMk = charIDToTypeID( "Mk  " );
    var desc10 = new ActionDescriptor();
    var idnull = charIDToTypeID( "null" );
        var ref6 = new ActionReference();
        var idAdjL = charIDToTypeID( "AdjL" );
        ref6.putClass( idAdjL );
    desc10.putReference( idnull, ref6 );
    var idUsng = charIDToTypeID( "Usng" );
        var desc11 = new ActionDescriptor();
        var idType = charIDToTypeID( "Type" );
            var desc12 = new ActionDescriptor();
            var idpresetKind = stringIDToTypeID( "presetKind" );
            var idpresetKindType = stringIDToTypeID( "presetKindType" );
            var idpresetKindDefault = stringIDToTypeID( "presetKindDefault" );
            desc12.putEnumerated( idpresetKind, idpresetKindType, idpresetKindDefault );
        var idCrvs = charIDToTypeID( "Crvs" );
        desc11.putObject( idType, idCrvs, desc12 );
    var idAdjL = charIDToTypeID( "AdjL" );
    desc10.putObject( idUsng, idAdjL, desc11 );
executeAction( idMk, desc10, DialogModes.NO );

// ===================================================
var idMk = charIDToTypeID( "Mk  " );
    var desc13 = new ActionDescriptor();
    var idnull = charIDToTypeID( "null" );
        var ref7 = new ActionReference();
        var idAdjL = charIDToTypeID( "AdjL" );
        ref7.putClass( idAdjL );
    desc13.putReference( idnull, ref7 );
    var idUsng = charIDToTypeID( "Usng" );
        var desc14 = new ActionDescriptor();
        var idType = charIDToTypeID( "Type" );
            var desc15 = new ActionDescriptor();
            var idpresetKind = stringIDToTypeID( "presetKind" );
            var idpresetKindType = stringIDToTypeID( "presetKindType" );
            var idpresetKindDefault = stringIDToTypeID( "presetKindDefault" );
            desc15.putEnumerated( idpresetKind, idpresetKindType, idpresetKindDefault );
        var idLvls = charIDToTypeID( "Lvls" );
        desc14.putObject( idType, idLvls, desc15 );
    var idAdjL = charIDToTypeID( "AdjL" );
    desc13.putObject( idUsng, idAdjL, desc14 );
executeAction( idMk, desc13, DialogModes.NO );

// ===================================================
var idMk = charIDToTypeID( "Mk  " );
    var desc16 = new ActionDescriptor();
    var idnull = charIDToTypeID( "null" );
        var ref8 = new ActionReference();
        var idAdjL = charIDToTypeID( "AdjL" );
        ref8.putClass( idAdjL );
    desc16.putReference( idnull, ref8 );
    var idUsng = charIDToTypeID( "Usng" );
        var desc17 = new ActionDescriptor();
        var idType = charIDToTypeID( "Type" );
            var desc18 = new ActionDescriptor();
            var idpresetKind = stringIDToTypeID( "presetKind" );
            var idpresetKindType = stringIDToTypeID( "presetKindType" );
            var idpresetKindDefault = stringIDToTypeID( "presetKindDefault" );
            desc18.putEnumerated( idpresetKind, idpresetKindType, idpresetKindDefault );
            var idClrz = charIDToTypeID( "Clrz" );
            desc18.putBoolean( idClrz, false );
        var idHStr = charIDToTypeID( "HStr" );
        desc17.putObject( idType, idHStr, desc18 );
    var idAdjL = charIDToTypeID( "AdjL" );
    desc16.putObject( idUsng, idAdjL, desc17 );
executeAction( idMk, desc16, DialogModes.NO );

// ===================================================
var idslct = charIDToTypeID( "slct" );
    var desc19 = new ActionDescriptor();
    var idnull = charIDToTypeID( "null" );
        var ref9 = new ActionReference();
        var idLyr = charIDToTypeID( "Lyr " );
        ref9.putName( idLyr, "Sharpen #2" );
    desc19.putReference( idnull, ref9 );
    var idMkVs = charIDToTypeID( "MkVs" );
    desc19.putBoolean( idMkVs, false );
executeAction( idslct, desc19, DialogModes.NO );

// ===================================================
var idslct = charIDToTypeID( "slct" );
    var desc20 = new ActionDescriptor();
    var idnull = charIDToTypeID( "null" );
        var ref10 = new ActionReference();
        var idLyr = charIDToTypeID( "Lyr " );
        ref10.putName( idLyr, "Sharpen #1" );
    desc20.putReference( idnull, ref10 );
    var idselectionModifier = stringIDToTypeID( "selectionModifier" );
    var idselectionModifierType = stringIDToTypeID( "selectionModifierType" );
    var idaddToSelection = stringIDToTypeID( "addToSelection" );
    desc20.putEnumerated( idselectionModifier, idselectionModifierType, idaddToSelection );
    var idMkVs = charIDToTypeID( "MkVs" );
    desc20.putBoolean( idMkVs, false );
executeAction( idslct, desc20, DialogModes.NO );

// ===================================================
var idMk = charIDToTypeID( "Mk  " );
    var desc21 = new ActionDescriptor();
    var idnull = charIDToTypeID( "null" );
        var ref11 = new ActionReference();
        var idlayerSection = stringIDToTypeID( "layerSection" );
        ref11.putClass( idlayerSection );
    desc21.putReference( idnull, ref11 );
    var idFrom = charIDToTypeID( "From" );
        var ref12 = new ActionReference();
        var idLyr = charIDToTypeID( "Lyr " );
        var idOrdn = charIDToTypeID( "Ordn" );
        var idTrgt = charIDToTypeID( "Trgt" );
        ref12.putEnumerated( idLyr, idOrdn, idTrgt );
    desc21.putReference( idFrom, ref12 );
    var idUsng = charIDToTypeID( "Usng" );
        var desc22 = new ActionDescriptor();
        var idNm = charIDToTypeID( "Nm  " );
        desc22.putString( idNm, """TK Web-Sharpen""" );
    var idlayerSection = stringIDToTypeID( "layerSection" );
    desc21.putObject( idUsng, idlayerSection, desc22 );
executeAction( idMk, desc21, DialogModes.NO );

// ===================================================
var idsetd = charIDToTypeID( "setd" );
    var desc23 = new ActionDescriptor();
    var idnull = charIDToTypeID( "null" );
        var ref13 = new ActionReference();
        var idLyr = charIDToTypeID( "Lyr " );
        var idOrdn = charIDToTypeID( "Ordn" );
        var idTrgt = charIDToTypeID( "Trgt" );
        ref13.putEnumerated( idLyr, idOrdn, idTrgt );
    desc23.putReference( idnull, ref13 );
    var idT = charIDToTypeID( "T   " );
        var desc24 = new ActionDescriptor();
        var idOpct = charIDToTypeID( "Opct" );
        var idPrc = charIDToTypeID( "#Prc" );
        desc24.putUnitDouble( idOpct, idPrc, layerOpacity );
    var idLyr = charIDToTypeID( "Lyr " );
    desc23.putObject( idT, idLyr, desc24 );
executeAction( idsetd, desc23, DialogModes.NO );

// ===================================================
cTID = function(s) { return app.charIDToTypeID(s); };
function ActP() {
    var desc1 = new ActionDescriptor();
    var ref1 = new ActionReference();
    ref1.putEnumerated(cTID('Mn  '), cTID('MnIt'), cTID('ActP'));
    desc1.putReference(cTID('null'), ref1);
    executeAction(cTID('slct'), desc1, DialogModes.NO);
  };
  ActP();
      // End ScriptListener capture  
      }  //End tkaction(function)
                }
            else {
                //Evaluate if radio2 button is selected, which is for down-sizing using a horizontal dimension, and if it is, close window and run ScriptListener script to downsize and sharpen.
                if (radio2 == true) {
    // Start ScriptListener capture   
    // ===================================================
// ===================================================
var idDplc = charIDToTypeID( "Dplc" );
    var desc2 = new ActionDescriptor();
    var idnull = charIDToTypeID( "null" );
        var ref1 = new ActionReference();
        var idDcmn = charIDToTypeID( "Dcmn" );
        var idOrdn = charIDToTypeID( "Ordn" );
        var idFrst = charIDToTypeID( "Frst" );
        ref1.putEnumerated( idDcmn, idOrdn, idFrst );
    desc2.putReference( idnull, ref1 );
executeAction( idDplc, desc2, DialogModes.NO );

    app.activeDocument.suspendHistory("Resize / Sharpen", "tkaction()");

function tkaction() {

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

try {

// ===================================================
var idCnvM = charIDToTypeID( "CnvM" );
    var desc4 = new ActionDescriptor();
    var idT = charIDToTypeID( "T   " );
    var idRGBM = charIDToTypeID( "RGBM" );
    desc4.putClass( idT, idRGBM );
executeAction( idCnvM, desc4, DialogModes.NO );  }

catch (e) {}

// ===================================================
var idImgS = charIDToTypeID( "ImgS" );
    var desc3 = new ActionDescriptor();
    var idWdth = charIDToTypeID( "Wdth" );
    var idPxl = charIDToTypeID( "#Pxl" );
    desc3.putUnitDouble( idWdth, idPxl, firstNumber );
    var idRslt = charIDToTypeID( "Rslt" );
    var idRsl = charIDToTypeID( "#Rsl" );
    desc3.putUnitDouble( idRslt, idRsl, 72.000000 );
    var idscaleStyles = stringIDToTypeID( "scaleStyles" );
    desc3.putBoolean( idscaleStyles, true );
    var idCnsP = charIDToTypeID( "CnsP" );
    desc3.putBoolean( idCnsP, true );
    var idIntr = charIDToTypeID( "Intr" );
    var idIntp = charIDToTypeID( "Intp" );
    var idBcbc = charIDToTypeID( "Bcbc" );
    desc3.putEnumerated( idIntr, idIntp, idBcbc );
executeAction( idImgS, desc3, DialogModes.NO );

// ===================================================
var idDplc = charIDToTypeID( "Dplc" );
    var desc4 = new ActionDescriptor();
    var idnull = charIDToTypeID( "null" );
        var ref2 = new ActionReference();
        var idLyr = charIDToTypeID( "Lyr " );
        var idOrdn = charIDToTypeID( "Ordn" );
        var idTrgt = charIDToTypeID( "Trgt" );
        ref2.putEnumerated( idLyr, idOrdn, idTrgt );
    desc4.putReference( idnull, ref2 );
    var idNm = charIDToTypeID( "Nm  " );
    desc4.putString( idNm, """Sharpen #1""" );
    var idVrsn = charIDToTypeID( "Vrsn" );
    desc4.putInteger( idVrsn, 5 );
executeAction( idDplc, desc4, DialogModes.NO );

// ===================================================
var idShrp = charIDToTypeID( "Shrp" );
executeAction( idShrp, undefined, DialogModes.NO );

// ===================================================
var idDplc = charIDToTypeID( "Dplc" );
    var desc5 = new ActionDescriptor();
    var idnull = charIDToTypeID( "null" );
        var ref3 = new ActionReference();
        var idLyr = charIDToTypeID( "Lyr " );
        var idOrdn = charIDToTypeID( "Ordn" );
        var idTrgt = charIDToTypeID( "Trgt" );
        ref3.putEnumerated( idLyr, idOrdn, idTrgt );
    desc5.putReference( idnull, ref3 );
    var idNm = charIDToTypeID( "Nm  " );
    desc5.putString( idNm, """Sharpen #2""" );
    var idVrsn = charIDToTypeID( "Vrsn" );
    desc5.putInteger( idVrsn, 5 );
executeAction( idDplc, desc5, DialogModes.NO );

// ===================================================
var idImgS = charIDToTypeID( "ImgS" );
    var desc6 = new ActionDescriptor();
    var idWdth = charIDToTypeID( "Wdth" );
    var idPxl = charIDToTypeID( "#Pxl" );
    desc6.putUnitDouble( idWdth, idPxl, secondNumber );
    var idscaleStyles = stringIDToTypeID( "scaleStyles" );
    desc6.putBoolean( idscaleStyles, true );
    var idCnsP = charIDToTypeID( "CnsP" );
    desc6.putBoolean( idCnsP, true );
    var idIntr = charIDToTypeID( "Intr" );
    var idIntp = charIDToTypeID( "Intp" );
    var idBcbc = charIDToTypeID( "Bcbc" );
    desc6.putEnumerated( idIntr, idIntp, idBcbc );
executeAction( idImgS, desc6, DialogModes.NO );

// ===================================================
var idShrp = charIDToTypeID( "Shrp" );
executeAction( idShrp, undefined, DialogModes.NO );

// ===================================================
var idMk = charIDToTypeID( "Mk  " );
    var desc7 = new ActionDescriptor();
    var idNw = charIDToTypeID( "Nw  " );
    var idChnl = charIDToTypeID( "Chnl" );
    desc7.putClass( idNw, idChnl );
    var idAt = charIDToTypeID( "At  " );
        var ref4 = new ActionReference();
        var idChnl = charIDToTypeID( "Chnl" );
        var idChnl = charIDToTypeID( "Chnl" );
        var idMsk = charIDToTypeID( "Msk " );
        ref4.putEnumerated( idChnl, idChnl, idMsk );
    desc7.putReference( idAt, ref4 );
    var idUsng = charIDToTypeID( "Usng" );
    var idUsrM = charIDToTypeID( "UsrM" );
    var idRvlA = charIDToTypeID( "RvlA" );
    desc7.putEnumerated( idUsng, idUsrM, idRvlA );
executeAction( idMk, desc7, DialogModes.NO );

// ===================================================
var idAppI = charIDToTypeID( "AppI" );
    var desc8 = new ActionDescriptor();
    var idWith = charIDToTypeID( "With" );
        var desc9 = new ActionDescriptor();
        var idT = charIDToTypeID( "T   " );
            var ref5 = new ActionReference();
            var idChnl = charIDToTypeID( "Chnl" );
            var idChnl = charIDToTypeID( "Chnl" );
            var idRGB = charIDToTypeID( "RGB " );
            ref5.putEnumerated( idChnl, idChnl, idRGB );
            var idLyr = charIDToTypeID( "Lyr " );
            var idOrdn = charIDToTypeID( "Ordn" );
            var idMrgd = charIDToTypeID( "Mrgd" );
            ref5.putEnumerated( idLyr, idOrdn, idMrgd );
        desc9.putReference( idT, ref5 );
        var idInvr = charIDToTypeID( "Invr" );
        desc9.putBoolean( idInvr, true );
        var idPrsT = charIDToTypeID( "PrsT" );
        desc9.putBoolean( idPrsT, true );
    var idClcl = charIDToTypeID( "Clcl" );
    desc8.putObject( idWith, idClcl, desc9 );
executeAction( idAppI, desc8, DialogModes.NO );

// ===================================================
var idMk = charIDToTypeID( "Mk  " );
    var desc10 = new ActionDescriptor();
    var idnull = charIDToTypeID( "null" );
        var ref6 = new ActionReference();
        var idAdjL = charIDToTypeID( "AdjL" );
        ref6.putClass( idAdjL );
    desc10.putReference( idnull, ref6 );
    var idUsng = charIDToTypeID( "Usng" );
        var desc11 = new ActionDescriptor();
        var idType = charIDToTypeID( "Type" );
            var desc12 = new ActionDescriptor();
            var idpresetKind = stringIDToTypeID( "presetKind" );
            var idpresetKindType = stringIDToTypeID( "presetKindType" );
            var idpresetKindDefault = stringIDToTypeID( "presetKindDefault" );
            desc12.putEnumerated( idpresetKind, idpresetKindType, idpresetKindDefault );
        var idCrvs = charIDToTypeID( "Crvs" );
        desc11.putObject( idType, idCrvs, desc12 );
    var idAdjL = charIDToTypeID( "AdjL" );
    desc10.putObject( idUsng, idAdjL, desc11 );
executeAction( idMk, desc10, DialogModes.NO );

// ===================================================
var idMk = charIDToTypeID( "Mk  " );
    var desc13 = new ActionDescriptor();
    var idnull = charIDToTypeID( "null" );
        var ref7 = new ActionReference();
        var idAdjL = charIDToTypeID( "AdjL" );
        ref7.putClass( idAdjL );
    desc13.putReference( idnull, ref7 );
    var idUsng = charIDToTypeID( "Usng" );
        var desc14 = new ActionDescriptor();
        var idType = charIDToTypeID( "Type" );
            var desc15 = new ActionDescriptor();
            var idpresetKind = stringIDToTypeID( "presetKind" );
            var idpresetKindType = stringIDToTypeID( "presetKindType" );
            var idpresetKindDefault = stringIDToTypeID( "presetKindDefault" );
            desc15.putEnumerated( idpresetKind, idpresetKindType, idpresetKindDefault );
        var idLvls = charIDToTypeID( "Lvls" );
        desc14.putObject( idType, idLvls, desc15 );
    var idAdjL = charIDToTypeID( "AdjL" );
    desc13.putObject( idUsng, idAdjL, desc14 );
executeAction( idMk, desc13, DialogModes.NO );

// ===================================================
var idMk = charIDToTypeID( "Mk  " );
    var desc16 = new ActionDescriptor();
    var idnull = charIDToTypeID( "null" );
        var ref8 = new ActionReference();
        var idAdjL = charIDToTypeID( "AdjL" );
        ref8.putClass( idAdjL );
    desc16.putReference( idnull, ref8 );
    var idUsng = charIDToTypeID( "Usng" );
        var desc17 = new ActionDescriptor();
        var idType = charIDToTypeID( "Type" );
            var desc18 = new ActionDescriptor();
            var idpresetKind = stringIDToTypeID( "presetKind" );
            var idpresetKindType = stringIDToTypeID( "presetKindType" );
            var idpresetKindDefault = stringIDToTypeID( "presetKindDefault" );
            desc18.putEnumerated( idpresetKind, idpresetKindType, idpresetKindDefault );
            var idClrz = charIDToTypeID( "Clrz" );
            desc18.putBoolean( idClrz, false );
        var idHStr = charIDToTypeID( "HStr" );
        desc17.putObject( idType, idHStr, desc18 );
    var idAdjL = charIDToTypeID( "AdjL" );
    desc16.putObject( idUsng, idAdjL, desc17 );
executeAction( idMk, desc16, DialogModes.NO );

// ===================================================
var idslct = charIDToTypeID( "slct" );
    var desc19 = new ActionDescriptor();
    var idnull = charIDToTypeID( "null" );
        var ref9 = new ActionReference();
        var idLyr = charIDToTypeID( "Lyr " );
        ref9.putName( idLyr, "Sharpen #2" );
    desc19.putReference( idnull, ref9 );
    var idMkVs = charIDToTypeID( "MkVs" );
    desc19.putBoolean( idMkVs, false );
executeAction( idslct, desc19, DialogModes.NO );

// ===================================================
var idslct = charIDToTypeID( "slct" );
    var desc20 = new ActionDescriptor();
    var idnull = charIDToTypeID( "null" );
        var ref10 = new ActionReference();
        var idLyr = charIDToTypeID( "Lyr " );
        ref10.putName( idLyr, "Sharpen #1" );
    desc20.putReference( idnull, ref10 );
    var idselectionModifier = stringIDToTypeID( "selectionModifier" );
    var idselectionModifierType = stringIDToTypeID( "selectionModifierType" );
    var idaddToSelection = stringIDToTypeID( "addToSelection" );
    desc20.putEnumerated( idselectionModifier, idselectionModifierType, idaddToSelection );
    var idMkVs = charIDToTypeID( "MkVs" );
    desc20.putBoolean( idMkVs, false );
executeAction( idslct, desc20, DialogModes.NO );

// ===================================================
var idMk = charIDToTypeID( "Mk  " );
    var desc21 = new ActionDescriptor();
    var idnull = charIDToTypeID( "null" );
        var ref11 = new ActionReference();
        var idlayerSection = stringIDToTypeID( "layerSection" );
        ref11.putClass( idlayerSection );
    desc21.putReference( idnull, ref11 );
    var idFrom = charIDToTypeID( "From" );
        var ref12 = new ActionReference();
        var idLyr = charIDToTypeID( "Lyr " );
        var idOrdn = charIDToTypeID( "Ordn" );
        var idTrgt = charIDToTypeID( "Trgt" );
        ref12.putEnumerated( idLyr, idOrdn, idTrgt );
    desc21.putReference( idFrom, ref12 );
    var idUsng = charIDToTypeID( "Usng" );
        var desc22 = new ActionDescriptor();
        var idNm = charIDToTypeID( "Nm  " );
        desc22.putString( idNm, """TK Web-Sharpen""" );
    var idlayerSection = stringIDToTypeID( "layerSection" );
    desc21.putObject( idUsng, idlayerSection, desc22 );
executeAction( idMk, desc21, DialogModes.NO );

// ===================================================
var idsetd = charIDToTypeID( "setd" );
    var desc23 = new ActionDescriptor();
    var idnull = charIDToTypeID( "null" );
        var ref13 = new ActionReference();
        var idLyr = charIDToTypeID( "Lyr " );
        var idOrdn = charIDToTypeID( "Ordn" );
        var idTrgt = charIDToTypeID( "Trgt" );
        ref13.putEnumerated( idLyr, idOrdn, idTrgt );
    desc23.putReference( idnull, ref13 );
    var idT = charIDToTypeID( "T   " );
        var desc24 = new ActionDescriptor();
        var idOpct = charIDToTypeID( "Opct" );
        var idPrc = charIDToTypeID( "#Prc" );
        desc24.putUnitDouble( idOpct, idPrc, layerOpacity );
    var idLyr = charIDToTypeID( "Lyr " );
    desc23.putObject( idT, idLyr, desc24 );
executeAction( idsetd, desc23, DialogModes.NO );

// ===================================================
cTID = function(s) { return app.charIDToTypeID(s); };
function ActP() {
    var desc1 = new ActionDescriptor();
    var ref1 = new ActionReference();
    ref1.putEnumerated(cTID('Mn  '), cTID('MnIt'), cTID('ActP'));
    desc1.putReference(cTID('null'), ref1);
    executeAction(cTID('slct'), desc1, DialogModes.NO);
  };
  ActP();
      // End ScriptListener capture
      }  //End tkaction() function
                    }
                //Defualt to FIT
                else {
                //First determine which is greather, WIDTH or HEIGHT
                if (app.activeDocument.width.value >=app.activeDocument.height.value)
                //Run the horizontal websharpening script if WIDTH is >= HEIGHT
                {
//Horizontal script starts here for web-sharpening
// ===================================================
var idDplc = charIDToTypeID( "Dplc" );
    var desc2 = new ActionDescriptor();
    var idnull = charIDToTypeID( "null" );
        var ref1 = new ActionReference();
        var idDcmn = charIDToTypeID( "Dcmn" );
        var idOrdn = charIDToTypeID( "Ordn" );
        var idFrst = charIDToTypeID( "Frst" );
        ref1.putEnumerated( idDcmn, idOrdn, idFrst );
    desc2.putReference( idnull, ref1 );
executeAction( idDplc, desc2, DialogModes.NO );

    app.activeDocument.suspendHistory("Resize / Sharpen", "tkaction()");

function tkaction() {
    
// ===================================================
var idFltI = charIDToTypeID( "FltI" );
executeAction( idFltI, undefined, DialogModes.NO );

try {

// ===================================================
var idCnvM = charIDToTypeID( "CnvM" );
    var desc4 = new ActionDescriptor();
    var idT = charIDToTypeID( "T   " );
    var idRGBM = charIDToTypeID( "RGBM" );
    desc4.putClass( idT, idRGBM );
executeAction( idCnvM, desc4, DialogModes.NO );  }

catch (e) {}

// ===================================================
var idImgS = charIDToTypeID( "ImgS" );
    var desc3 = new ActionDescriptor();
    var idWdth = charIDToTypeID( "Wdth" );
    var idPxl = charIDToTypeID( "#Pxl" );
    desc3.putUnitDouble( idWdth, idPxl, firstNumber );
    var idRslt = charIDToTypeID( "Rslt" );
    var idRsl = charIDToTypeID( "#Rsl" );
    desc3.putUnitDouble( idRslt, idRsl, 72.000000 );
    var idscaleStyles = stringIDToTypeID( "scaleStyles" );
    desc3.putBoolean( idscaleStyles, true );
    var idCnsP = charIDToTypeID( "CnsP" );
    desc3.putBoolean( idCnsP, true );
    var idIntr = charIDToTypeID( "Intr" );
    var idIntp = charIDToTypeID( "Intp" );
    var idBcbc = charIDToTypeID( "Bcbc" );
    desc3.putEnumerated( idIntr, idIntp, idBcbc );
executeAction( idImgS, desc3, DialogModes.NO );

// ===================================================
var idDplc = charIDToTypeID( "Dplc" );
    var desc4 = new ActionDescriptor();
    var idnull = charIDToTypeID( "null" );
        var ref2 = new ActionReference();
        var idLyr = charIDToTypeID( "Lyr " );
        var idOrdn = charIDToTypeID( "Ordn" );
        var idTrgt = charIDToTypeID( "Trgt" );
        ref2.putEnumerated( idLyr, idOrdn, idTrgt );
    desc4.putReference( idnull, ref2 );
    var idNm = charIDToTypeID( "Nm  " );
    desc4.putString( idNm, """Sharpen #1""" );
    var idVrsn = charIDToTypeID( "Vrsn" );
    desc4.putInteger( idVrsn, 5 );
executeAction( idDplc, desc4, DialogModes.NO );

// ===================================================
var idShrp = charIDToTypeID( "Shrp" );
executeAction( idShrp, undefined, DialogModes.NO );

// ===================================================
var idDplc = charIDToTypeID( "Dplc" );
    var desc5 = new ActionDescriptor();
    var idnull = charIDToTypeID( "null" );
        var ref3 = new ActionReference();
        var idLyr = charIDToTypeID( "Lyr " );
        var idOrdn = charIDToTypeID( "Ordn" );
        var idTrgt = charIDToTypeID( "Trgt" );
        ref3.putEnumerated( idLyr, idOrdn, idTrgt );
    desc5.putReference( idnull, ref3 );
    var idNm = charIDToTypeID( "Nm  " );
    desc5.putString( idNm, """Sharpen #2""" );
    var idVrsn = charIDToTypeID( "Vrsn" );
    desc5.putInteger( idVrsn, 5 );
executeAction( idDplc, desc5, DialogModes.NO );

// ===================================================
var idImgS = charIDToTypeID( "ImgS" );
    var desc6 = new ActionDescriptor();
    var idWdth = charIDToTypeID( "Wdth" );
    var idPxl = charIDToTypeID( "#Pxl" );
    desc6.putUnitDouble( idWdth, idPxl, secondNumber );
    var idscaleStyles = stringIDToTypeID( "scaleStyles" );
    desc6.putBoolean( idscaleStyles, true );
    var idCnsP = charIDToTypeID( "CnsP" );
    desc6.putBoolean( idCnsP, true );
    var idIntr = charIDToTypeID( "Intr" );
    var idIntp = charIDToTypeID( "Intp" );
    var idBcbc = charIDToTypeID( "Bcbc" );
    desc6.putEnumerated( idIntr, idIntp, idBcbc );
executeAction( idImgS, desc6, DialogModes.NO );

// ===================================================
var idShrp = charIDToTypeID( "Shrp" );
executeAction( idShrp, undefined, DialogModes.NO );

// ===================================================
var idMk = charIDToTypeID( "Mk  " );
    var desc7 = new ActionDescriptor();
    var idNw = charIDToTypeID( "Nw  " );
    var idChnl = charIDToTypeID( "Chnl" );
    desc7.putClass( idNw, idChnl );
    var idAt = charIDToTypeID( "At  " );
        var ref4 = new ActionReference();
        var idChnl = charIDToTypeID( "Chnl" );
        var idChnl = charIDToTypeID( "Chnl" );
        var idMsk = charIDToTypeID( "Msk " );
        ref4.putEnumerated( idChnl, idChnl, idMsk );
    desc7.putReference( idAt, ref4 );
    var idUsng = charIDToTypeID( "Usng" );
    var idUsrM = charIDToTypeID( "UsrM" );
    var idRvlA = charIDToTypeID( "RvlA" );
    desc7.putEnumerated( idUsng, idUsrM, idRvlA );
executeAction( idMk, desc7, DialogModes.NO );

// ===================================================
var idAppI = charIDToTypeID( "AppI" );
    var desc8 = new ActionDescriptor();
    var idWith = charIDToTypeID( "With" );
        var desc9 = new ActionDescriptor();
        var idT = charIDToTypeID( "T   " );
            var ref5 = new ActionReference();
            var idChnl = charIDToTypeID( "Chnl" );
            var idChnl = charIDToTypeID( "Chnl" );
            var idRGB = charIDToTypeID( "RGB " );
            ref5.putEnumerated( idChnl, idChnl, idRGB );
            var idLyr = charIDToTypeID( "Lyr " );
            var idOrdn = charIDToTypeID( "Ordn" );
            var idMrgd = charIDToTypeID( "Mrgd" );
            ref5.putEnumerated( idLyr, idOrdn, idMrgd );
        desc9.putReference( idT, ref5 );
        var idInvr = charIDToTypeID( "Invr" );
        desc9.putBoolean( idInvr, true );
        var idPrsT = charIDToTypeID( "PrsT" );
        desc9.putBoolean( idPrsT, true );
    var idClcl = charIDToTypeID( "Clcl" );
    desc8.putObject( idWith, idClcl, desc9 );
executeAction( idAppI, desc8, DialogModes.NO );

// ===================================================
var idMk = charIDToTypeID( "Mk  " );
    var desc10 = new ActionDescriptor();
    var idnull = charIDToTypeID( "null" );
        var ref6 = new ActionReference();
        var idAdjL = charIDToTypeID( "AdjL" );
        ref6.putClass( idAdjL );
    desc10.putReference( idnull, ref6 );
    var idUsng = charIDToTypeID( "Usng" );
        var desc11 = new ActionDescriptor();
        var idType = charIDToTypeID( "Type" );
            var desc12 = new ActionDescriptor();
            var idpresetKind = stringIDToTypeID( "presetKind" );
            var idpresetKindType = stringIDToTypeID( "presetKindType" );
            var idpresetKindDefault = stringIDToTypeID( "presetKindDefault" );
            desc12.putEnumerated( idpresetKind, idpresetKindType, idpresetKindDefault );
        var idCrvs = charIDToTypeID( "Crvs" );
        desc11.putObject( idType, idCrvs, desc12 );
    var idAdjL = charIDToTypeID( "AdjL" );
    desc10.putObject( idUsng, idAdjL, desc11 );
executeAction( idMk, desc10, DialogModes.NO );

// ===================================================
var idMk = charIDToTypeID( "Mk  " );
    var desc13 = new ActionDescriptor();
    var idnull = charIDToTypeID( "null" );
        var ref7 = new ActionReference();
        var idAdjL = charIDToTypeID( "AdjL" );
        ref7.putClass( idAdjL );
    desc13.putReference( idnull, ref7 );
    var idUsng = charIDToTypeID( "Usng" );
        var desc14 = new ActionDescriptor();
        var idType = charIDToTypeID( "Type" );
            var desc15 = new ActionDescriptor();
            var idpresetKind = stringIDToTypeID( "presetKind" );
            var idpresetKindType = stringIDToTypeID( "presetKindType" );
            var idpresetKindDefault = stringIDToTypeID( "presetKindDefault" );
            desc15.putEnumerated( idpresetKind, idpresetKindType, idpresetKindDefault );
        var idLvls = charIDToTypeID( "Lvls" );
        desc14.putObject( idType, idLvls, desc15 );
    var idAdjL = charIDToTypeID( "AdjL" );
    desc13.putObject( idUsng, idAdjL, desc14 );
executeAction( idMk, desc13, DialogModes.NO );

// ===================================================
var idMk = charIDToTypeID( "Mk  " );
    var desc16 = new ActionDescriptor();
    var idnull = charIDToTypeID( "null" );
        var ref8 = new ActionReference();
        var idAdjL = charIDToTypeID( "AdjL" );
        ref8.putClass( idAdjL );
    desc16.putReference( idnull, ref8 );
    var idUsng = charIDToTypeID( "Usng" );
        var desc17 = new ActionDescriptor();
        var idType = charIDToTypeID( "Type" );
            var desc18 = new ActionDescriptor();
            var idpresetKind = stringIDToTypeID( "presetKind" );
            var idpresetKindType = stringIDToTypeID( "presetKindType" );
            var idpresetKindDefault = stringIDToTypeID( "presetKindDefault" );
            desc18.putEnumerated( idpresetKind, idpresetKindType, idpresetKindDefault );
            var idClrz = charIDToTypeID( "Clrz" );
            desc18.putBoolean( idClrz, false );
        var idHStr = charIDToTypeID( "HStr" );
        desc17.putObject( idType, idHStr, desc18 );
    var idAdjL = charIDToTypeID( "AdjL" );
    desc16.putObject( idUsng, idAdjL, desc17 );
executeAction( idMk, desc16, DialogModes.NO );

// ===================================================
var idslct = charIDToTypeID( "slct" );
    var desc19 = new ActionDescriptor();
    var idnull = charIDToTypeID( "null" );
        var ref9 = new ActionReference();
        var idLyr = charIDToTypeID( "Lyr " );
        ref9.putName( idLyr, "Sharpen #2" );
    desc19.putReference( idnull, ref9 );
    var idMkVs = charIDToTypeID( "MkVs" );
    desc19.putBoolean( idMkVs, false );
executeAction( idslct, desc19, DialogModes.NO );

// ===================================================
var idslct = charIDToTypeID( "slct" );
    var desc20 = new ActionDescriptor();
    var idnull = charIDToTypeID( "null" );
        var ref10 = new ActionReference();
        var idLyr = charIDToTypeID( "Lyr " );
        ref10.putName( idLyr, "Sharpen #1" );
    desc20.putReference( idnull, ref10 );
    var idselectionModifier = stringIDToTypeID( "selectionModifier" );
    var idselectionModifierType = stringIDToTypeID( "selectionModifierType" );
    var idaddToSelection = stringIDToTypeID( "addToSelection" );
    desc20.putEnumerated( idselectionModifier, idselectionModifierType, idaddToSelection );
    var idMkVs = charIDToTypeID( "MkVs" );
    desc20.putBoolean( idMkVs, false );
executeAction( idslct, desc20, DialogModes.NO );

// ===================================================
var idMk = charIDToTypeID( "Mk  " );
    var desc21 = new ActionDescriptor();
    var idnull = charIDToTypeID( "null" );
        var ref11 = new ActionReference();
        var idlayerSection = stringIDToTypeID( "layerSection" );
        ref11.putClass( idlayerSection );
    desc21.putReference( idnull, ref11 );
    var idFrom = charIDToTypeID( "From" );
        var ref12 = new ActionReference();
        var idLyr = charIDToTypeID( "Lyr " );
        var idOrdn = charIDToTypeID( "Ordn" );
        var idTrgt = charIDToTypeID( "Trgt" );
        ref12.putEnumerated( idLyr, idOrdn, idTrgt );
    desc21.putReference( idFrom, ref12 );
    var idUsng = charIDToTypeID( "Usng" );
        var desc22 = new ActionDescriptor();
        var idNm = charIDToTypeID( "Nm  " );
        desc22.putString( idNm, """TK Web-Sharpen""" );
    var idlayerSection = stringIDToTypeID( "layerSection" );
    desc21.putObject( idUsng, idlayerSection, desc22 );
executeAction( idMk, desc21, DialogModes.NO );

// ===================================================
var idsetd = charIDToTypeID( "setd" );
    var desc23 = new ActionDescriptor();
    var idnull = charIDToTypeID( "null" );
        var ref13 = new ActionReference();
        var idLyr = charIDToTypeID( "Lyr " );
        var idOrdn = charIDToTypeID( "Ordn" );
        var idTrgt = charIDToTypeID( "Trgt" );
        ref13.putEnumerated( idLyr, idOrdn, idTrgt );
    desc23.putReference( idnull, ref13 );
    var idT = charIDToTypeID( "T   " );
        var desc24 = new ActionDescriptor();
        var idOpct = charIDToTypeID( "Opct" );
        var idPrc = charIDToTypeID( "#Prc" );
        desc24.putUnitDouble( idOpct, idPrc, layerOpacity );
    var idLyr = charIDToTypeID( "Lyr " );
    desc23.putObject( idT, idLyr, desc24 );
executeAction( idsetd, desc23, DialogModes.NO );

// ===================================================
cTID = function(s) { return app.charIDToTypeID(s); };
function ActP() {
    var desc1 = new ActionDescriptor();
    var ref1 = new ActionReference();
    ref1.putEnumerated(cTID('Mn  '), cTID('MnIt'), cTID('ActP'));
    desc1.putReference(cTID('null'), ref1);
    executeAction(cTID('slct'), desc1, DialogModes.NO);
  };
  ActP();
  //Horizontal script stops here
  }  //end tkaction() function
                }
                else 
                //Do the Vertical web-sharpening script since HEIGHT must be greater than WIDTH
                {
//Script for vertical FIT starts here                   
// ===================================================
var idDplc = charIDToTypeID( "Dplc" );
    var desc2 = new ActionDescriptor();
    var idnull = charIDToTypeID( "null" );
        var ref1 = new ActionReference();
        var idDcmn = charIDToTypeID( "Dcmn" );
        var idOrdn = charIDToTypeID( "Ordn" );
        var idFrst = charIDToTypeID( "Frst" );
        ref1.putEnumerated( idDcmn, idOrdn, idFrst );
    desc2.putReference( idnull, ref1 );
executeAction( idDplc, desc2, DialogModes.NO );

    app.activeDocument.suspendHistory("Resize / Sharpen", "tkaction()");

function tkaction() {

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

try {

// ===================================================
var idCnvM = charIDToTypeID( "CnvM" );
    var desc4 = new ActionDescriptor();
    var idT = charIDToTypeID( "T   " );
    var idRGBM = charIDToTypeID( "RGBM" );
    desc4.putClass( idT, idRGBM );
executeAction( idCnvM, desc4, DialogModes.NO );  }

catch (e) {}

// ===================================================
var idImgS = charIDToTypeID( "ImgS" );
    var desc3 = new ActionDescriptor();
    var idHght = charIDToTypeID( "Hght" );
    var idPxl = charIDToTypeID( "#Pxl" );
    desc3.putUnitDouble( idHght, idPxl, firstNumber );
    var idRslt = charIDToTypeID( "Rslt" );
    var idRsl = charIDToTypeID( "#Rsl" );
    desc3.putUnitDouble( idRslt, idRsl, 72.000000 );
    var idscaleStyles = stringIDToTypeID( "scaleStyles" );
    desc3.putBoolean( idscaleStyles, true );
    var idCnsP = charIDToTypeID( "CnsP" );
    desc3.putBoolean( idCnsP, true );
    var idIntr = charIDToTypeID( "Intr" );
    var idIntp = charIDToTypeID( "Intp" );
    var idBcbc = charIDToTypeID( "Bcbc" );
    desc3.putEnumerated( idIntr, idIntp, idBcbc );
executeAction( idImgS, desc3, DialogModes.NO );

// ===================================================
var idDplc = charIDToTypeID( "Dplc" );
    var desc4 = new ActionDescriptor();
    var idnull = charIDToTypeID( "null" );
        var ref2 = new ActionReference();
        var idLyr = charIDToTypeID( "Lyr " );
        var idOrdn = charIDToTypeID( "Ordn" );
        var idTrgt = charIDToTypeID( "Trgt" );
        ref2.putEnumerated( idLyr, idOrdn, idTrgt );
    desc4.putReference( idnull, ref2 );
    var idNm = charIDToTypeID( "Nm  " );
    desc4.putString( idNm, """Sharpen #1""" );
    var idVrsn = charIDToTypeID( "Vrsn" );
    desc4.putInteger( idVrsn, 5 );
executeAction( idDplc, desc4, DialogModes.NO );

// ===================================================
var idShrp = charIDToTypeID( "Shrp" );
executeAction( idShrp, undefined, DialogModes.NO );

// ===================================================
var idDplc = charIDToTypeID( "Dplc" );
    var desc5 = new ActionDescriptor();
    var idnull = charIDToTypeID( "null" );
        var ref3 = new ActionReference();
        var idLyr = charIDToTypeID( "Lyr " );
        var idOrdn = charIDToTypeID( "Ordn" );
        var idTrgt = charIDToTypeID( "Trgt" );
        ref3.putEnumerated( idLyr, idOrdn, idTrgt );
    desc5.putReference( idnull, ref3 );
    var idNm = charIDToTypeID( "Nm  " );
    desc5.putString( idNm, """Sharpen #2""" );
    var idVrsn = charIDToTypeID( "Vrsn" );
    desc5.putInteger( idVrsn, 5 );
executeAction( idDplc, desc5, DialogModes.NO );

// ===================================================
var idImgS = charIDToTypeID( "ImgS" );
    var desc6 = new ActionDescriptor();
    var idHght = charIDToTypeID( "Hght" );
    var idPxl = charIDToTypeID( "#Pxl" );
    desc6.putUnitDouble( idHght, idPxl, secondNumber );
    var idscaleStyles = stringIDToTypeID( "scaleStyles" );
    desc6.putBoolean( idscaleStyles, true );
    var idCnsP = charIDToTypeID( "CnsP" );
    desc6.putBoolean( idCnsP, true );
    var idIntr = charIDToTypeID( "Intr" );
    var idIntp = charIDToTypeID( "Intp" );
    var idBcbc = charIDToTypeID( "Bcbc" );
    desc6.putEnumerated( idIntr, idIntp, idBcbc );
executeAction( idImgS, desc6, DialogModes.NO );

// ===================================================
var idShrp = charIDToTypeID( "Shrp" );
executeAction( idShrp, undefined, DialogModes.NO );

// ===================================================
var idMk = charIDToTypeID( "Mk  " );
    var desc7 = new ActionDescriptor();
    var idNw = charIDToTypeID( "Nw  " );
    var idChnl = charIDToTypeID( "Chnl" );
    desc7.putClass( idNw, idChnl );
    var idAt = charIDToTypeID( "At  " );
        var ref4 = new ActionReference();
        var idChnl = charIDToTypeID( "Chnl" );
        var idChnl = charIDToTypeID( "Chnl" );
        var idMsk = charIDToTypeID( "Msk " );
        ref4.putEnumerated( idChnl, idChnl, idMsk );
    desc7.putReference( idAt, ref4 );
    var idUsng = charIDToTypeID( "Usng" );
    var idUsrM = charIDToTypeID( "UsrM" );
    var idRvlA = charIDToTypeID( "RvlA" );
    desc7.putEnumerated( idUsng, idUsrM, idRvlA );
executeAction( idMk, desc7, DialogModes.NO );

// ===================================================
var idAppI = charIDToTypeID( "AppI" );
    var desc8 = new ActionDescriptor();
    var idWith = charIDToTypeID( "With" );
        var desc9 = new ActionDescriptor();
        var idT = charIDToTypeID( "T   " );
            var ref5 = new ActionReference();
            var idChnl = charIDToTypeID( "Chnl" );
            var idChnl = charIDToTypeID( "Chnl" );
            var idRGB = charIDToTypeID( "RGB " );
            ref5.putEnumerated( idChnl, idChnl, idRGB );
            var idLyr = charIDToTypeID( "Lyr " );
            var idOrdn = charIDToTypeID( "Ordn" );
            var idMrgd = charIDToTypeID( "Mrgd" );
            ref5.putEnumerated( idLyr, idOrdn, idMrgd );
        desc9.putReference( idT, ref5 );
        var idInvr = charIDToTypeID( "Invr" );
        desc9.putBoolean( idInvr, true );
        var idPrsT = charIDToTypeID( "PrsT" );
        desc9.putBoolean( idPrsT, true );
    var idClcl = charIDToTypeID( "Clcl" );
    desc8.putObject( idWith, idClcl, desc9 );
executeAction( idAppI, desc8, DialogModes.NO );

// ===================================================
var idMk = charIDToTypeID( "Mk  " );
    var desc10 = new ActionDescriptor();
    var idnull = charIDToTypeID( "null" );
        var ref6 = new ActionReference();
        var idAdjL = charIDToTypeID( "AdjL" );
        ref6.putClass( idAdjL );
    desc10.putReference( idnull, ref6 );
    var idUsng = charIDToTypeID( "Usng" );
        var desc11 = new ActionDescriptor();
        var idType = charIDToTypeID( "Type" );
            var desc12 = new ActionDescriptor();
            var idpresetKind = stringIDToTypeID( "presetKind" );
            var idpresetKindType = stringIDToTypeID( "presetKindType" );
            var idpresetKindDefault = stringIDToTypeID( "presetKindDefault" );
            desc12.putEnumerated( idpresetKind, idpresetKindType, idpresetKindDefault );
        var idCrvs = charIDToTypeID( "Crvs" );
        desc11.putObject( idType, idCrvs, desc12 );
    var idAdjL = charIDToTypeID( "AdjL" );
    desc10.putObject( idUsng, idAdjL, desc11 );
executeAction( idMk, desc10, DialogModes.NO );

// ===================================================
var idMk = charIDToTypeID( "Mk  " );
    var desc13 = new ActionDescriptor();
    var idnull = charIDToTypeID( "null" );
        var ref7 = new ActionReference();
        var idAdjL = charIDToTypeID( "AdjL" );
        ref7.putClass( idAdjL );
    desc13.putReference( idnull, ref7 );
    var idUsng = charIDToTypeID( "Usng" );
        var desc14 = new ActionDescriptor();
        var idType = charIDToTypeID( "Type" );
            var desc15 = new ActionDescriptor();
            var idpresetKind = stringIDToTypeID( "presetKind" );
            var idpresetKindType = stringIDToTypeID( "presetKindType" );
            var idpresetKindDefault = stringIDToTypeID( "presetKindDefault" );
            desc15.putEnumerated( idpresetKind, idpresetKindType, idpresetKindDefault );
        var idLvls = charIDToTypeID( "Lvls" );
        desc14.putObject( idType, idLvls, desc15 );
    var idAdjL = charIDToTypeID( "AdjL" );
    desc13.putObject( idUsng, idAdjL, desc14 );
executeAction( idMk, desc13, DialogModes.NO );

// ===================================================
var idMk = charIDToTypeID( "Mk  " );
    var desc16 = new ActionDescriptor();
    var idnull = charIDToTypeID( "null" );
        var ref8 = new ActionReference();
        var idAdjL = charIDToTypeID( "AdjL" );
        ref8.putClass( idAdjL );
    desc16.putReference( idnull, ref8 );
    var idUsng = charIDToTypeID( "Usng" );
        var desc17 = new ActionDescriptor();
        var idType = charIDToTypeID( "Type" );
            var desc18 = new ActionDescriptor();
            var idpresetKind = stringIDToTypeID( "presetKind" );
            var idpresetKindType = stringIDToTypeID( "presetKindType" );
            var idpresetKindDefault = stringIDToTypeID( "presetKindDefault" );
            desc18.putEnumerated( idpresetKind, idpresetKindType, idpresetKindDefault );
            var idClrz = charIDToTypeID( "Clrz" );
            desc18.putBoolean( idClrz, false );
        var idHStr = charIDToTypeID( "HStr" );
        desc17.putObject( idType, idHStr, desc18 );
    var idAdjL = charIDToTypeID( "AdjL" );
    desc16.putObject( idUsng, idAdjL, desc17 );
executeAction( idMk, desc16, DialogModes.NO );

// ===================================================
var idslct = charIDToTypeID( "slct" );
    var desc19 = new ActionDescriptor();
    var idnull = charIDToTypeID( "null" );
        var ref9 = new ActionReference();
        var idLyr = charIDToTypeID( "Lyr " );
        ref9.putName( idLyr, "Sharpen #2" );
    desc19.putReference( idnull, ref9 );
    var idMkVs = charIDToTypeID( "MkVs" );
    desc19.putBoolean( idMkVs, false );
executeAction( idslct, desc19, DialogModes.NO );

// ===================================================
var idslct = charIDToTypeID( "slct" );
    var desc20 = new ActionDescriptor();
    var idnull = charIDToTypeID( "null" );
        var ref10 = new ActionReference();
        var idLyr = charIDToTypeID( "Lyr " );
        ref10.putName( idLyr, "Sharpen #1" );
    desc20.putReference( idnull, ref10 );
    var idselectionModifier = stringIDToTypeID( "selectionModifier" );
    var idselectionModifierType = stringIDToTypeID( "selectionModifierType" );
    var idaddToSelection = stringIDToTypeID( "addToSelection" );
    desc20.putEnumerated( idselectionModifier, idselectionModifierType, idaddToSelection );
    var idMkVs = charIDToTypeID( "MkVs" );
    desc20.putBoolean( idMkVs, false );
executeAction( idslct, desc20, DialogModes.NO );

// ===================================================
var idMk = charIDToTypeID( "Mk  " );
    var desc21 = new ActionDescriptor();
    var idnull = charIDToTypeID( "null" );
        var ref11 = new ActionReference();
        var idlayerSection = stringIDToTypeID( "layerSection" );
        ref11.putClass( idlayerSection );
    desc21.putReference( idnull, ref11 );
    var idFrom = charIDToTypeID( "From" );
        var ref12 = new ActionReference();
        var idLyr = charIDToTypeID( "Lyr " );
        var idOrdn = charIDToTypeID( "Ordn" );
        var idTrgt = charIDToTypeID( "Trgt" );
        ref12.putEnumerated( idLyr, idOrdn, idTrgt );
    desc21.putReference( idFrom, ref12 );
    var idUsng = charIDToTypeID( "Usng" );
        var desc22 = new ActionDescriptor();
        var idNm = charIDToTypeID( "Nm  " );
        desc22.putString( idNm, """TK Web-Sharpen""" );
    var idlayerSection = stringIDToTypeID( "layerSection" );
    desc21.putObject( idUsng, idlayerSection, desc22 );
executeAction( idMk, desc21, DialogModes.NO );

// ===================================================
var idsetd = charIDToTypeID( "setd" );
    var desc23 = new ActionDescriptor();
    var idnull = charIDToTypeID( "null" );
        var ref13 = new ActionReference();
        var idLyr = charIDToTypeID( "Lyr " );
        var idOrdn = charIDToTypeID( "Ordn" );
        var idTrgt = charIDToTypeID( "Trgt" );
        ref13.putEnumerated( idLyr, idOrdn, idTrgt );
    desc23.putReference( idnull, ref13 );
    var idT = charIDToTypeID( "T   " );
        var desc24 = new ActionDescriptor();
        var idOpct = charIDToTypeID( "Opct" );
        var idPrc = charIDToTypeID( "#Prc" );
        desc24.putUnitDouble( idOpct, idPrc, layerOpacity );
    var idLyr = charIDToTypeID( "Lyr " );
    desc23.putObject( idT, idLyr, desc24 );
executeAction( idsetd, desc23, DialogModes.NO );

// ===================================================
cTID = function(s) { return app.charIDToTypeID(s); };
function ActP() {
    var desc1 = new ActionDescriptor();
    var ref1 = new ActionReference();
    ref1.putEnumerated(cTID('Mn  '), cTID('MnIt'), cTID('ActP'));
    desc1.putReference(cTID('null'), ref1);
    executeAction(cTID('slct'), desc1, DialogModes.NO);
  };
  ActP();
                };
 //End out of "Fit" sharpening   
                    }
                }   //End tkaction() function
                }
            }
        //Send alert to enter a proper value for dimensions if entry is not between 50 and 9999 inclusive.
        else {
            if (pixelDimension <50 || pixelDimension > 9999 || isNaN(pixelDimension) == true)  {
                alert ("Please enter a number from 50 to 9999 for FINAL DIMENSION");}
            else {
                alert ("Please enter a number from 1 to 100 for STRENGTH");}
            }
}
