AHK Opening last opened windows explorer window if active, else starting new - autohotkey

This code figures out the current explorer windows open,
I would like to open the first in the list, and if the list is empty open a
new explorer instead.
I hope to the open/activate the either window at the current mouse position
#e::
list := ""
numberOfwindows := ""
wins := ""
WinGet, id, list, ahk_class CabinetWClass ahk_exe explorer.exe
Loop, %id%
{
numberOfwindows := A_Index
this_ID := id%A_Index%
WinGetTitle, title, ahk_id %this_ID%
wins .= A_Index A_Space title ? A_Index A_Space title "`n" : ""
}
MsgBox, number of explorer windows = %numberOfwindows%`n`n%wins%
return

This solves it. - however it can be optimized, anyone have any suggestions ?
#e::
list := ""
numberOfwindows := ""
wins := ""
WinGet, id, list, ahk_class CabinetWClass ahk_exe explorer.exe
Loop, %id%
{
numberOfwindows := A_Index
this_ID := id%A_Index%
WinGetTitle, title, ahk_id %this_ID%
if (A_Index = 1) { ; if it's the first index of the loop
;MsgBox %title%
win = %title% ; store the title in " win "
}
wins .= A_Index A_Space title ?½½ A_Index A_Space title "`n" : ""
}
IfWinNotExist ahk_class CabinetWClass
{
Run C:\Windows\explorer.exe
win := File Explorer
WinWait, %win% ahk_class CabinetWClass
WinMove, mxpos_new , mypos_new
WinActivate
}
;MsgBox, number of explorer windows = %numberOfwindows%`n`n%wins%
; above msgbox displays number and the names of the windows.
;~ ; we now know the win
; and its title, exe and class.
; we want it's current position.
WinGetPos, X, Y, Width, Height,%win% ahk_class CabinetWClass
;MsgBox, %X%, %Y%, %Width%, %Height%
; and we want the mouse position.
CoordMode, Mouse, Screen ; Coordinates are relative to the desktop (entire screen).
MouseGetPos, mxpos , mypos,
;MsgBox, %mxpos%, %mypos%
mxpos_new := mxpos - (Width / 2)
mypos_new := mypos - (Height / 2)
;MsgBox, %mxpos% %mypos% %Width% %Height% %mxpos_new% %mypos_new%
; activate that specific window
WinWait, %win% ahk_class CabinetWClass
WinMove, mxpos_new , mypos_new
WinActivate
return

Related

ControlSend not sending to the non-topmost window

