0 Replies Latest reply on Feb 16, 2014 10:23 AM by ojodegato

    High Pass Dialog Textbook Exercise

    ojodegato Level 1

      I purchased the Power, Speed and Automatitation textbook to learn Photoshop Scripting. I am working through the exercises in the last chapeter Building Custom Dialogs and have stumbled on the last exercise building a High Pass Dialog. When I test the script ai get a time run error which I am not able to track down. Can someone please look at the script and let me know where the error is coming from.

      Many thanks,

       

      //highPassDialog.jsx

      //021514

       

      /*

      Description:

      This dialog gathers data for creating a High Pass layer,

      which can shapern or blur a docuemnt.

       

      This dialog shows the use of many differnt types of UI controls

       

       

      Algorithm:

      Stage #1 build the dialog

          Create helloWorldDialog() function

              Place the dialog where it will be displayed

              Add UI elemtnst to function

              Add a group box panel for formating the buttons in a row

              Add On and Cancel buttons

             

          Create initHelloWorldDialog() function

              Initalize buttons OK and Cancel

              Add Events to OK and Cancel buttons

       

       

      Hook up the dialog to respond to events (add to SET UP)

       

       

      Stage #2 display the dialog

          Create runHelloWorldDialog() function

       

       

      Stage #3 process the results

          processHelloWorldResults() function

          show alert()

       

       

       

      Script Installation:

      1.Mac place script in Applications/Adobe Photoshop CC/Presets/Scripts

      2.Windows place script in C:\Program Files\Adobe\Adobe Photoshop CC#\Presets\Scripts\

      3.Restart Photoshop

      4.Choose File > Scripts > template

      */

       

       

      ///////////////////////////////////////////////////////////////

      // SET UP

      ///////////////////////////////////////////////////////////////

       

       

      //Enable double clicking from hte Macintosh Finder or Windows  Explorer

      #target photoshop

       

       

      //Make Photoshop the formost Appplication

      app.bringToFront();

       

       

      //** DIALOG STAGE #1 - build the dilaog **

       

       

      //Create the dialog object and its UI objects

      var theDialog = createHighPassDialog();

       

       

      //Hook up the dialog to respond to events

      initHighPassDialog (theDialog);

       

       

       

       

      ///////////////////////////////////////////////////////////////

      // MAIN

      ///////////////////////////////////////////////////////////////

       

       

      //** DIALOG STAGE #2 - display the dialog **

      if (runHighPassDialog(theDialog) == 1) {

         

          //User pressed OK

         

          //** DIALOG STAGE #3 - process the dialog results **

          processHighPassResults(theDialog);

         

          }

       

       

      ///////////////////////////////////////////////////////////////

      // FUNCTIONS

      ///////////////////////////////////////////////////////////////

       

       

      //Create a dialog for creating the High Pass Layer

       

       

      function createHighPassDialog() {

             

              //Create and empty dialog window

              var myDialog = new Window ('dialog',"Create a High Pass Layer");

             

              //Place the dialog 100px from the left and 100px from the top

              myDialog.frameLocation = [100, 100];

              myDialog.orientation = "row"; 

              myDialog.alignChildren = "top";   

             

              //Add a group box for the new layer options

              myDialog.optionsPanel = myDialog.add('panel', undefined, "Options");

              myDialog.optionsPanel.orientation = "column";

              myDialog.optionsPanel.alignChildren = "left";

             

              //Add radio buttons groups fro sharpen vs blur choice

              myDialog.optionsPanel.choiceRadioButtons = myDialog.optionsPanel.add('group');

              myDialog.optionsPanel.choiceRadioButtons.orientation = "row";

              myDialog.optionsPanel.choiceRadioButtons.alignChildren = "top";

             

              //Add text for user instructiuons

              myDialog.optionsPanel.choiceRadioButtons.staticText =

              myDialog.optionsPanel.choiceRadioButtons.add('statictext', undefined, "Select operation to perform");

             

              //Crate gropup to align radio buttons

              myDialog.optionsPanel.choiceRadioButtons.radioGroup =

              myDialog.optionsPanel.choiceRadioButtons.add('group');

              myDialog.optionsPanel.choiceRadioButtons.radioGroup.orientation = "column";

              myDialog.optionsPanel.choiceRadioButtons.radioGroup.alignChildren = "left";

             

              //Add a radio button with a label of Sharpen

              myDialog.optionsPanel.choiceRadioButtons.radioGroup.radioButtonSharpen =

              myDialog.optionsPanel.choiceRadioButtons.radioGroup.add('radiobutton', undefined, "Sharpen");

              myDialog.optionsPanel.choiceRadioButtons.radioGroup.radioButtonSharpen.value = true;

             

              //Add a radio buon with the label of blur

              myDialog.optionsPanel.choiceRadioButtons.radioGroup.radioButtonBlur =

              myDialog.optionsPanel.choiceRadioButtons.radioGroup.add('radiobutton', undefined, "Blur");

             

              //Add tool tip with descriptiond for instructions

              myDialog.optionsPanel.choiceRadioButtons.helpTip = "Choose which operation to perform";

             

              //Add a group for the radius parameter

              myDialog.optionsPanel.radiusGroup = myDialog.optionsPanel.add('group');

              myDialog.optionsPanel.radiusGroup.orientation = "row";

             

              //Add a text for the radius descriptions

              myDialog.optionsPanel.radiusGroup.staticText =

              myDialog.optionsPanel.radiusGroup.add ('statictext', undefined, "Radius");

             

              //Add edittext field to display radius value

              myDialog.optionsPanel.radiusGroup.editText =

              myDialog.optionsPanel.radiusGroup.add ('edittext', undefined, "3.0");

              myDialog.optionsPanel.radiusGroup.editText.preferredSize = [60, 20];

             

              //Add label for units after the edittext field

              myDialog.optionsPanel.radiusGroup.percent =

              myDialog.optionsPanel.radiusGroup.add ('statictext', undefined, "pixels");

             

              //Add the slider control

              myDialog.optionsPanel.slider = myDialog.optionsPanel.add('slider');

              myDialog.optionsPanel.slider.minvalue = 0.1;

              myDialog.optionsPanel.slider.maxvalue = 3.0;

              myDialog.optionsPanel.slider.preferredSize = [160, 20];

             

              //Add tooltips for the slider

              myDialog.optionsPanel.radiusGroup.editText.helpTip = "The radius value of the filter applied.";

              myDialog.optionsPanel.slider.helpTip = "The radius value of the filter applied.";

             

              //----------part 2---------

             

              //Add a popup menu       

              myDialog.optionsPanel.blendPopup.popUp.add ('item', "Vivid Light");

              myDialog.optionsPanel.blendPopup.popUp.add ('item', "Linear Light");

              myDialog.optionsPanel.blendPopup.popUp.add ('item', "Pin Light");

             

              //Set the default selected item in the popup list to the first tiem in the array "Overlay"

              myDialog.optionsPanel.blendPopup.items[0].selected = true;

             

              //Set the value of the blend mode

              myDialog.optionsPanel.blendPopup.popUp.mode = myDialog.optionsPanel.blendPopup.popUp.items[0];

                 

              //----------end part 2----------

             

             

              //----------part 4 -------------

             

              //Add a checkbox for flattening image when done

              myDialog.optionsPanel.flattenBox = myDialog.optionsPanel.add('checkbox', undefined, "Flatten when finished");   

              myDialog.optionsPanel.falttenBox.value = false;

              myDialog.optionsPanel.falttenBox.helpTip =

              "Check yo flatten the layers when processing completed";

              

              //----------end part 4 -------------

             

             

              //DEFAULT OK cancel buttons

             

              //Add a group box for the OK and Cancel buttons   

              myDialog.buttonPanel = myDialog.add ('group');

              mydialog.buttonPanel.orientation = "column";

             

              //Add an OK button

              myDialog.buttonPanel.okButton = myDialog.

              buttonPanel.add('button', undefined, "OK");

             

              //Add a cancel button

              myDialog.buttonPanel.cancelButton = myDialog.

              buttonPanel.add('button', undefined, "Cancel");

             

              return myDialog;

                  

          }

       

       

      //Initialize a High Pass dialog to activate buttons

      function initHighPassDialog(myDialog) {

             

              with (myDialog) {           

                 

                  //Set the edittext field's value to the slider's value if the slider chages

                  optionsPanel.slider.onChanging = function() {

                      optionsPanel.radiusgroup.editText.text =optionsPanel.slider.value;

                      }

                 

                  //Add a group for the blend mode pop UI elements

                  myDialog.optionsPanel.blendPopup = myDialog.optionsPanel.add('group');

                  myDialog.optionsPanel.blendPopup.orientation = "row";

                 

                  //Add popup to select the blendmode

                  myDialog.optionsPanel.blendPopup.popup = myDialog.optionsPanel.blendingPopup.add('dropdownlist');

                  myDialog.optionsPanel.blendPopup.popup.text = "Select the blending mode";

                  myDialog.optionsPanel.blendPopup.helpTip = "Choose the blending mode";

                             

                  //Populate the popup with items

                  myDialog.optionsPanel.blendPopup.popUp.add ('item', "Overlay");

                  myDialog.optionsPanel.blendPopup.popUp.add ('item', "Soft Light");

                  myDialog.optionsPanel.blendPopup.popUp.add ('item', "Hard Light");

                 

                

                  //Set the slider's value to the edittext field's value if the edittext changes

                  optionsPanel.rediudGroup.editText.onChange = function() {

                     

                      optionsPenel.slider.value = optionsPanel.radiusGroup.editText.text;              

                      }

                 

                  //--------------part 3--------------

                 

                  //Set the behavior for the blend menu

                  optionsPanel.blendPopup.popUp.onChange = function() {

                     

                          //Capture any changes the user makes after the dialog is dispalyed,

                          //updating the value of the variable for the blend mode

                          optionsPanel.blendPopup.popUp.mode = optionsPanel.blendPopup.popUp.itemd[this.selection.index];

                      }

                     

                  //--------------end part 3----------

                 

                 

                  //DEFAULT Cancel and OK buttons

                  //OK button clicked

                  buttonPanel.okButton.onCLick = function() {

                     

                       //Check that the radius value makes sense (that itis a number)

                      var radiusValueStr = isNaN(myDialog.optionsPanel.radiusgroup.editText.text);

                     

                      //Data validation

                      if (radiusValueStr ==  true) {

                              alert("Please enter a valid number 0.1 - 250 for 'Radius'");

                              return;

                          }

                      //If  it is a number, makesure it's between 0.1 - 250 and warn hte user if it isn't

                      if (!(my.Dialog.optionsPanel.radiusGroup.editText.text >= 0.1 &&

                      myDialog.optionsPanel.radiusGroup.editText.text <= 250)) {

                              alert ("You must enter a number between 0.1 - 250 for 'Radious'");

                              return;

                          }

       

       

                      //Close the dialog - OK clicked

                      close(1);

                      }

                 

                  //Cancel button clicked

                  buttonPanel.cancelButton.onCLick = function() {

                       //Close the dialog - Cancel clicked

                       close (2);

                      }

                 

                  }

          }

       

       

       

      //Show the dialog and wait for its return value

      function runHighPassDialog(myDialog) {

          

           //Display the dialog

           return myDialog.show();

         

          }

       

       

      //Process the dialog results by showing a message to the user

      function processHighPassResults(myDialog){

         

          //Be sure there is an open document

          if (app.document.length > 0) {

                 

                  //Get user choices from the dialog

                  var radius = myDialog.optionsPanel.slider.value;

                  var blsSharpen = myDialog.optionsPanel.choiceRadioButtons.radioGroup.radioButtonSharpen.value;

                  var bFaltten = myDialog.optionsPanel.flattenBox.value;

                 

                  //figure out the blend mode constant

                  var userChoice = myDialog.optionsPanel.blendPopup.popUp.selection.index;

                  var blendMode = BelndMode.OVERLAY;

                 

                  if (userChoice == 1)

                          blendMode = BlendMode.SOFTLIGHT;

                 

                  else if (userChocie == 2)

                          blendeMode = BlendMode.HARDLIGHT;

                 

                  else if (userChocie == 3)

                          blendeMode = BlendMode.VIVIDLIGHT;       

                 

                  else if (userChocie == 4)

                          blendeMode = BlendMode.LINEARLIGHT;

                 

                  else if (userChocie == 5)

                          blendeMode = BlendMode.PINLIGHT;

                         

                  //Run the operation on the activeDocument

                  HighPassOperation(app.activeDocument, radius, blenMode, blSharpen, bFlatten);       

              }

         

          else {

                  alert("This dialog requires a docuemnt to be open to run successfully!");

              }

         

          

          }