Skip to main content
Byron Nash - INSP
Inspiring
August 2, 2022
Answered

Passing data from ScriptUI to the rest of the script

  • August 2, 2022
  • 1 reply
  • 744 views

I have frequently have issues with getting ScriptUI dialogs to work. I am able to create the dialogs how I want but never seem to handle the onClick and user interaction functions correctly. In this script I used scriptui.joonas.me to generate some code. I'm able to get one of the onClick functions to work but not the other. Can anyone point out what I'm doing wrong?

 

 

// DIALOG
// ======
var dialog = new Window("dialog");
    dialog.text = "INSP Snipe Creator";
    dialog.orientation = "column";
    dialog.alignChildren = ["center","top"];
    dialog.spacing = 10;
    dialog.margins = 16;

// GRP_TEXTFILE
// ============
var grp_textFile = dialog.add("group", undefined, {name: "grp_textFile"});
    grp_textFile.orientation = "row";
    grp_textFile.alignChildren = ["left","center"];
    grp_textFile.spacing = 10;
    grp_textFile.margins = 0;

var st_textFile = grp_textFile.add("statictext", undefined, undefined, {name: "st_textFile"});
    st_textFile.text = "Text file";

var et_textFile = grp_textFile.add('edittext {properties: {name: "et_textFile"}}');
    et_textFile.helpTip = "Path to the text file";
    et_textFile.text = "Click button to choose data file";
    et_textFile.preferredSize.width = 350;

var btn_loadFile = grp_textFile.add("button", undefined, undefined, {name: "btn_loadFile"});
    btn_loadFile.helpTip = "click here to pick a text file.\nIt must be a tab delimited text file.";
    btn_loadFile.text = "...";
    btn_loadFile.onClick = function() {var dataFile = File.openDialog("Select a text file to open.", "")
                            et_textFile.text = dataFile.absoluteURI.replace("%20", " ");};

// DIALOG
// ======
var st_pastedText = dialog.add("group");
    st_pastedText.orientation = "column";
    st_pastedText.alignChildren = ["center","center"];
    st_pastedText.spacing = 0;

    st_pastedText.add("statictext", undefined, "-or- ", {name: "st_pastedText"});
    st_pastedText.add("statictext", undefined, "", {name: "st_pastedText"});
    st_pastedText.add("statictext", undefined, "Data from Spreadsheet", {name: "st_pastedText"});

var et_pastedText = dialog.add('edittext {properties: {name: "et_pastedText", multiline: true, scrollable: true}}');
    et_pastedText.helpTip = "Copy and paste rows of data from excel. ";
    et_pastedText.text = "Paste lines from Excel here";
    et_pastedText.preferredSize.width = 800;
    et_pastedText.preferredSize.height = 150;

// GRP_BUTTONS
// ===========
var grp_buttons = dialog.add("group", undefined, {name: "grp_buttons"});
    grp_buttons.orientation = "row";
    grp_buttons.alignChildren = ["center","center"];
    grp_buttons.spacing = 25;
    grp_buttons.margins = 0;

var btn_create = grp_buttons.add("button", undefined, undefined, {name: "btn_create"});
    btn_create.helpTip = "Run the script and make the snipes";
    btn_create.text = "CREATE SNIPES";
    btn_create.preferredSize.width = 130;
    btn_create.preferredSize.height = 30;
    btn_create.alignment = ["center","center"];
    btn_create.onClick = createSnipes;

var btn_help = grp_buttons.add("button", undefined, undefined, {name: "btn_help"});
    btn_help.text = "?";
    btn_help.onClick = clicked;
dialog.show();



function clicked(){ //this function works
    alert("button click");
    alert(dialog.grp_textFile.et_textFile.text);
}

function createSnipes(){ //this function does not work
    var textInput = dialog.grp_textFile.et_pastedText.text;
    alert("creating snipes now\n"+ textInput);
    var pastedText = textInput.split("\t");
    alert(pastedText[2]);
}

 

This topic has been closed for replies.
Correct answer Dan Ebberts

It looks like you added et_pastedText directly to dialog, so it can't find dialog.grp_textFile.et_pastedText.text

1 reply

Dan Ebberts
Dan EbbertsCorrect answer
Community Expert
August 2, 2022

It looks like you added et_pastedText directly to dialog, so it can't find dialog.grp_textFile.et_pastedText.text

Byron Nash - INSP
Inspiring
August 2, 2022

Thanks Dan. Often it takes going to all the trouble and having another set of eyes on the dang code to see the glaring error.  <facepalm>

Joonass
Inspiring
August 23, 2022

Generally speaking dialog.findElement() is the easiest way to find any item by the name property, because it doesn't care how nested the item is:

dialog.findElement("et_pastedText");


In SDB (scriptui.joonas.me) you can right-click any item to copy this value:


Another option SDB offers is a reference list option that shows all elements with a custom name, the item type, and if that strikes your fancy it does also show how you can find the item, but that said the list object itself it can also work as a way to pick an item. In the screenshot below, as long as I have access to dialog, I can get the ok button and its text like this: 

dialog.items.ok.text
// Or in your case it could be
dialog.items.et_pastedText.text

But like I said, this only contains items you've given a custom name. Every single item SDB creates gets a name automatically, but they won't be on this list. In most cases the findElement() method is probably your best option and just like you can see in the gif above, you can easily copy the findElement() method even for these automatically named items.