Is it possible to make ControlSend send / click to a background window?
This code finds a window by id / handler, but sends a click / keystroke only if the window is the topmost. ControlSend skips its action if the window is not topmost. ControlClick force brings window to top and then clicks.
#Singleinstance
DetectHiddenWindows, On
#SingleInstance Force
#MaxThreadsPerHotkey, 2
SendMode Input ; Recommended for new scripts
SetControlDelay -1
PostClick(x,y,win="A") {
lParam := x & 0xFFFF | (y & 0xFFFF) << 16
PostMessage, 0x201, , %lParam%, , %win% ;WM_LBUTTONDOWN
PostMessage, 0x202, , %lParam%, , %win% ;WM_LBUTTONUP
}
RunAsAdmin() {
Loop, %0% {
param := %A_Index% ; Fetch the contents of the variable whose name is contained in A_Index.
params .= A_Space . param
}
ShellExecute := A_IsUnicode ? "shell32\ShellExecute":"shell32\ShellExecuteA"
if not A_IsAdmin
{
If A_IsCompiled
DllCall(ShellExecute, uint, 0, str, "RunAs", str, A_ScriptFullPath, str, params , str, A_WorkingDir, int, 1)
Else
DllCall(ShellExecute, uint, 0, str, "RunAs", str, A_AhkPath, str, """" . A_ScriptFullPath . """" . A_Space . params, str, A_WorkingDir, int, 1)
ExitApp
}
}
RunAsAdmin()
F1::
Loop
{
; http://particle-clicker.web.cern.ch/particle-clicker/
WinGet, WinID, ID, Particle Clicker - Google Chrome
; Send keystrokes only if tab active and topmost, suspend if focus lost, continue when gain focue
; ControlSend, ahk_parent, {Space}, ahk_id %WinID%
; send clicks only if tab not mininised, if tab on background - force bring it to front
; ControlClick, x799 y449, ahk_id %WinID%
; same as above
; ControlClick, x799 y449, ahk_id %WinID%,,,,NA
; same as above
; Controlclick x799 y449,ahk_id %WinID%,,Left,1,NA
; same as above
PostClick(799,449,"Particle Clicker - Google Chrome")
sleep 1000
}
return
F12::ExitApp

Autohotkey: Activate foremost of X monitor

How do I activate the foremost window in a given monitor? suppose I have two monitors, one with an editor, and one with different apps, such as chrome and slack. I want to bind a key that will activate the foremost window in monitor two, be it slack or chrome, and one for the editor, for easy manuvering.
The foremost window in a given monitor is the currently active window
or (if the currently active window is on the other monitor) the last active window of this monitor.
; activate the editor:
F1::
WinActivate, ahk_exe notepad.exe ; replace notepad with the ahk_exe of the editor
return
; activate the last active window in the right monitor:
F2::
WinGetPos, X,,,, A
If (X > 1920) ; replace 1920 with the width of the left monitor
return ; do nothing
; otherwise:
WinGet, id, list
Loop, %id%
{
this_ID := id%A_Index%
WinGet, exStyle, exStyle, ahk_id %this_ID%
If !(exStyle & 0x100)
continue
WinGetTitle, title, ahk_id %this_ID%
If (title = "")
continue
WinGetPos, X,,,, ahk_id %this_ID%
If (X > 1920) ; replace 1920 with the width of the left monitor
{
WinActivate, ahk_id %this_ID%
break
}
}
return

AHK key remapping alt tab

My goal is to make my mouse extra button act as alt tab so I could scroll through my tabs without using the keyboard.
XButton1::Alt
LButton::
If (GetKeyState("XButton1", "P"))
Send {TAB down}
LButton Up::
If (GetKeyState("XButton1", "P"))
Send {TAB up}
What I was hoping for this to do was when I am holding XButton1(the back button on my mouse) I could click and it would act as a tab and only while I am holding XButton1 otherwise it my click would act as a click
Try this:
XButton1::Send {XButton1} ; If you remove this line XButton1 loses its original/native function
; Hold down XButton1 and press LButton to navigate the alt-tab menu:
XButton1 & LButton::AltTab ; XButton1 becomes a prefix key
https://autohotkey.com/docs/Hotkeys.htm#combo
https://autohotkey.com/docs/Hotkeys.htm#AltTabDetail
; AltTab-replacement for Windows 8:
XButton1 & LButton::
list = ""
WinGet, id, list
Loop, %id%
{
this_ID := id%A_Index%
WinGet, exStyle, exStyle, ahk_id %this_ID%
If !(exStyle & 0x100)
continue
IfWinActive, ahk_id %this_ID%
continue
WinGetTitle, title, ahk_id %this_ID%
If (title = "")
continue
WinActivate, ahk_id %this_ID%
break
}
return
; AltTabMenu-replacement for Windows 8:
XButton1 & RButton::
list = ""
Menu, windows, Add
Menu, windows, deleteAll
WinGet, id, list
Loop, %id%
{
this_ID := id%A_Index%
WinGet, exStyle, exStyle, ahk_id %this_ID%
If !(exStyle & 0x100)
continue
WinGetTitle, title, ahk_id %this_ID%
If (title = "")
continue
WinGetClass, class, ahk_id %this_ID%
If (class = "")
continue
If (class = "ApplicationFrameWindow")
continue
Menu, windows, Add, %title%%A_Tab%ahk_class %class%, ActivateWindow
WinGet, Path, ProcessPath, ahk_id %this_ID%
Menu, windows, Icon, %title%%A_Tab%ahk_class %class%, %Path%,, 0
}
Menu, windows, Show
return
ActivateWindow:
WinActivate, %A_ThisMenuItem%
return
XButton1::Send {XButton1}

Autohotkey and Windows 10: How to get current explorer path

I use autohotkey version: 1.0.48.05 (because I stick with activeaid).
The script to read the current path is as follows (and worked until Win 7).
; Get full path from open Explorer window
WinGetText, FullPath, A
; Clean up result
StringReplace, FullPath, FullPath, `r, , all
FullPath := RegExReplace(FullPath, "^.*`nAddress: ([^`n]+)`n.*$", "$1")
How I suspect that while switching to Win10 it seems that I also switched the language.
If I MsgBox out the %FullPath% before cleaning with
WinGetText, FullPath, A
MsgBox %FullPath%
I see amongst other strings (obvoíously separated by CR):
Adresse: V:\Vertrieb\Prospects\MyFile
so how do I need to adjust the regexp to extract that very string!
Best regards
Hannes
#IfWinActive, ahk_class CabinetWClass ; explorer
F1:: MsgBox, % GetActiveExplorerPath()
; or
F2::
ActiveExplorerPath := GetActiveExplorerPath()
MsgBox, % ActiveExplorerPath
return
#IfWinActive
GetActiveExplorerPath() {
; https://www.autohotkey.com/boards/viewtopic.php?f=6&t=69925
explorerHwnd := WinActive("ahk_class CabinetWClass")
if (explorerHwnd)
{
for window in ComObjCreate("Shell.Application").Windows
{
if (window.hwnd==explorerHwnd)
return window.Document.Folder.Self.Path
}
}
}
Try:
f1::MsgBox % Explorer_GetSelection()
Explorer_GetSelection(hwnd="") {
WinGet, process, processName, % "ahk_id" hwnd := hwnd? hwnd:WinExist("A")
WinGetClass class, ahk_id %hwnd%
if (process = "explorer.exe")
if (class ~= "(Cabinet|Explore)WClass") {
for window in ComObjCreate("Shell.Application").Windows
if (window.hwnd==hwnd)
path := window.Document.FocusedItem.path
SplitPath, path,,dir
}
return dir
}
It takes me so much time to find the best solution (for me).
Maybe it will work for you as well.
ControlClick, ToolbarWindow323, A
ControlGetText, path, Edit1, A
Msgbox, %path%

AutoHotKey: Merge scripts that monitor selection in Firefox and other programs

I am struggling with a script that monitors selections by mouse in FireFox, Adobe Acrobat and one more program and then copies this selection to clipboard and changes it according to a regex. For each program another regex is needed. Each script works as a separate program, but when I merge them, the copied text is not changed according to my regex.
Script for Adobe Acrobat:
#ifWinActive ahk_class AcrobatSDIWindow
~LButton::
now := A_TickCount
while GetKeyState("LButton", "P")
continue
if (A_TickCount-now > 600 )
{
Send ^c
copied := true
}
return
OnClipboardChange:
if !copied
return
copied := false
ToolTip % Clipboard := RegExReplace(Clipboard, "\r\n", " ")
SetTimer, ToolTipOff, -1000
return
ToolTipOff:
ToolTip
return
And stript for Firefox:
#ifWinActive ahk_class MozillaWindowClass
~LButton::
now := A_TickCount
while GetKeyState("LButton", "P")
continue
if (A_TickCount-now > 600 )
{
Send ^c
copied := true
}
return
OnClipboardChange2:
if !copied
return
copied := false
ToolTip % Clipboard := RegExReplace(Clipboard, "[0-9]\.\s*|\s?\([^)]*\)|\.")
SetTimer, ToolTipOff1, -1000
return
ToolTipOff1:
ToolTip
return
The #If does only work on hotkeys, not on labels. Using OnClipboardChange seems unnecessary. When you press ctrl+c you already know that the clipboard changed.
I also really recommend setting indentations for hotkeys and also #If statements.
Here is how I would do it:
#If WinActive("ahk_class AcrobatSDIWindow") || WinActive("ahk_class MozillaWindowClass")
~LButton::
now := A_TickCount
while GetKeyState("LButton", "P")
continue
if (A_TickCount-now > 600 )
{
Send ^c
if WinActive("ahk_class AcrobatSDIWindow")
{
regex := "\r\n"
replace := " "
}
else if WinActive("ahk_class MozillaWindowClass")
{
regex := "[0-9]\.\s*|\s?\([^)]*\)|\."
replace := ""
}
ToolTip % Clipboard := RegExReplace(Clipboard, regex, replace)
SetTimer, ToolTipOff, -1000
}
return
#If
ToolTipOff:
ToolTip
return
(untested)
edit:
.....
~Left::
~Right::
~Up::
~Down::
now := A_TickCount
while GetKeyState("Shift", "P")
continue
if (A_TickCount-now > 600 )
{
oldShiftState := GetKeyState("Shift", "P")
Send, {Shift Up}
Send ^c
If (oldShiftState)
Send, {Shift Down}
.....
(untested)