Skip to main content
Inspiring
March 20, 2012
Answered

ScriptUI run script from dialog button

  • March 20, 2012
  • 3 replies
  • 10565 views

Here is what I have right now.  It is a little clunky in the way it works, and I would just like to make it a bit slicker.

I have a script on startup that creates a Menu item with a dropdown submenu item. when you click on the submenu item, it starts a script. 

The first thing the script does is pop up a message box that tells them that two dialog boxes are going to popup. The first one asking them to choose the location of an xml file and the second dialog box that ask them to choose the location of their image folder.  After they finish choosing their folder in the second dialog, the script takes those two values, makes them variables, and continues to run the rest of the script. 

Here is what I would like it to do..

1. open a dialog window with two input fields, two "browse" buttons, and OK and CANCEL buttons

2. when you click the first browse button next to the first input field, it will open a file dialog window.  You select your file and the file string will populate the input field

3. when you click the second button next to the second input field, it opens a folder dialog window.  You select the folder and the string populates the second input field.

4. when you click OK, it feeds the values in the input fields to a script.

I already have scripts to that open the file and folder dialogs, but what I can't figure out  how to do is to start and run a script from a button press, in this case, when the OK button is pressed.  It is probably an onClick, but I haven't been able to find any examples of what the syntax would be. 

The closest I have found is in the Beginning ScriptUI document that was on Jongware's site, in the section on Communication between windows.  But that opens each of the windows with a different script and not using a button press to call a script.

As always, any point in the right direction is very much appreciated. thanks.

This topic has been closed for replies.
Correct answer Peter Kahrel

thanks Peter. that was the correct form.

The next piece of the puzzle is getting the file string info from the dialog box.  In the example in the ScriptUI guide in the COMMUNICATION BETWEEN WINDOWS section, you created both windows and their input fields, and were able to give them different names in order to target them in the second script, copying the text from the first window to the second.

But with the file dialog box, I see a way to give it a title, but not a "name".  So when I tried something like    Window.find("dialog","Window Title");  it didn't work. 

So do you know if there is a specific name given to dialog boxes by default created using the File.openDialog method in order to target them?

and secondly, how would you grab the return value from the dialog?  I have tried things like window.returnValue which should be valid JS, but I cannot find any reference to it, or anything like it, in the Object Model Viewer.

Thank you for your help.


ScriptUI windows and the JS file dialog boxes don't communucate. To get a return value from a file dialog, you'd do something like this:

myFile = Folder (myFolder).openDlg ("Select some files");

if (myFile === null)

   // return, do nothing

