Snap Window to edges everytime different program is launched

Get help with using AutoHotkey and its commands and hotkeys
kbpman
Posts: 1
Joined: 08 May 2020, 12:56

Snap Window to edges everytime different program is launched

08 May 2020, 13:13

Hi there,

I am quite new to AHK (no IT background) and am trying to create the following automation:
Every time certain programs are launched, snap it either to the right or left edge.
E.g. when I launch Spotify -> snap to right edge, when I launch Explorer -> snap to left edge.

First, I created the following script:

Code: Select all

WinWait, ahk_exe Spotify.exe
Send #{Right}
WinWaitClose
This works, but only once. This is why I added a loop and #Persistent around it.
Then it works great, and every time I launch Spotify, it automatically snaps to the right edge.
Now, I probably could create another script for Explorer. But I guess this is not the purpose of it.

I tried the following script and it works but only if all the other programs are closed first.

Code: Select all

#Persistent

loop
{
	WinWait, ahk_exe Spotify.exe
	Send #{Right}
	WinWaitClose

	WinWait, ahk_exe Explorer.exe
	Send #{Left}
	WinWaitClose
}
I also tried Groupadd with different groups for left and right edge but couldn't get it to work.

Can anyone help?
Thanks a lot in advance!
GEV
Posts: 844
Joined: 25 Feb 2014, 00:50

Re: Snap Window to edges everytime different program is launched

08 May 2020, 17:45

https://autohotkey.com/board/topic/80644-how-to-hook-on-to-shell-to-receive-its-messages/

Code: Select all

#Persistent
SetBatchLines, -1
Process, Priority,, High

Gui +LastFound
hWnd := WinExist()

DllCall( "RegisterShellHookWindow", UInt,hWnd )
MsgNum := DllCall( "RegisterWindowMessage", Str,"SHELLHOOK" )
OnMessage( MsgNum, "ShellMessage" )
return

ShellMessage( wParam,lParam )
{
	If ( wParam = 1 ) ;  1 means HSHELL_WINDOWCREATED
	{
		WinGet, pname, ProcessName, ahk_id %lParam% 
		WinGetTitle, title, ahk_id %lParam%
		WinGetClass, class, ahk_id %lParam%
		If (title != "")
		{
			If (pname = "Spotify.exe")
				WinMove, ahk_id %lParam%,, 0, 0, (A_ScreenWidth/2), A_ScreenHeight
			If (class = "CabinetWClass")
				WinMove, ahk_id %lParam%,, (A_ScreenWidth/2), 0, (A_ScreenWidth/2), A_ScreenHeight
		}
	}
}

Return to “Ask For Help”

Who is online

Users browsing this forum: 2ndprotocol, Bing [Bot], connatic, Google [Bot], xroot and 47 guests