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

BigGunNAuthor
Inspiring
March 23, 2012

Thanks. That will probably come in helpful at some point. Unfortunately it won't help me at the moment. 

However, I did figure out how to pass the variables based on this post.  I am setting them up as environment variables in my JSX file like so..

f2.onClick = function()                {              

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

if (tf != null)                   

{                       

e2.text = tf;

tf = tf.absoluteURI

$.setenv("myFileVar",tf)                 

}

}

and then grabbing them in my  VBS script  like this..

Set WshShell = CreateObject("WScript.Shell")

Set objEnv = WshShell.Environment("Process")

myFileName = objEnv("myFileVar")

The problem that I am running into is that the file and folder dialogs don't give me an absolute path, but a path that is relative to the startup disk.  It is discussed in this post and I haven't found any way to get around it yet.  From the discussion, it doesn't sound like there is a work around, so I may just be out of luck in trying to do what I want to do.  But I am going to keep trying for a bit longer.

John Hawkinson
Inspiring
March 24, 2012

The problem that I am running into is that the file and folder dialogs don't give me an absolute path, but a path that is relative to the startup disk.  It is discussed in this post and I haven't found any way to get around it yet.  From the discussion, it doesn't sound like there is a work around, so I may just be out of luck in trying to do what I want to do.  But I am going to keep trying for a bit longer.

I think that thread is somewhat confused about how file path works, and also that is Mac OS -specific. Can you please give a very specific example of the problematic file path? With real actual values and show how it is a problem?

(Environment variables? Wow. I would have hoped there was a better way. But I'm not a VB programmer.)

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?