Get the Name of a Window I Just Opened

Started by USMuscle, August 17, 2016, 12:37:13 PM

Previous topic - Next topic

USMuscle

I'd like to open an Explorer window in a specific size and location. Since I can't think of a function that does this, I've been approaching it from the standpoint of opening the window, then using WinPlace, but I can't seem to figure out a way to get that window name. I am familiar with WinItemNameID and those types of functions, but that doesn't seem to help me in this context because I need to know the name of the window I just opened. Does anyone know a way to do this?

Thank you.

td


  • One way is to use the IntControl 31 which is documented in the Consolidated WIL Help file.  You can also view an example of its use in the PopMenu.mnw file which is located in your WinBatch System directory.   The IntControl is used in the implementation of the 'Two Explorers, side by side' PopMenu menu item.

  • You could use the  combination of  RunShell and WinItemProcID.

  • You could use the 'Shell.Application' COM Automation object to obtain a list of Explorer windows and choose the one you want based on object properties.

  • If you want to use ShellExecute to start an instance of an Explore windows, you could specify a folder that the window will display at start.  You can then enumerate windows to find the one with the folder in the window caption.   The actual title may vary a bit with the version of Windows.

Examples for most of the above can be found on the forum or Tech Database.

Here is one:
http://forum.winbatch.com/index.php?topic=1650.msg8148#msg8148
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

td

Neglected to mention that your implementation choice may be influenced by whether or not the system or systems you are running your script on are configured so that each instance of Explorer runs in a separate process or in a separate thread of the same process.  Also, generally windows are enumerated in last to first created order.
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

USMuscle

Quote from: td on August 17, 2016, 01:43:06 PM
Neglected to mention that your implementation choice may be influenced by whether or not the system or systems you are running your script on are configured so that each instance of Explorer runs in a separate process or in a separate thread of the same process.
Good point.
QuoteAlso, generally windows are enumerated in last to first created order.
I guess this means I can look for the highest number.

I've been toying with something like the below. Different system configurations notwithstanding, in regards to your first point above, how reliable do you think something like this would be, as compared to the enumeration option you mentioned above? The one thing I have concerns about is having to wait, as indicated by the TimeDelay after the Run command. I mean, what if someone's computer is slower, as when a computer gets bogged down by some other process? I also normally try to stay away from deductive processes, but in this case, maybe...?

Code (winbatch) Select
localdir=DirHome()

#DefineFunction UDFReformat_WindowsBeforeList(list)
newlist = ""
count = ItemCount( list,"|")
count = count/2
For x = 1 To count By 2
   title= ItemExtract(x,list,"|")
   id= ItemExtract(x+1,list,"|")
   newlist = StrCat(newlist,Title,"|",id,@TAB)
Next
WindowsBefore = StrTrim(newlist)
Return WindowsBefore
#EndFunction
winlist = WinItemNameId( )
WindowsBefore=UDFReformat_WindowsBeforeList(winlist); get list of windows before opening new Explorer window

Run("explorer.exe",localdir)
TimeDelay(1)

#DefineFunction UDFReformat_WindowsAfterList(list)
newlist = ""
count = ItemCount( list,"|")
count = count/2
For x = 1 To count By 2
   title= ItemExtract(x,list,"|")
   id= ItemExtract(x+1,list,"|")
   newlist = StrCat(newlist,Title,"|",id,@TAB)
Next
WindowsAfter = StrTrim(newlist)
Return WindowsAfter
#EndFunction
winlist = WinItemNameId( )
WindowsAfter=UDFReformat_WindowsAfterList(winlist) ;get list of windows after opening new Explorer window

;Compare the windows from the "After" list with those in the "Before" list. The "After" window that is not in the "Before" list must be the Explorer window that was opened
WindowsAfterCount=ItemCount(WindowsAfter,@tab)

For xx=1 to WindowsAfterCount
ThisWindow=ItemExtract(xx,WindowsAfter,@tab)
;Message("",ThisWindow)
If StrIndexNC(WindowsBefore,ThisWindow,1,@fwdscan)==0 Then Break
Next

Position=StrIndex(ThisWindow,"|",1,@fwdscan)
ThisWindowID=StrSub(ThisWindow,Position+1,-1)
WinActivate(ThisWindowID)
Message("New Window",ThisWindow)

USMuscle

I've been looking, but can't seem to find the topics in the Tech Database pertaining to enumerating windows. Can you please tell me what phrase to search for?

fhammer

Check out the post with the title: "Window which was active when script was invoked".