Alder Posted January 7, 2006 Share Posted January 7, 2006 Hi all, I'm writing an HTML Application (HTA) with VBScript that will automate an ActiveX control put out by AutoDesk: the MapGuide Viewer ActiveX Control. This control exposes a number of objects and methods for scripting, but not one that will send print jobs directly from code. There is simply a method to pop open a Page Setup dialog. After that the user must manually select print options and click OK to set print options and send the print job to the selected printer. Since the HTA will often require the user to suffer through 400-500 repetitions, I need to find a way to control this dialog, and probably others. The forums for MapGuide are extremely quiet and I've received no responses there for a week. I have precious little experience writing HTAs, or VBScript, for that matter, so if someone could take a look at my code and make a suggestion for getting keystrokes to work I'd really appreciate it. Thanks. My HTA uses 4 documents: example.hta; frmMap.html; frmTitle.html; and frmButtons.html. The .hta file simply contains a FRAMESET to define frames for the three HTML files. The MapGuide ActiveX Control is activated by an <OBJECT> tag in the frmMap.html file. The script for opening and controlling the Page Setup dialog is located in the frmButtons.html file. Here are the relevant bits: ------- Example.hta ------- <html> <head> <title>Viewer API Frames Example</title> <HTA:APPLICATION id="appHTA" <frameset rows="10%,*,15%"> <frame application="yes" src="frmTitle.html" /> <frame application="yes" src="frmMap.html" name="mapframe" scrolling="no" /> <frame application="yes" src="frmButtons.html" /> </frameset> </head> </html> ------- frmButtons.html ------- <html> <head> <script language="VBScript" type="text/vbscript"> <!-- Option Explicit Dim objITX Set objITX = CreateObject("AutoITX3.Control") Sub SetLandscapeMode 'Enable landscape mode on printer With objITX .Sleep 100 .Send "!p" 'pop open the ActiveX control's Print Setup dialog .Sleep "100" .Send "!a" 'select Landscape mode .Sleep 100 End With End Sub 'SetLandscapeMode Sub cmdPrint_onclick objMap.PageSetupDlg 'pops open the ActiveX control's Page Setup dialog ' Set landscape mode Call SetLandscape ' Print the map objITX.Send "{ENTER}" End Sub 'cmdPrint_onclick --> </script> <body> <div align="center"> <form> <input type="button" name="cmdPrint" value="Print View" /> </form> </div> </body> </html> ------- frmMap.html ------- <html> <head> </head> <body> <object id="objMap" width="100%" height="100%" align="baseline" classid="CLSID:62789780-B744-11D0-986B-00609731A21D" codebase="http://images.autodesk.com/adsk/files/MgAxCtrl.cab#Version=6,5,5,25" /> <param name="URL" value="file:///H:\MGSite\map.mwf" /> <param name="Lat" value="55.928142" /> <param name="Lon" value="-120.590787" /> <param name="MapScale" value="50000" /> <param name="Units" value="M" /> <param name="ToolBar" value="big" /> <param name="StatusBar" value="On" /> <param name="LayersViewWidth" value="0" /> <param name="URLListState" value="0" /> <param name="AutoLinkDelay" value="20" /> </object> </body> </html> Link to comment Share on other sites More sharing options...
PeterSwiss Posted January 7, 2006 Share Posted January 7, 2006 Hi, I can't speak for MapGuide, but I tried to steer Word and it works. First a few words about the sequence of (exactly) four events of your VBS code, as it is presented and I replay it, and for this integrate the subroutine SetLandscapemode into cmdPrint you call Page Setup you call Print Setup you set landscape with !a you dismiss one dialog with {ENTER}, comment is Print the map. I would call page setup dialog, dismiss it, call print dialog, dismiss it. Maybe printing does not love an open Page Setup in parallel to an open Print Dialog. But it could well be nested in the case of MapGuide. We with high probability need two {ENTER} to dismiss both dialogs, no matter what the relationship is. Next I tried to control Words print dialog with the following code. Notes: I had Word open manually, launched the script and brought Word into foreground manually after dismissing display of the date (to make things simple). Notes to the script snippet: both ways given to call the Print Dialog behaved ok, "^p" and "!dd". "13" is the number of TABs required to reach the page-number field, in the Print Dialog (of my version of Word), into which the script writes "15" and then cancels. var ws = WScript; //WSH Grundobjekt var autoit = WScript.CreateObject("AutoItX3.Control"); //AutoIt V3 Objekt v = new Date(); ws.echo(v); autoit.Sleep(5000); //autoit.Send("^p"); autoit.Send("!dd"); for (var i=0;i<13;i++) autoit.Send("{TAB}"); autoit.Send("15"); autoit.Sleep(5000); autoit.Send("{ESCAPE}"); ws.Quit(); Link to comment Share on other sites More sharing options...
Alder Posted January 8, 2006 Author Share Posted January 8, 2006 Hi, I can't speak for MapGuide, but I tried to steer Word and it works.First a few words about the sequence of (exactly) four events of your VBS code, as it is presented and I replay it, and for this integrate the subroutine SetLandscapemode into cmdPrintyou call Page Setupyou call Print Setupyou set landscape with !ayou dismiss one dialog with {ENTER}, comment is Print the map.I would call page setup dialog, dismiss it, call print dialog, dismiss it. Maybe printing does not love an open Page Setup in parallel to an open Print Dialog. But it could well be nested in the case of MapGuide. We with high probability need two {ENTER} to dismiss both dialogs, no matter what the relationship is.Next I tried to control Words print dialog with the following code. Notes: I had Word open manually, launched the script and brought Word into foreground manually after dismissing display of the date (to make things simple). Notes to the script snippet: both ways given to call the Print Dialog behaved ok, "^p" and "!dd". "13" is the number of TABs required to reach the page-number field, in the Print Dialog (of my version of Word), into which the script writes "15" and then cancels.var ws = WScript; //WSH Grundobjektvar autoit = WScript.CreateObject("AutoItX3.Control"); //AutoIt V3 Objektv = new Date();ws.echo(v);autoit.Sleep(5000);//autoit.Send("^p");autoit.Send("!dd");for (var i=0;i<13;i++) autoit.Send("{TAB}");autoit.Send("15");autoit.Sleep(5000);autoit.Send("{ESCAPE}");ws.Quit();PeterSwiss,Thanks for the response. Just so you know, both dialogs (Page Setup and Print Setup) are specific to the MapGuide ActiveX control, and yes, the Print Setup is called from a button on the Page Setup dialog, i.e. it is 'nested' in that sense. It is also a consistent route to set landscape mode and does vary if the printer changes. Even if I merely want to select a checkbox on the Page Setup dialog, which does not open another dialog box, the key codes go nowhere.Maybe it's the difference between using the AutoITX control and the AutoIT script, like you did.I have tried the Window Info tool that came with the control, but am unclear on how I would use the information to ensure I had the Page Setup dialog in focus. Any expereince with that stuff?Cheers, Link to comment Share on other sites More sharing options...
PeterSwiss Posted January 9, 2006 Share Posted January 9, 2006 (edited) Hi, I use AutoItX, but the language is Javascript, executed from the Windows Scripting Host. It should be available to you. Why don't you just try my sample script (the code goes to test.js and you double-click it, and see whether it works, steering a ready Word.) You may rewrite it in VBS. I tried to steer Internet Explorer with the same script. I added Sleeps between the Sends. With a delay of 800 or 1000 between the Sends it works, with a delay of 100 it does'nt, with a delay of 500 some but not all {TAB} go thru, 5 are needed in case of IE to reach the page number field. Your Sleeps are 100 msec. I needed 800 msec for Internet Explorer. The window info tool is for showing in clear text Window Titles and Control IDs. That's to know them for Win... and Control... calls. It is not needed here as the keys go to the foreground window. Launch it , bring the other application to foreground and move the cursor over points of interest. Peter. Edited January 9, 2006 by PeterSwiss Link to comment Share on other sites More sharing options...
Alder Posted January 9, 2006 Author Share Posted January 9, 2006 Hi, I use AutoItX, but the language is Javascript, executed from the Windows Scripting Host. It should be available to you. Why don't you just try my sample script (the code goes to test.js and you double-click it, and see whether it works, steering a ready Word.) You may rewrite it in VBS.I tried to steer Internet Explorer with the same script. I added Sleeps between the Sends. With a delay of 800 or 1000 between the Sends it works, with a delay of 100 it does'nt, with a delay of 500 some but not all {TAB} go thru, 5 are needed in case of IE to reach the page number field. Your Sleeps are 100 msec. I needed 800 msec for Internet Explorer.The window info tool is for showing in clear text Window Titles and Control IDs. That's to know them for Win... and Control... calls. It is not needed here as the keys go to the foreground window. Launch it , bring the other application to foreground and move the cursor over points of interest.Peter.Thanks again, PeterSwiss.To be sure, the script will open the Print dialog in Internet Explorer, but printing from IE will result in a print job different from the one sent directly from MapGuide's own Print Setup dialog. That's the rub. MapGuide throws up a modal dialog box that seems impossible to control, though the Window Info tool of AutoIT DOES identify the Class and Name of the dialog box.Would you, or anyone else, have sample code for using this information to send keystrokes to a particular window using its Class and/or Name attributes?Cheers,Terry Link to comment Share on other sites More sharing options...
PeterSwiss Posted January 9, 2006 Share Posted January 9, 2006 (edited) Thanks again, PeterSwiss.To be sure, the script will open the Print dialog in Internet Explorer, but printing from IE will result in a print job different from the one sent directly from MapGuide's own Print Setup dialog. That's the rub. MapGuide throws up a modal dialog box that seems impossible to control, though the Window Info tool of AutoIT DOES identify the Class and Name of the dialog box.Cheers,TerryTerry, from my experience with Internet Explorer I suggest you try first to increase the Sleep time in your script from 100 to much larger (as the key strokes have to go thru IE and long sleeps were needed in my IE test). If that does not work immediately, you can always try to steer IE with my script, first.There's a principle of trying something simpler if the target (in this case, MapGuide) does not react well, to separate the possible causes. I did get the same result in my Internet Explorer test, as you got for MapGuide (nothing works at all, no error message, just no result) with Sleeps of your amount - 100 msec. I'm sure you did install AutoItX correctly - but - if not, it again would react the same way. No error message, just no result.Peter. Edited January 10, 2006 by PeterSwiss Link to comment Share on other sites More sharing options...
PeterSwiss Posted January 10, 2006 Share Posted January 10, 2006 Terry it seems to eat keystrokes, that is, it is unreliable. I tried with a sample MapGuide from their website. I tried ENTER and ESC, I tried TABs and ENTER, with enough Sleeping between. Maybe clicks are more reliable. see autoit.MouseClick and autoit.AutoItSetOption(MouseControlMode). For simple IE and simple Word I did not notice unreliable behaviour. Peter Link to comment Share on other sites More sharing options...
Alder Posted January 10, 2006 Author Share Posted January 10, 2006 Terry it seems to eat keystrokes, that is, it is unreliable. I tried with a sample MapGuide from their website. I tried ENTER and ESC, I tried TABs and ENTER, with enough Sleeping between. Maybe clicks are more reliable. see autoit.MouseClick and autoit.AutoItSetOption(MouseControlMode). For simple IE and simple Word I did not notice unreliable behaviour.PeterI used the Window Info tool to get the X,Y screen coordinates of the Print Setup button on the Page Setup dialog, then issued this from script with the Page Setup dialog already open: map.PageSetupDlg objITX.Sleep 800 MouseClick "", 620, 425The mouse moved to these coordinates (and resulted in a map selection), but only after I dismissed the Page Setup dialog box. I also tried the ControlFocus and ControlClick methods using the information from the Window Info tool.Looks like it's time for a feature request to AutoDesk!Thanks for all your help, PeterSwiss. Link to comment Share on other sites More sharing options...
PeterSwiss Posted January 10, 2006 Share Posted January 10, 2006 I used the Window Info tool to get the X,Y screen coordinates of the Print Setup button on the Page Setup dialog, then issued this from script with the Page Setup dialog already open: map.PageSetupDlg objITX.Sleep 800 MouseClick "", 620, 425The mouse moved to these coordinates (and resulted in a map selection), but only after I dismissed the Page Setup dialog box. I also tried the ControlFocus and ControlClick methods using the information from the Window Info tool.Looks like it's time for a feature request to AutoDesk!Thanks for all your help, PeterSwiss.Terry, this is to be expected, because it is in the same thread. For what you want, you need something more elaborate, but still it is just a few lines of code. You have to launch another execution, that is a WScript.Shell.Exec "another script.js or .vbs", which runs in parallel to your script. Pls note this is also what I did when controlling IE or word from outside. Look for documentation of the WshShell object and its Exec method.Or better, if available, pop open the dialogs from key strokes.... .I got the MS script documentation from a MS website, probably http://msdn.microsoft.com/library/default..../Scriptinga.aspand could download it.Peter Link to comment Share on other sites More sharing options...
Alder Posted January 11, 2006 Author Share Posted January 11, 2006 Terry, this is to be expected, because it is in the same thread. For what you want, you need something more elaborate, but still it is just a few lines of code. You have to launch another execution, that is a WScript.Shell.Exec "another script.js or .vbs", which runs in parallel to your script. Pls note this is also what I did when controlling IE or word from outside. Look for documentation of the WshShell object and its Exec method.Or better, if available, pop open the dialogs from key strokes.... .I got the MS script documentation from a MS website, probably http://msdn.microsoft.com/library/default..../Scriptinga.aspand could download it.PeterIf I understand correctly, this parallel script would run until the calling script itself exited and use something like a While..Wend loop to monitor the window for any appearance of a Page Setup dialog?Cheers,Terry Link to comment Share on other sites More sharing options...
PeterSwiss Posted January 11, 2006 Share Posted January 11, 2006 (edited) If I understand correctly, this parallel script would run until the calling script itself exited and use something like a While..Wend loop to monitor the window for any appearance of a Page Setup dialog?Cheers,TerryAs I see it,script 1:calls script 2 via WScript.Shell.Execcalls map.PageSetupDlg //pauses until OK is hit, does not proceed earlier more steps...script2:waits a few seconds (perhaps 10 seconds, to start with in development) // or (later in development:) until the window with the specified title appears or similarclicks into the page setup dlg as needed, works with auto.Controlxxx callsexitsBUT you called page setup with !p in your first example. IF this works but I've not seen anything in mapguide about keyboard equivalentsscript1:write !pwait a few secondsclick into the page setup dlg as neededYet another remark:I noticed that ENTER and ESC commands were processed unreliably. Maybe, a sequence of TAB (to obtain the right control), and then ENTER resp SPACE will work more reliably.Yet yet another remark:I suggest trying the two-script approach stand-alone, with Windows Scripting Host, not in the full MapGuide application.Peter. Edited January 11, 2006 by PeterSwiss Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now