How to get and set Windows Explorer view settings - windows-7

I'm trying to make a script cycle views in Windows Explorer (Windows 7 libraries don't allow remembering view settings per folder).
I found postMessage with the WM_COMMAND message (code 0x111), but can't seem to use it to affect the Explorer view. Nothing happens when I send:
PostMessage,0x111,0x702c,0,,ahk_id %parent%
where %parent% is the handle of the window. Examples on the forums are for Windows XP, which seems to work differently. How to get and set the view setting?

You can try the following. It starts a persistent script that runs a timer subroutine looking for Save-As type windows, and when it encounters one, it sends that found window certain key strokes. Note, the keys to send WILL DEFINITELY DEPEND on which Win version you use -- and I had trouble with the standard Win 7 shortcuts and hence used a kludgey work-around. It should be clear from the comments in the code what I am doing, and this should get you on your way to doing what you need.
#Persistent
SetTitleMatchMode, 2 ; Matches all titles with the designated words in it (picks the top most)
SetTimer, MaxAll, 150 ; time in ms
return
MaxAll:
IfWinActive, Save MoviePlus File As ; runs only on "Save MoviePlus File As"
DoIt("Movie")
IfWinActive, Save ; runs on "Save" "Save As" "Save File" etc.
DoIt("Save")
IfWinExist, Open ; runs on "Open" "Open File" "File Open" etc.
DoIt("Open")
IfWinExist, Import ; runs on "Import" "File Import" "Import Commands" etc.
DoIt("Import")
return
DoIt(Type)
{
SetTimer, MaxAll, Off ; turn of timer
sleep, 250
WinMaximize ; maximize the window (or comment out)
sleep, 250
Send, !n ; start at the Filename textbox
sleep, 250
Send, +{tab} ; SHIFT+TAB to move to files pane
sleep, 250
; Send, ^+5 ; CTRL+SHIFT+5: Win8.1 to go to "List View"
; Send, ^!5 ; CTRL+ALT+5: Win8 to go to "List View"
; Send, {LAlt}vl ; LEFTALT+V+L: Win7 to go to "List View" - but doesn't work consistently
SendEvent, {F3}{tab}{right 2}{down}{end}{up 3}{enter} ; Navigate to "View" drop-down-list starting from from search bar
sleep, 250
IfEqual, Type, Open ; If the dialog was a File Open
{
Send, !p ; ALT+P: Toggles preview pane
sleep, 250
}
IfEqual, Type, Movie ; If the dialog was for MoviePlus
{
Send, ^!p ; Ctrl+ALT+P: Toggles preview pane? Google the keyboard shortcuts (I didn't check)
sleep, 250
}
Send, !n ; back to Filename textbox
WinWaitClose ; wait to close the dialog
SetTimer, MaxAll, On ; turn timer back on
return
}
+esc::ExitApp ; Shift+Esc ends script
HTH,

Found a UDF for AutoIt that works for me. It's called automating windows explorer. The code below I wrote based on the forum example, it shows a working example of getting the view and changing it by incrementing the existing state.
Since it is for Windows 7 I skip the thumbnail and thumbstrip views--I think those are for Vista. The content view is not supported by Vista either.
I googled the Windows Constants names and values. I found the correct view sizes by experimenting/looking at my own results.
#include "Includes\AutomatingWindowsExplorer.au3"
;Icon sizes are standard: 16, 48, 96, 196
;Details & list: 16
;Tiles: 48
;Content: 32 (!)
;~ FVM_ICON = 1, (48, 96, 196)
;~ FVM_SMALLICON = 2, (16)
;~ FVM_LIST = 3,
;~ FVM_DETAILS = 4,
;~ FVM_THUMBNAIL = 5, (seems to be same as ICON in win7)
;~ FVM_TILE = 6,
;~ FVM_THUMBSTRIP = 7, (seems to be same as ICON in win7)
;~ FVM_CONTENT = 8,
Opt( "MustDeclareVars", 1 )
Example()
Func Example()
; Windows Explorer on Vista, 7, 8
Local $hExplorer = WinGetHandle( "[REGEXPCLASS:^(Cabinet|Explore)WClass$]" )
If Not $hExplorer Then
MsgBox( 0, "Automating Windows Explorer", "Could not find Windows Explorer. Terminating." )
Return
EndIf
; Get an IShellBrowser interface
GetIShellBrowser( $hExplorer )
If Not IsObj( $oIShellBrowser ) Then
MsgBox( 0, "Automating Windows Explorer", "Could not get an IShellBrowser interface. Terminating." )
Return
EndIf
; Get other interfaces
GetShellInterfaces()
; Get current icon view
Local $view = GetIconView() ;returns array [view,size]
; Determine the new view
Local $iView, $iSize, $iNewView, $iNewSize
$iView = $view[0] ; Icon view
$iSize = $view[1] ; Icon size
If $iView = 8 Then
$iNewView = 1
$iNewSize = 48
Else
$iNewView = $iView + 1
If $iNewView = 5 Or 7 Then
$iNewView += 1 ;skip from 5 to 6, or from 7 to 8
EndIf
EndIf
Switch $iNewView
Case 2 To 4
$iNewSize = 16
Case 6
$iNewSize = 48
Case 8
$iNewSize = 32
EndSwitch
;MsgBox( 0, "NewView", $iNewView )
SetIconView( $iNewView, $iNewSize ) ; Set details view
Sleep( 1000 ) ; Wait
SetIconView( $iView, $iSize ) ; Restore old view
EndFunc

Related

AutoIT Close all windows except shutdown

I'm building a program that shuts down all open windows after 60 seconds of no mouse movement using AutoIT and a code I found here:
https://www.autoitscript.com/forum/topic/97638-closing-all-open-windows/
I edited it a little such that it looks like this:
#include <MsgBoxConstants.au3>
Opt("WinTitleMatchMode", 3) ;3 = exact title match
Opt("TrayIconDebug", 1)
$S_running = "check-4-app2" ;name the script
If WinExists($S_running) Then Exit
AutoItWinSetTitle($S_running)
$count = 0
$mousePos = MouseGetPos()
while 1
$count += 1
$mousePosNow = mouseGetPos()
If $mousePosNow[0] <> $mousePos[0] Or $mousePosNow[1] <> $mousePos[1] Then $count = 0
$mousePos = MouseGetPos()
;ToolTip("count = " & $count, 0, 0)
If $count > 60 Then
$count = 0
$var = WinList ()
$length = UBound($var) - 2
For $i = 1 to $length
If BitAnd (WinGetState ($var[$i][1]), 2) And $var[$i][0] <> "" AND $var[$i][0] <> "Program Manager" Then WinClose ($var[$i][1], "")
Next
EndIf
Sleep(1000)
WEnd
It does actually close all open windows after 60 seconds but it opens the windows shutdown window as well (like when you press the alt+f4 combination) in windows 7.
How to stop this window from popping up? I just like all windows to close and have a clean desktop view.
First you should do some debug for all values in $var=WinList() to see if some window in your OS is special ,that means, if such window is shutdown, it will trigger windows shutdown, if problem still exists, I found a walkround way , I tried in my computer to do following code
WinClose("Close Windows")
; or maybe WinClose("Shutdown Windows")
to close what you said "windows shutdown window",maybe you can put this line at the bottom of your code
attention: in my computer, its OS language is not english, so maybe you should first trigger "windows shutdown window" and use AutoIt window info tool to see its window title precisely

AutoIt Wait until browser window is visible not working as expected

Consider that I have the handle ($browser_handle) of a web browser window ('firefox') available in my AutoIt script.
I would like perform some keystrokes after I know that the firefox browser window is open and visible on my display so that I can bring it to focus using -
WinActivate($browser_handle)
or
WinWaitActive($browser_handle)
To make sure that the window is visible before I try to bring it into focus I have a while loop which waits till the state of the window handle is visible (2).
While (Not BitAND(WinGetState($browser_handle), 2)) WEnd //until window visible
If I use a Sleep(5000) function before the while loop then I do not face any issues.
If I do not use an arbitrary Sleep function in my script, the Whileloop condition never becomes true and turns into an infinite loop.
When I tried to check what the return value of WinGetState($browser_handle) is when there is no Sleep function,
It remains 5 even if the browser is visible and becomes 0 after the browser window is closed.
I'm unable to understand why the WinGetStatereturn value never becomes 2(visible) even if the browser is visible when there is no Sleep function.
This is a test code which can reproduce the issue -
#include <Constants.au3>
Local $browser_name = 'C:\Program Files (x86)\Internet Explorer\iexplore.exe'
Run($browser_name)
ProcessWait('iexplore.exe')
Local $browsers = ProcessList('iexplore.exe')
Local $pid = $browsers[1][1];
_WinActiveByPID($pid)
Local $sText = WinGetTitle("[ACTIVE]")
; Display the window title.
MsgBox($MB_SYSTEMMODAL, "", $sText)
Func _WinActiveByPID($pid) ;False to WinActivate, True to just see if it's active
Local $aWL = WinList()
For $iCC = 1 To $aWL[0][0]
If ($aWL[$iCC][0] <> '') And _
(WinGetProcess($aWL[$iCC][1]) = $pid) Then
While (Not BitAND(WinGetState($aWL[$iCC][1]), 2))
MsgBox($MB_SYSTEMMODAL, "", WinGetState($aWL[$iCC][1]))
WEnd
WinWait($aWL[$iCC][1])
WinActivate($aWL[$iCC][1])
WinWaitActive($aWL[$iCC][1])
Return 1
EndIf
Next
Return SetError(2, 0, 0)
EndFunc
Note:
WinWait does not work in this situation, If you see the example in the link there is a Sleep after WinWait, this function returns even if the window is hidden.
The browser window in my original script is not launched using Run, it is a Java program which opens the browser. I know that the Run method returns the PID of the browser which I can use to resolve this issue but since it starts from Java I have to use ProcessList to obtain the PID of the browser.
Please let me know how I can get this to work.
Thanks
You got thing too complicated for no reason there. Let me show you.
Local $browser_name = 'C:\Program Files (x86)\Internet Explorer\iexplore.exe'
Run($browser_name)
ProcessWait('iexplore.exe')
Sleep(500) ; because ProcessWait will exit when the process is created not when the window is visible. If your computer is slow you will have an problem
Local $BrowsersHWND = WinList()
Local $hBrowserHandle[2]
For $i = 0 To $BrowsersHWND[0][0] -1
If StringInStr($BrowsersHWND[$i][0], "Internet Explorer") Then
$hBrowserHandle[0] = $BrowsersHWND[$i][0] ;title
$hBrowserHandle[1] = $BrowsersHWND[$i][1] ;handle
ExitLoop
EndIf
Next
If $hBrowserHandle[0] = "" Or $hBrowserHandle[1] = "" Then Exit MsgBox(0, 0, "Internet Explorer not found")
Local $hState = WinGetState($hBrowserHandle[0])
Local $sVisibleStates[24] = [2, 3, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 34, 35, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47] ;visible states
Local $hCheck = False
For $j = 0 To UBound($sVisibleStates) -1
If $hState = $sVisibleStates[$j] Then
ConsoleWrite("Window state(visisble): " & $hState & #LF)
$hCheck = True
ExitLoop
EndIf
Next
If $hCheck = False Then
ConsoleWrite("Activating: " & $hBrowserHandle[0] & #LF)
WinActivate($hBrowserHandle[0])
EndIf
I have added all the cases of states where your window would be visible.
WinWait will not activate any window and I recommend you never use it. It could freeze a script for ever without reason. I will not explain why because it would take pages but avoid it. Also avoid using WinWaitActive. For similar reasons.
You have too many issues in your code that's why I completely changed it. Not worth explaining it.
I think the problem is not necessarily with the code, but possibly with the logic. I could be way off base here, but maybe we should not be looking for a state of 2, but something closer to 15.
A sample with notepad:
$WIN_STATE_EXISTS (1) = Window exists
$WIN_STATE_VISIBLE (2) = Window is visible
$WIN_STATE_ENABLED (4) = Window is enabled
$WIN_STATE_ACTIVE (8) = Window is active
A window that is active (8), enabled (4), visible(2), and exists(1) will have a windows state of 15.
; Look for notepad and display WindowState number
if WinExists("[Class:Notepad]") Then
MsgBox($MB_SYSTEMMODAL, "", "Found")
WinActivate("[Class:Notepad]")
$WinState = WinGetState("[Class:Notepad]")
MsgBox($MB_SYSTEMMODAL, "", $WinState)
EndIf

Cancel an existing key remap in autohotkey

I have a AutoHotKey script that asks me if I want to remap my Win keys to Ctrl or cancel their remapping, thus making them Win keys again.
However I cannot find a way to cancel the remap. If I use the command LWin::Lwin I get the error message that there is a "duplicate key".
I'm new to AutoHotKey, but I did search first, so please don't bite my head off is this is a stupid question. (It is a Lenovo laptop with Windows7-64).
Here's the script:
MsgBox, 4, , Remap CTRL for Desktop Keyboard?
IfMsgBox, Yes
LWin::LCtrl
RWin::RCtrl
return
; Otherwise, the user picked No
; LWin::LWin
; RWin::RWin
; return
Various ways.
Create a hotkey to close ahk, e.g. ^!x::ExitApp = [Ctrl]+[Alt]+[x]
Create a hotkey to disable/enable all hotkeys e.g. f12::suspend
Create hotkeys that ONLY work in a specific appliaction.
Here are all suggestions combined.
Under normal circumstances: LWin::LCtrl and RWin::RCtrl are active, Unless you pressed F12. You can in AHK_L set variables that can be used in #If (Var = 1), where you can define Hotkeys that only work when that variable is set to 1 (true).
SetTitleMatchMode, 2 ; Allow the use of a portion of the wintitle
F12::
Suspend
If A_IsSuspended
TrayTip, HotKeys, Off, 3, 0
Else
TrayTip, HotKeys, On, 3, 0
Return
^!x::ExitApp
LWin::LCtrl
RWin::RCtrl
F1::MsgBox, Normal Mode
#IfWinActive, Window title
F1::MsgBox, Window X is active
F2::MsgBox, You pressed F2 inside Window x
#IfWinActive
Toggle := False
F10::Toggle := !Toggle ; Turns Mouse button ON|Off
#if Toggle ; ONLY worls in AHK_L
LButton::Return ; Disables Mouse button
#if
Here's a version you can drive from the command line:
; Allow the script to be reloaded multiple times
#SingleInstance force
; Check the command line for input
NumberOfParameters = %0%
; If any command line param was passed then just unload the mappings
If ( NumberOfParameters > 0 )
{
MsgBox Command line parameter was passed, unloading...
ExitApp
}
Else
{
; Let's ask the user what they want to do
MsgBox, 4, , Remap CTRL for Desktop Keyboard?
IfMsgBox, Yes
{
; If yes, then remap
MsgBox Keys have been mapped.
}
Else
{
; If no, then unload
MsgBox Unloading mapping.
ExitApp
}
}
; Keys will be mapped so long as the script remains resident
LWin::LCtrl
RWin::RCtrl

Focus-follows-mouse conflicts with Resharper navigation

In Windows 7, you can give focus to a window just by hovering over it with the mouse. This feature is not enabled by default, but you can enable it in the Control Panel. (Here is the path to take:
[Ease of Access Center-->Make the mouse easier to use-->check "Activate a window by hovering over it with the mouse"]).
I like this feature a lot, but sometimes it annoys me when I try to open a C# class in Visual Studio using Resharper. I'll hit CTRL+N and type the name of the class I want to see (for example, "MyWpfClass"). Resharper will then show a dropdown of suggestions with "MyWpfClass" on top. I hit return, and now Resharper opens a dropdown which lets me choose between "MyWpfClass.xaml" and "MyWpfClass.xaml.cs". However, if the mouse cursor is in the wrong place, the dropdown closes within a second and I'm back to square one. Is there a way to fix this without turning the focus-follows-mouse feature off?
I was having the same problem with MS Outlook: the auto-suggestions list for contacts would close automatically because Windows treated it as a window rather than part of the New Message window.
You could use NiftyWindows, which has the same option "Focus Follows Mouse", accessible through its context menu.
Alternatively, as it is written in Autohotkey, you could use extract and run its subroutine "XWN_FocusHandler" into a standalone script:
#Persistent
#SingleInstance force
#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
#Warn All, OutputDebug ; Recommended for catching common errors.
SendMode Input ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir% ; Ensures a consistent starting directory.
SetTimer, XWN_FocusHandler, 100
return
XWN_FocusHandler:
CoordMode, Mouse, Screen
MouseGetPos, XWN_MouseX, XWN_MouseY, XWN_WinID
If ( !XWN_WinID )
Return
If ( (XWN_MouseX != XWN_MouseOldX) or (XWN_MouseY != XWN_MouseOldY) )
{
IfWinNotActive, ahk_id %XWN_WinID%
XWN_FocusRequest = 1
Else
XWN_FocusRequest = 0
XWN_MouseOldX := XWN_MouseX
XWN_MouseOldY := XWN_MouseY
XWN_MouseMovedTickCount := A_TickCount
}
Else
If ( XWN_FocusRequest and (A_TickCount - XWN_MouseMovedTickCount > 500) )
{
WinGetClass, XWN_WinClass, ahk_id %XWN_WinID%
If ( XWN_WinClass = "Progman" )
Return
; checks wheter the selected window is a popup menu
; (WS_POPUP) and !(WS_DLGFRAME | WS_SYSMENU | WS_THICKFRAME)
WinGet, XWN_WinStyle, Style, ahk_id %XWN_WinID%
If ( (XWN_WinStyle & 0x80000000) and !(XWN_WinStyle & 0x4C0000) )
Return
IfWinNotActive, ahk_id %XWN_WinID%
WinActivate, ahk_id %XWN_WinID%
XWN_FocusRequest = 0
}
Return

Assign hotkeys to windows (and remember size & location). AHK

I have found the following script for dynamically assigning hotkeys to already open windows:
Code (Expand):
Loop 10
{
i := A_Index - 1
HotKey #^%i%,DynHotkey
HotKey #%i%, DynHotkey
HotKey #!%i%,DynHotkey
}
Exit
DynHotkey:
StringRight i, A_ThisHotKey, 1
StringMid what,A_ThisHotKey, 2, 1
var := var%i%
IfEqual what, ^, WinGet var%i%, ID, A ; Save ID
Else IfEqual what,!, WinMinimizeAll ; MinimizeAll
WinRestore ahk_id %var%
WinActivate ahk_id %var% ; Switch
Return
(the code was copied from this thread http://www.autohotkey.com/forum/topic38773.html&highlight=dynamic+hot+key)
With the above script you can:
Use Win+Ctrl+0..9 to attach hotkey to current active window.
Use Win+0..9 to switch to correspoding window.
However, if I assign a hotkey to a given window (using Win+Ctrl+0..9), and then I want I want to go back to that window (Win+0..9), the window is reset to a new size & location.
Is there way of saving the size & location of the window along with it's ID?
If so, what would the script look like?
I am running the above script on Windows 7 64-bit.
Thanks a lot,
You dont need to complicate the code :)
Quick question: if your window is minimized you dont have any problems right?
The "problem" on the code is the WinRestore.
The thing is that if the window is not minimized and then you do a WinRestore it will change the size and position to the "not maximized" version of it.
WinActivate automatically does a WinRestore only if the window is minimized, so you can safely remove line 16 (the WinRestore one) since WinActivate will do what you need.
--edit--
this is how the code should look:
Loop 10
{
i := A_Index - 1
HotKey #^%i%,DynHotkey
HotKey #%i%, DynHotkey
HotKey #!%i%,DynHotkey
}
Exit
DynHotkey:
StringRight i, A_ThisHotKey, 1
StringMid what,A_ThisHotKey, 2, 1
var := var%i%
IfEqual what, ^, WinGet var%i%, ID, A ; Save ID
Else IfEqual what,!, WinMinimizeAll ; MinimizeAll
WinActivate ahk_id %var% ; Switch
Return
I tested it, it works perfectly.
You can use WinGetPos to read the actual position and save it. Then you can use WinMove to set the position. Here is a function list: http://www.autohotkey.com/docs/commands.htm.

Resources