Can an already running program's window be hidden? - windows

I have been using wsh to run vbscripts in conjunction with iMacros at my work for going on 3 years now. I know that the iMacros browser itself has an option to run hidden but the web-based db interface I have to work with does not function in the iMacros browser. For this reason I have had to use Internet Explorer. I have no problems with the script itself or how it runs. The thing I would like to improve is how the Internet Explorer window itself is handled.
Currently, when iMacros is initiated it creates the IE window in a non-maximized state cascaded from where the previous window was created. Because of iMacros's behavior, I then resize the window using an iMacros script so that enough of the page is rendered for the script to see everything it needs.
Set iim1 = CreateObject ("imacros")
iret = iim1.iimOpen ("-ie -iePrivate", TRUE, 300)
iret = iim1.iimPlayCode("SIZE X=" & scrWidth & " Y=" & scrHeight)
This all works fine and dandy. The scripts are scheduled to run at specific times, gather information out of the db and then imports that data into Excel spreadsheets and prints them out.
What I would like to do is make the IE window hidden while these scripts run. Because I do not create the IE window first (Set objIE = CreateObject("internetexplorer.Application")) I do not have access to the IE window object. I let iMacros create the window with the -iePrivate flag so that it will not disturb my own IE window if I should be logged in and working in the db while the script executes. However, it does try to take focus and become the active application while the script executes. Which can be very annoying at time. My goal is to be able to share these scripts with my co-workers but I don't want the IE windows popping up on them while they may be working on something else.
Is there a way to get to the IE object created by iMacros so that the window can be hidden?

This does Internet Explorer and Explorer windows (they used to be the same program).
Set objShell = CreateObject("Shell.Application")
Set AllWindows = objShell.Windows
For Each window in AllWindows
msgbox window.locationname
Next

Related

RStudio lost focus when start up

When I start up RStudio on Windows, it lost focus. I have to click on it before I could input anything to the console or script window. I know that Ctrl-1 to focus on the Script Window, and Ctrl-2 to focus on the console window; however, both of these two shortcuts will work as long as I click on RStudio first.
Is there anything I need to set in RStudio?
Software Environment:
RStudio Version 0.99.446
Windows 7 Ultimate 64-bit
As a kludge, you can write a VBScript which first opens RStudio and then switches the focus to it:
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.CurrentDirectory = "C:\Program Files\RStudio"
WshShell.Run """C:\Program Files\RStudio\bin\rstudio.exe"""
WScript.Sleep 200 'milliseconds
focused = WshShell.AppActivate("RStudio")
i = 1
Do While Not focused And i < 10
WScript.Sleep 100
focused = WshShell.AppActivate("RStudio")
i = i + 1
Loop
Save this to the desktop in a file with a name like ToRStudio.vbs. Then, when you click on the icon, it opens up Rstudio and then tries to repeatedly switch focus to the RStudio window. In my original version of the code, I relied on a fixed time delay before issuing a single call to AppActivate. But, I discovered that when my workspace saved from my last session was large then my VBScript would finish executing before RStudio was ready. With the above loop, it repeatedly tries to grab focus before timing out. Since I put in the loop, it has had a 100% success rate in opening RStudio with the proper focus. In some debugging code I removed, the variable i never once got above 3 in 10 consecutive attempts.
Obviously, you might have to change the path names, and tweak the timings.
In my comment above I said that certain things about how RStudio handled focus struck me as counterintuitive. Since then I discovered RStudio's option to automatically switch focus to console on sourcing. The lack of doing this by default is what I had in mind. The issue in this question was the only remaining focus issue for me. I'm somewhat used to it by now, so I'm not sure if I'll actually use this VBS script.

Minimize windows, application startup

Is there a way to minimize all windows automatically when launching an application?
I tried to call a .vbs file with the content above:
Set shell = wscript.CreateObject("Shell.Application")
Shell.MinimizeAll
But doing this, application is minimized too.
I'm not sure if this minimizes the windows, but it will probably satisfy your requirement:
set objShell = CreateObject("shell.application")
objShell.ToggleDesktop
This is equivalent to clicking the show desktop button.
Or as just extending your code:
shell.ToggleDesktop
Edit: Nevermind... this does the same thing as your code above. But why not just run this and then display your window? For instance, if it is your app... call the vbs and then display the window. Or if you have no control over the app, call it from a .bat file and run this vbs then your program.
What you want to do is first minimize all applications or toggle the desktop; THEN (perhaps even after a delay) open your application.

Switching Internet Explorer Windows with VBA

