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

Passing data from ScriptUI to the rest of the script

Explorer ,
Aug 02, 2022 Aug 02, 2022

Copy link to clipboard

Copied

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]);
}

 

TOPICS
Scripting

Views

204

Translate

Translate

Report

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

correct answers 1 Correct answer

Community Expert , Aug 02, 2022 Aug 02, 2022

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

Votes

Translate

Translate
Community Expert ,
Aug 02, 2022 Aug 02, 2022

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

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
Explorer ,
Aug 02, 2022 Aug 02, 2022

Copy link to clipboard

Copied

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>

Votes

Translate

Translate

Report

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
Explorer ,
Aug 23, 2022 Aug 23, 2022

Copy link to clipboard

Copied

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:
CleanShot 2022-08-23 at 12.39.22.2022-08-23 12_42_38.gif


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.

CleanShot 2022-08-23 at 12.28.09@2x.png

Votes

Translate

Translate

Report

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
Explorer ,
Aug 23, 2022 Aug 23, 2022

Copy link to clipboard

Copied

Is there a way to have your super helpful webapp export resource spec text?

Votes

Translate

Translate

Report

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
Explorer ,
Aug 23, 2022 Aug 23, 2022

Copy link to clipboard

Copied

LATEST

Unfortunately no.

 

I've had that feature request a few times and they wanted it because it's more compact, but I deemed it not good enough reason  given how much work it would be, essentially building the export again from the ground up. Including the dialog from a separate file or minifying the code does about the same. Then there's the hard core people who  said the benefit they get from it (whatever it was) is such that they wouldn't use SDB in the first place.

Votes

Translate

Translate

Report

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