Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Window dialog with error check

Engaged ,
Apr 15, 2016 Apr 15, 2016

I am trying to figure this out and my brain seems to think it is the weekend already.

What I am looking for is.....

if the user clicks OK without check boxing at least one name....it tells them to "Please make a selection"

if the user clicks CANCEL it closes and tells them "Script terminated with no results"

if the user clicks any of the check boxes it continues the script and writes the file....here is what I have....

#target illustrator

var doc = app.activeDocument;

var allText = doc.textFrames;

var emailAddress = [];

////////////////////  PRIMARY CONTENT TO CHANGE FOR USERS  ////////////////////

/*  IF PL's ARE ADDED OR REMOVED MAKE THE CORRESPONDING CODE MATCH APROX. LINE 86 */

var nameOne = "NAME 1";

var emailOne = "nameONE@email.com";

var nameTwo = "NAME 2";

var emailTwo = "nameTwo@email.com";

var nameThree = "NAME 3";

var emailThree = "nameThree@email.com";

var desktopPath = "C:\\Users\\USER_NAME_HERE\\Desktop\\";

////////////////////  PRIMARY CONTENT TO CHANGE FOR USERS  ////////////////////

// START OF CHECK BOX SCRIPT

myDlg = new Window('dialog', 'Email');

myDlg.orientation = 'column';

myDlg.alignment = 'right';

with(myDlg.add('group')) {

    orientation = 'row';

    with(add('panel')) //First structure of Window

        {

            orientation = 'column';

            add('statictext', undefined, "Select USER");

            with(add('group')) {

                orientation = 'column';

                alignChildren = 'left';

                var plOne = add('checkbox', undefined, nameOne);

                var plTwo = add('checkbox', undefined, nameTwo);

                var plThree = add('checkbox', undefined, nameThree);

                var btnOK = add('button', undefined, 'OK');

                btnOK.onClick = function() {

                    myDlg.close();

                    return this.value = true;

                }

         var btnCancel = add('button', undefined, 'CANCEL');

                    btnCancel.onClick = function() {

                        myDlg.close();

                        return this.value = true;

                    }

            }

        }

}

myDlg.show();

// END OF CHECK BOX SCRIPT

// IF NAME 1 IS CHECKED

if (plOne.value == true) {

    emailAddress.push(emailOne);

}

// IF NAME 2 IS CHECKED

if (plTwo.value == true) {

    emailAddress.push(emailTwo);

}

// IF NAME 3 IS CHECKED

if (plThree.value == true) {

    emailAddress.push(emailThree);

}

// JOIN EMAILS AND FORMAT

var allEmails = emailAddress.join(', ');

// FIND THE MEDIA NUMBER LISTED ON THE COVER PAGE

for (var i = 0; i < allText.length; i++) {

    if (allText.layer == "[Layer Single Line]" || allText.layer == "[Layer Two Line]" || allText.layer == "[Layer Three Line]" || allText.layer == "[Layer Four Line]") {

        if (allText.contents.substr(0, 4) == "UENR" || allText.contents.substr(0, 4) == "RENR" || allText.contents.substr(0, 4) == "SENR" || allText.contents.substr(0, 4) == "KENR") {

            var mediaNumber = allText.contents;

        }

    }

}

// CREATE NEW FOLDER WITH MEDIA NUMBER ON DESKTOP

var mediaFolder = new Folder(desktopPath + mediaNumber.substring(0, 8));

mediaFolder.create();

// START OF HTML FILE INFO //

var str = '<html>\r' + '\r' + '<head>\r' +

  '<meta name="Internet E-Mail Address" content="' + allEmails + '">\r' +

    '<a href="' + mediaNumber.substr(0, 8) + '_SIS.pdf">Main Schematic</a><br>\r';

var fTwo = File(desktopPath + mediaNumber.substr(0, 8) + ".txt");

fTwo.open('w');

fTwo.write(str);

fTwo.close();

Any help would be appreciated!

TOPICS
Scripting
985
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe
Valorous Hero ,
Apr 15, 2016 Apr 15, 2016

I always put my UI windows into their own function and use this method to do my OKing and Canceling:

if(w.show() == 2){

     alert("Cancelled");

     return null;

} else {

     return myDataObj;

}

So when the window dismisses, you can see if the returned data is a null or something else, and based on that quit or proceed with the rest of the script.

To validate the user input, you will need to put a validation function in the OK button's click event, to prevent closing the window when input is invalid.

Sorry, I've never used with statements in JS, as I've been told by many sources that it's not a good practice (something about stuff being too ambiguous and leading to bugs?), but in the case of ScriptUI it looks hard to read -- of course I'm not used to reading them this way at all.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Apr 18, 2016 Apr 18, 2016