I have been working on an Excel macro to automate an Internet Explorer process at work. I'm incredibly close to figuring everything out, but at the end, the macro clicks on a "Download Report" button, which launches a new window. I need to initiate a few things on this new window but I cannot figure out how to change the focus of the macro to the new window. I have found a few things online, but they have been unsuccessful. I know there are ways to toggle between browser windows by telling Excel to look for the name of the browser window, and to activate it, but parts of the name of the new window are constantly changing (it's a unique URL for that particular window).
I was wondering if there might be a way to have the Macro look for just the first Nine letters of the window and switch to that window? Similar to the LEFT function in excel? The reason being is that the first 9 characters of the URL never change. The name of the original window (the one I'm trying to switch from) never changes, so maybe I could tell Excel to look for the the Browser Window that is NOT named the original one? Or any other ideas?
Thanks in advance for any help
Is the VBA AppActivate command what you are looking for?
You will need to add the references to the Internet Controls and shell. You just need to loop through the IE windows and look at the left hand side of the url. See below
Function GetInternetWindow(urlToLookFor as String) As InternetExplorer
Dim winShell As Shell
Dim ie As InternetExplorer
Set winShell = New Shell
'loop through the windows and look at the urls
For Each ie In winShell.Windows
If Left(ie.LocationURL, 9) = urlToLookFor Then
GetInternetWindow = ie
Exit For
End If
Next
End Function

How to set focus on file dialog opened in Vbscript

Our Team is automating tests/test data preparation in QTP and we do the scripting in VBScript.
In several tests the tester who runs the script need to supply an MS-Excel file with the indata. We use UserAccounts.CommonDialog for this and it works great. Except for one litle problem, when we run this from QTP the file dialog does not get focus. It's opened in the background and it's easy for the tester that runs the script to miss it and waste time waiting for the dialog.
How do we give the file dialog focus?
Code Example:
Set ObjFSO = CreateObject("UserAccounts.CommonDialog")
ObjFSO.Filter = "MS-Excel Spreadsheets|*.xls|All Files|*.*"
while ObjFSO.ShowOpen = false
msgbox "Script Error: Please select a file!"
wend
msgbox "You selected the file: " & ObjFSO.FileName
My guess is that since the dialog is modal, the ShowOpen method doesn't return the execution control back to the script until the dialog is closed. So there's no way to interact with the dialog as part of your test script.
As a workaround, you could spawn a parallel script that would wait for the dialog and activate it. But I guess QTP cannot run two scripts in parallel, so you'll probably need an external shell script (written in VBScript / JScript / PowerShell / etc).
Edit: Try the following:
Create an external VBScript file (.vbs) with the following contents:
Set oShell = CreateObject("WScript.Shell")
While Not oShell.AppActivate("Open")
WScript.Sleep 500
Wend
This script calls WshShell.AppActivate to activate a window whose title contains Open (replace it with the actual dialog title). If there's no such widnow at the monent, it retries the attempt after 0.5 sec (you can increase the delay if you wish).
Launch this script from your QTP test before opening the dialog. (Use SystemUtil.Run or something like this.)
I'm not sure, but I think this should do the trick.
Did you try recording a click on the dialog - so that QTP will click on it to set focus before proceeding?

Using WSH (VBS) with iMacros - how do they do it?

(iMacros For Firefox 6.6.5.0; Firefox 3.6.3; Windows XP Pro SP3 w/all updates)
I made an iMacro to select "load next 25" (comments) on a web page (CNN.COM). Unfortunately, iMacros doesn't appear to do looping (do the above until that string doesn't appear on the page anymore - i.e. all the comments are loaded). I tried putting {!iloop} in the TAG command, and it didn't work - then I read it wouldn't.
So I tried the example at http://wiki.imacros.net/Loop_after_Query_or_Login
I can't find any information on how to actually run the script in the above example. I searched Google and found VBS scripting is handled with .wsh files with Windows XP Pro. (The examples and other references there say Windows does VBS natively, so I looked up how with Google.)
So I made the following .wsh file (modified the above example):
Option Explicit
Dim iim1, iret
'initialize iMacros instance
set iim1 = CreateObject ("imacros")
iret = iim1.iimInit()
do while not iret < 0
iret = iim1.iimPlay("Load All CNN Comments")
loop
' tell user we're done
msgbox "End."
' exit iMacros instance and quit script
iret = iim1.iimExit()
Wscript.Quit()
Here's the iMacro: (Load All CNN Comments.iim)
VERSION BUILD=6650406 RECORDER=FX
TAG POS=1 TYPE=A ATTR=TXT:Load<SP>next<SP>25
WAIT SECONDS=#DOWNLOADCOMPLETE#
The iMacro works by itself - I press Play (left iMacro panel) and the next 25 comments load on the CNN.com page in the current tab.
I put the .wsh file in the ...\iMacros\Macros directory - with the iMacro "Load All CNN Comments.iim"
When I run the .wsh file (by just double clicking on it's icon - I created it with Notepad, and Windows gave it an icon for that file type - it's executable) I get the message from "Windows Script Host" - "There is no script file specified." I wasn't actually expecting it to work, as I don't see how Windows would know to call iMacros to run the iim macro.
It would be nice if there was a simple, COMPLETE, example of how to use a VBS script with iMacros, that isn't bogged down with unnecessary complication like filling in a form, loading multiple pages, etc. I can't find ANY example.
So what do I need to do to get this to work? I just installed iMacros yesterday, because I am constantly having the problem that there are hundred of comments after a CNN.com article, and loading 25 more at a time until they are all on the page makes it impractical to read any replies to my comments.
It would also be nice if I could run the Macro from Firefox, rather than by double clicking on some file somewhere.
Thanks for any help.
Try changing the extension of your script from .WSH to .VBS.

Resources