The JavaScript Tools Guide CSx (see the ESTK's Help menu) documents several file and folder selection dialogs.

Peter

3 replies

bagonterman
Inspiring
March 23, 2012

This may help in passing your variables to anothe script. I used this little snippet(not my own) to have entry fields remember what the user typed in the last time they ran the dialog. This may help you write to another script. This one rewrites itself.

var schlNamePos = 1002 ;

    var updateFile = function(n){

    var f = File(app.activeScript);

    if ( f.open('e') ){

        f.seek(schlNamePos);

        f.write(''+n);

        f.close();

        }

    }

updateFile(mySchoolName);

Kukurykus
Legend
April 24, 2019

I searched over all Adobe forums for script like yours, so to replace (without RegExp) just single character inside of text file.

But I found one problem I don't know whether there is a tweak for. Namely following script replace not only found character, but length of characters starting from looked for character, so if content of text file is: 0123456789

And I want to replace number 5 for letter 'A' there is no problem, I'm getting: 01234A6789

But if I like to change that for 'ABC' I'm getting 01234ABC89 (instead of 01234ABC6789)

Can anyone help me to change this script it worked the way I need:

fle = File('~/desktop/Nums.txt')

fle.open('e'), i = 0; while(!fle.eof) {

     fle.seek(i); if (fle.readch(i) == '5')

     fle.seek(i), fle.write('ABC'); i++

}

fle.close()

Kukurykus
Legend
April 24, 2019

> replace (without RegExp) just single character inside of text file

Why? That's like 'disassemble this four-stroke engine without using spanners'. So get used to RegExp and do as follows:

find = '5';

change = 'ABC';

re = RegExp (find + '.{' + (change.length-1) + '}');

fle = File('~/desktop/Nums.txt');

fle.open('r');

s = fle.read();

fle.write (s.replace (re, replacement));

fle.close();

P.


That was only very little example of something much bigger I want to achieve. So I did that simple, and mentioned to not use RegExp in the code not without reason. Actually I'm quite good with RegEx. Few years ago I wrote this code in just 2 days to change that the other one:

Action Manager (Script Listener) code converter script!

But now I want to do something different where RegEx is too slow for the files with over few tousends of lines where some characters must be changed to others. The solution is using kind of method like in the sample I left. That gives much faster performance. The only what I have to figure out is how to change one character into few others without cutting the length of fallowed characters from original text.

bagonterman
Inspiring
March 22, 2012

I think what your looking for is the value from entry in the text field? To get this value after the window closes you would use your variable name that you created the entry field with.

So it would be something like this i think. var myColors=myColorsFieldOne.text; myColorsFieldOne being the dialogs variable.

BigGunNAuthor
Inspiring
March 22, 2012

Thanks Gotterman, but the issue I was having was that the dialogs I was opening were not being created by me.  They were more like OS generated windows.  So I am not able to give the window a name, or the input field a name in order to reference it.

Peters response was actually correct, although I did have to open the folder dialog a bit differently to get it to work.  I had to use a style that I got from Jongware in a previous thread, but the concept that Peter showed in the last post pointed me in the right direction. 

I just have one more piece to figure out, how I am going to pass the variables to another script, but I think that is something i will have to work out on my own.

For anyone who is curious, here is the code I came up, modifying and combining some of Peters examples from the book plus the help he gave me in this thread.  The alert after closing the dialog is just for testing to show that the values are being held by the variables so I can use them else where.

function mySnippet(){    

//<fragment>   

#target indesign;

#targetengine "session";

var w2 = new Window("palette", "Window 2", undefined, {resizeable: true});

var e2 = w2.add ("edittext"); e2.characters = 30;

var e3 = w2.add ("edittext"); e3.characters = 30;

var f2 = w2.add ("button", undefined,"Browse XML");              

var f3 = w2.add ("button", undefined,"Browse Folder");              

var f4 = w2.add ("button",undefined,"SAVE AND RUN");              

var tf              

var tfo                

f2.onClick = function()                {               

tf = File.openDialog("Select your XML File","*.xml");                    

if (tf != null)                    

{                        

e2.text = tf;                    

}

}                

f3.onClick = function()                {              

var tfol = new Folder("~/My Documents")               

tfo = tfol.selectDlg("Get Folder");                    

if (tfo != null)                    

{                        

e3.text = tfo;                    

}

}                         

f4.onClick = function()               

{                    

w2.close();                   

alert ("xml value: " + tf  + "\nfolder value: " + tfo);                

}  

w2.show();    

//</fragment>

}

Peter Kahrel
Community Expert
Community Expert
March 20, 2012

> It is probably an onClick

It is indeed. You were looking in the wrong place in that guide. Look in the section "Responding to button presses".

Peter

BigGunNAuthor
Inspiring
March 20, 2012

Thanks for the reply Peter.  I looked in the section you suggested, and see an example of the onClick, but i am still at a loss as to use it to call another script.  My latest attempts was something like this...

var f2 = w2.add ("button", undefined,"Remove Menu");

f2.onClick = function()

{

var f = File('C:\Users\me\AppData\Roaming\Adobe\InDesign\Version 7.5\en_US\Scripts\Scripts Panel\Scripts\MenuTest_1.jsx');

$.evalFile(f);

}

but it doesn't appear to do anything.  I am not getting any error messages or any indication that anything is wrong, it just doesn't seem to do anything at all.

Am I on the right track, and do you see anything obvious that would not cause it to work, or am I going in a completely wrong direction.

BigGunNAuthor
Inspiring
March 20, 2012

so playing around with it a little more, I am getting closer to where I need to be, but not quite there yet.  I have changed what I tried previously to this...

f2.onClick = function()

{

var SCRIPTS_FOLDER =  "/Users/me/AppData/Roaming/Adobe/InDesign/Version 7.5/en_US/Scripts/Scripts Panel/Scripts/";

var Script1 = File(SCRIPTS_FOLDER + "MenuTest_1.jsx");

$.evalFile (Script1);

}

and this worked, but now the issue I am running into is that one of the actual scripts i need to open is a .vbs script, and for some reason, this doesn't seem to be working when trying to run it.

Is there  a special way I would need to call a .vbs file, or can a .jsx not call a .vbs script?