Any chance you could expand on that? I have never written a UI window. I just used some code that I found online (which just happen to include the with part of the code) and modified to my own needs.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 19, 2016 Apr 19, 2016

Ok. I started writing a reply to this early this morning, and kept piecing together an example throughout the day during brief downtimes.

Here's an example that i wrote up that shows how you can do form validation from within a script UI function (using no with statements). Basically the UI function is repeatedly self invoked until the information is either correct or until you hit cancel. I'd be glad to answer any questions you may have (as i've had a TON of headaches related to wrapping my head around script UI stuff). This was originally supposed to be a really simple, quick example, and it kept getting more and more complex as i experimented with different things. But i hope it's clear enough to help you out. Again, please ask questions, i'm glad to help.

function wrapper(){

    var resultsOfDialog = makeUIDialog()

    var defaultText1 = "";

    var defaultText2 = "";

    function makeUIDialog()

    {

        var result = {};

        if(result == null)

            return null;

        var myDlg = new Window("dialog","Text Description");

            var txtInput1 = myDlg.add("statictext", undefined, "Enter Some Text");

            var userInput1 = myDlg.add("edittext", undefined, defaultText1);

                userInput1.characters = 25;

            var txtInput2 = myDlg.add("statictext", undefined, "Enter an integer greater than 0");

            var userInput2 = myDlg.add("edittext", undefined, defaultText2);

                userInput2.characters = 2;

            var btnGroup = myDlg.add("group")

                var submitButton = btnGroup.add("button", undefined, "OK");

                var cancelButton = btnGroup.add("button", undefined, "Cancel");

          

        function validate()

        {

          

            if(myDlg.show() == 1)

            {

                defaultText1 = userInput1.text;

                defaultText2 = userInput2.text;

                if(userInput1.text == "")

                {

                    alert("Such and Such can't be empty!");

                    result = null;

                    makeUIDialog();

                }

                else

                {

                    result["userInput1"] = userInput1.text;

                }

                if(parseInt(userInput2.text) * 0 != 0)

                {

                    alert("Such and Such Else must be an integer!");

                    result = null;

                    makeUIDialog();

                }

                else if(parseInt(userInput2.text)<1)

                {

                    alert("Such and Such must be greater than 0!");

                    result = null;

                    makeUIDialog();

                }

                else

                {

                    result["userInput2"] = userInput2.text;

                }

              

            }

            else

            {

                alert("Why you cancel? You stupid!");

                result = null;

            }

      

          

        }

        validate();

        return result;

    }

    if(resultsOfDialog != null)

    {

        alert("Results:\nUser Input 1 = " + resultsOfDialog["userInput1"] + "\nUser Input 2 = " + resultsOfDialog["userInput2"]);

    }

}

wrapper();

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Apr 20, 2016 Apr 20, 2016

williamadowling​ thank you for taking the time to write this out. I have been banging my head against the wall trying to get this to work (and still am). So here is my code updated to my info....

#target illustrator

var doc = app.activeDocument; 

var allText = doc.textFrames; 

var emailAddress = [];

function wrapper() {

    var resultsOfDialog = makeUIDialog()

    function makeUIDialog() {

        var result = {};

        if (result == null)

            return null;

        var myDlg = new Window("dialog", "NUP Email");

  myDlg.orientation = 'column';

  myDlg.panel = myDlg.add('panel', undefined, "Select Project Leader(s)");

  myDlg.panel.alignChildren = 'left';

  var plOne = myDlg.panel.add('checkbox', undefined, nameOne);

  var plTwo = myDlg.panel.add('checkbox', undefined, nameTwo);

  var plThree = myDlg.panel.add('checkbox', undefined, nameThree);

        var btnGroup = myDlg.add("group")

        var okButton = btnGroup.add("button", undefined, "OK");

        var cancelButton = btnGroup.add("button", undefined, "Cancel");

        function validate() {

            if (myDlg.show() == 1) {

                if (plOne.value == false && plTwo.value == false && plThree.value == false) {

                    alert("Please select a project leader to receive the NUP email");

                    result = null;

                    makeUIDialog();

                }

            } else {

                alert("Script stopped with no results");

                result = null;

            }

        }

        validate();

        return result;

    }

    if (resultsOfDialog != null) {

        alert("Results:\nplOne = " + resultsOfDialog["plOne"]);

    }

}

wrapper();

Now how do I add this portion into that??....

  1. ////////////////////  PRIMARY CONTENT TO CHANGE FOR USERS  //////////////////// 
  2. /*  IF PL's ARE ADDED OR REMOVED MAKE THE CORRESPONDING CODE MATCH APROX. LINE 86 */ 
  3. var nameOne = "NAME 1"
  4. var emailOne = "nameONE@email.com"
  5.  
  6.  
  7. var nameTwo = "NAME 2"
  8. var emailTwo = "nameTwo@email.com"
  9.  
  10.  
  11. var nameThree = "NAME 3"
  12. var emailThree = "nameThree@email.com"
  13.  
  14.  
  15. var desktopPath = "C:\\Users\\USER_NAME_HERE\\Desktop\\"
  16.  
  17.  
  18. ////////////////////  PRIMARY CONTENT TO CHANGE FOR USERS  ////////////////////

and this.....

// IF NAME 1 IS CHECKED 

if (plOne.value == true) { 

    emailAddress.push(emailOne); 

// IF NAME 2 IS CHECKED 

if (plTwo.value == true) { 

    emailAddress.push(emailTwo); 

// IF NAME 3 IS CHECKED 

if (plThree.value == true) { 

    emailAddress.push(emailThree); 

// JOIN EMAILS AND FORMAT 

var allEmails = emailAddress.join(', '); 

// FIND THE MEDIA NUMBER LISTED ON THE COVER PAGE 

for (var i = 0; i < allText.length; i++) { 

    if (allText.layer == "[Layer Single Line]" || allText.layer == "[Layer Two Line]" || allText.layer == "[Layer Three Line]" || allText.layer == "[Layer Four Line]") { 

        if (allText.contents.substr(0, 4) == "UENR" || allText.contents.substr(0, 4) == "RENR" || allText.contents.substr(0, 4) == "SENR" || allText.contents.substr(0, 4) == "KENR") { 

            var mediaNumber = allText.contents; 

        } 

    } 

// CREATE NEW FOLDER WITH MEDIA NUMBER ON DESKTOP 

var mediaFolder = new Folder(desktopPath + mediaNumber.substring(0, 8)); 

mediaFolder.create(); 

// START OF HTML FILE INFO // 

var str = '<html>\r' + '\r' + '<head>\r' + 

  '<meta name="Internet E-Mail Address" content="' + allEmails + '">\r' + 

    '<a href="' + mediaNumber.substr(0, 8) + '_SIS.pdf">Main Schematic</a><br>\r'; 

 

var fTwo = File(desktopPath + mediaNumber.substr(0, 8) + ".txt"); 

fTwo.open('w'); 

fTwo.write(str); 

fTwo.close(); 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 20, 2016 Apr 20, 2016

can you please clarify what's going on in this loop? Is it possible for you to post an example file and a specific description of the information you want to pull out and the possible locations the information might be within the file?

Your loop is cycling the textFrames in the document, and then trying to determine which layer it's on? Does that mean that the mediaNumber could be on any of 4 different layers called "One Line", "Two Line", "Three Line" or "Four Line"?

Will a media number always start with "UENR", "RENR", "SENR" or "KENR"?

I've almost got this whole thing written and finished, i'm just caught up on this loop and what information is going to be returned. A sample file would be a HUGE help.

  1. for (var i = 0; i < allText.length; i++) {   
  2.     if (allText.layer == "[Layer Single Line]" || allText.layer == "[Layer Two Line]" || allText.layer == "[Layer Three Line]" || allText.layer == "[Layer Four Line]") {   
  3.         if (allText.contents.substr(0, 4) == "UENR" || allText.contents.substr(0, 4) == "RENR" || allText.contents.substr(0, 4) == "SENR" || allText.contents.substr(0, 4) == "KENR") {   
  4.             var mediaNumber = allText.contents;   
  5.         }   
  6.     }   
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Apr 21, 2016 Apr 21, 2016

For copyright reasons I can not upload a full sample file. But yes the way you described it is correct.

This script will be run on multiple types of documents.

It will ALWAYS contain one of the following layers:

"One Line", "Two Line", "Three Line" or "Four Line"

On one of those layers there will ALWAYS be one text frame that starts with:

"UENR", "RENR", "SENR" or "KENR"

That text frame is the media number of the document. There is only one and it is very specific.

Hope that makes sense....and sorry about not being able to upload a file.

Thank you both for your continuing conversation on this! This UI stuff is new to me and I am loving learning about it!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
Apr 20, 2016 Apr 20, 2016

Hey will, I remember I first experimented with a self-calling dialog which would re-produce the dialog window when the validation failed, kind of like in your example. The problem with that is among other things, the stuff a person may have filled in previously will be gone, and at least in my experience, calling a dialog like so had yielded a bunch of unexpected results.

The best way is to just prevent the dialog from closing with the validation function.

This is an example of the format I always use:

//---------------------- start ---------------------//

var NAMES = ["Art", "Bob", "Don"]; // a 'global-to-script' variable which can be accessed by all functions

//------------------------ UI ----------------------//

function validate(dropdownElem){

  return dropdownElem.selection != null;

};

function myUIDialog(){

var w = new Window("dialog", "My Dialog!");

var myDropdown = w.add("dropdownlist", undefined, NAMES);

var btnOk = w.add("button", undefined, "Ok");

btnOk.onClick = function(){

   if(validate(myDropdown)){

     w.close();

   } else {

     alert("Select a name first.");

   }

};

if(w.show() == 2){

   alert("Cancelled");

   return null;

} else {

   return { selectedName : myDropdown.selection.text };

}

};

var userInput = myUIDialog();

if(userInput == null){

return; // exits the script when dialog is cancelled.

}

//---------------- rest of script -------------------//

alert("You have chosen " + userInput.selectedName);

//----------------------- end -----------------------//

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 20, 2016 Apr 20, 2016

Yea i did notice that when i was testing, so my solution was to save the user's input upon clicking "OK" and then populating the fields with whatever was in the field when "OK" was pressed.

Your solution is definitely more elegant though and doesn't eat up extra variables like mine did. I was initially trying to do something very similar to what you have there, but I was having issues attaching an onclick function to the buttons, (which i just realized right now that I was trying to use "button.onclick" instead of "button.onClick" because in web based stuff, it's all lowercase.. i think that's the issue i was having... )

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 26, 2016 Apr 26, 2016
LATEST

Ok, sorry this took a little longer than i wanted. I got pretty busy at work.

I incorporated your media number loop into my version of the script, so you can see where everything goes at least in terms of the flow of control. I'm sure this isn't the most efficient or even the best way to do this, and it could definitely be much cleaner, but it seems to work (though i can't do very intensive testing without the file). Let me know if you have any other issues or questions.

as i normally do, i began second guessing this as i was typing the message and it works so long as the validation works the first time... For example, if you don't enter text in the first dialog box, the UI window will re-launch and when you submit the info the second time, the data comes back undefined.. I'm still looking into that, but at least this works the first time 'round and may help you finish up your own code.

function wrapper(){

    var docRef = app.activeDocument;

    var resultsOfDialog = makeUIDialog()

    var defaultText1 = "";

    var defaultText2 = "";

    function makeUIDialog()

    {

        var result = {};

        if(result == null)

            return null;

        var myDlg = new Window("dialog","Text Description");

            var txtInput1 = myDlg.add("statictext", undefined, "Enter Some Text");

            var userInput1 = myDlg.add("edittext", undefined, defaultText1);

                userInput1.characters = 25;

            var txtInput2 = myDlg.add("statictext", undefined, "Enter an integer greater than 0");

            var userInput2 = myDlg.add("edittext", undefined, defaultText2);

                userInput2.characters = 2;

            var btnGroup = myDlg.add("group")

                var submitButton = btnGroup.add("button", undefined, "OK");

                var cancelButton = btnGroup.add("button", undefined, "Cancel");

        

        function validate()

        {

        

            if(myDlg.show() == 1)

            {

                defaultText1 = userInput1.text;

                defaultText2 = userInput2.text;

                if(userInput1.text == "")

                {

                    alert("Such and Such can't be empty!");

                    // result = null;

                    makeUIDialog();

                }

                else

                {

                    result["userInput1"] = userInput1.text;

                }

                if(parseInt(userInput2.text) * 0 != 0)

                {

                    alert("Such and Such Else must be an integer!");

                    result = null;

                    makeUIDialog();df

                }

                else if(parseInt(userInput2.text)<1)

                {

                    alert("Such and Such must be greater than 0!");

                    result = null;

                    makeUIDialog();

                }

                else

                {

                    result["userInput2"] = userInput2.text;

                }

            

            }

            else

            {

                alert("Why you cancel? You stupid!");

                result = null;

            }

    

        

        }

        validate();

        var allText = docRef.textFrames;

        try

        {

            for (var i = 0; i < allText.length; i++)

            {

                if (allText.layer == "[Layer Single Line]" || allText.layer == "[Layer Two Line]" || allText.layer == "[Layer Three Line]" || allText.layer == "[Layer Four Line]")

                {

                    if (allText.contents.substr(0, 4) == "UENR" || allText.contents.substr(0, 4) == "RENR" || allText.contents.substr(0, 4) == "SENR" || allText.contents.substr(0, 4) == "KENR")

                    {

                        var mediaNumber = allText.contents;

                    }

                }

            }

        }

        catch(e)

        {

            result["mediaNumber"] = null;

        }

        result["mediaNumber"] = mediaNumber;

        return result;

    }

    if(resultsOfDialog != null)

    {

        alert("Results:\nUser Input 1 = " + resultsOfDialog["userInput1"] + "\nUser Input 2 = " + //

            resultsOfDialog["userInput2"] + "\nMedia Number = " + resultsOfDialog["mediaNumber"]);

    }

}

wrapper();

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines