Resizing a window regardless of how it is launched

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Lem2001
Posts: 127
Joined: 27 Jun 2017, 17:59

Resizing a window regardless of how it is launched

28 Jun 2017, 17:25

I have managed to use WinMove to size and position a window to my liking based on a hotkey trigger. However, this sizing will obviously not apply when the window is launched by other means (e.g. Start Menu etc.)

How can I make it so that this particular window is always shown at the same size regardless of how it is launched?

I did try to use IfWinExist, but again, this only worked if I assigned a hotkey to it.
A_AhkUser
Posts: 1147
Joined: 06 Mar 2017, 16:18
Location: France
Contact:

Re: Resizing a window regardless of how it is launched

28 Jun 2017, 18:00

Here's some command that seems to me relevant to take advantage of in this case:

. #Persistent directive
. SetTimer
. Loop
. WInWait
. WinWaitClose

Basically, the idea consists in making a run only once timer start a new thread, tasked with setting the window position and size as soon as it appears (or appears again after possibly been destroyed).

Hope this helps


[EDIT] As an alternative you can also create a script with custom command line arguments support:

Code: Select all

arguments = %0%

if not (arguments)
	ExitApp
; otherwise
winTitle = %1%
WinActivate % winTitle
WinMove, % winTitle,, 0, 0, 400, 400
and create a shorcut for you program specifying as its target the command line below:

Code: Select all

FullPathOfAutoHotkey.exe YourScriptName.ahk WindowTitle
; for example with notepad: "C:\Program Files (x86)\AutoHotkey\AutoHotkeyU32.exe" "C:\Users\Jérémy\Desktop\ahk.ahk" "ahk_class Notepad"
so that whether you lunch this shorcut by clicking it or running it from an ahk script using the run command the window will be sized and positioned to your liking at start.
my scripts
Lem2001
Posts: 127
Joined: 27 Jun 2017, 17:59

Re: Resizing a window regardless of how it is launched

01 Jul 2017, 11:47

Thank you very much for your reply.

You helpfully provided an example of how to insert a program into the shortcut (which is the easy, straight-forward bit that even I could figure out) but not how to do the same in the script itself (which is the difficult bit that I don't understand).

I don't know which bits of the code are 'placeholders' for me to insert the name of my chosen app, and which bits are actually part of the code.

With regards to your other suggestion; the idea of having a persistent script to always 'catch' the program whenever it is run (so that it always appears correctly sized) sounds great, but unfortunately beyond my beginner abilities. Also, I'm a bit hesitant about using timers because it sounds as if it could become inefficient or take too much CPU if done incorrectly (which is very likely, because I don't know what I am doing - haha). So I will stick with the shortcut method.

I like the fact that you can point to a script and then specify is parameters. Presumably that means that I could put various window sizes all in once script, rather than needing to create a separate file for each one, and then use parameters to specify the appropriate code section in that single script.

Would the code that you provided simply be repeated within the script with new settings for each app? that I wanted to size? I'm not really clear on how to structure it, or where to put the window / program name.
A_AhkUser
Posts: 1147
Joined: 06 Mar 2017, 16:18
Location: France
Contact:

Re: Resizing a window regardless of how it is launched

01 Jul 2017, 14:26

Hi Lem2001,

Concerning the command line script it sees incoming parameters as the variables %1%, %2%,... while %0% contains the total number of parameters passed. Your criteria are: the program to lunch, its position (x and y), its size (widht and height) - 5 parameters in total:

Code: Select all


; cmdprog.ahk
arguments = %0% ; total number of arguments
if not (arguments == 5)
ExitApp
path = %1% ; first argument is the path to run
x =%2% ; second argument is x position
y =%3% ; third argument is y position
w =%4% ; fourth argument is width
h =%5% ; fifth argument is height

run, % path,, UseErrorLevel, PID ; run the path and store the program ID (PID) in PID variable
if (ErrorLevel) {
MsgBox, 64,, % "Failed to lunch program.`r`nThe program will exit.'"
ExitApp
}
WinWait, ahk_pid %PID% ; waits for the matching window to appear
ID := WinExist() ; stores its ID
WinMove, ahk_id %ID%,, % x, % y, % w, % h ; move this window according to the command line parameters
ExitApp
If you intend to create more than one shorcut without the need to neither create a separate file for each one nor put various window sizes and position hard-coded in you script, I suggest you make a GUI script tasked with create shorcuts taking advantage of the FileCreateShorcut command (with autohotkey.exe as target and the command line script above, the program, its size (w and h) an its position (x and y) as argument,s depending on GUI input) - here's a basic and ugly GUI:

Code: Select all

; GUI.ahk
#NoEnv
#Warn
#SingleInstance force


; a basic GUI to create shorcuts
Gui, Margin, 15, 15
Gui, Font, s12, Segoe UI
Gui, Color, White, White
Gui, Add, Text, cBlue Section, name of the link:
Gui, Add, Edit, ys w280 vname,
Gui, Add, Text, cBlue Section xm, program to be run:
Gui, Add, Edit, ys w280 vpath,
Gui, Add, Button, ys gLabel, ...
Gui, Add, Text, cBlue Section xm, window position:
Gui, Add, Text, ys, x:
Gui, Add, Edit, ys +Number vX, 0	
Gui, Add, Text, ys, y:,
Gui, Add, Edit, ys +Number vY, 0
Gui, Add, Text, cBlue Section xm, window size:
Gui, Add, Text, ys, width:
Gui, Add, Edit, ys +Number vW, 400	
Gui, Add, Text, ys, height:
Gui, Add, Edit, ys +Number vH, 400
Gui, Add, Button, Section x300 w80 gOK, OK
Gui, Show, autosize
return


Label: ; label subroutine
FileSelectFile, selectedFile ; displays a standard dialog to choose a file and save the choice (if any) in selectedFile variable
GuiControl,, path, % selectedFile ; fill the edit with the choice
return

OK: ; button OK subroutine
Gui, Submit, NoHide ; saves the contents of each GUI control to its associated variable (here: name, path, x, y, w, h)
arguments := """" . A_ScriptDir . "\cmdprog.ahk" . """" . A_Space . """" . path . """" . A_Space . X . A_Space . Y . A_Space . W . A_Space . H
; link arguments: the mainScript ; the path of the file to run ; x position ; y position ; width ; height
; assumes that cmdprog.ahk is in the GUI script own directory
FileCreateShortcut, % A_AhkPath, % A_ScriptDir . "\" . name . ".lnk",, % arguments
; create a shorcut called ChosenName.lnk in the script own directory which lunch AHK with the arguments 
if not (ErrorLevel)
	MsgBox, 64,, Shortcut created.
return

GuiClose:
ExitApp
Cheers


[EDIT]
And to lunch it from the script, use the run command, specifying arguments:

Code: Select all

!i:: ; ALT+I
path := "notepad.exe"
x := 100
y := 200
w := 500
h := 500
arguments := """" . A_ScriptDir . "\cmdprog.ahk" . """" . A_Space . """" . path . """" . A_Space . x . A_Space . y . A_Space . w . A_Space . h
; link arguments: the mainScript ; the path of the file to run ; x position ; y position ; width ; height
; assumes cmdprog.ahk is in the script own directory
run, % A_AHKPath . A_Space . arguments
; runs AutoHotkey.exe with arguments
return
my scripts
Lem2001
Posts: 127
Joined: 27 Jun 2017, 17:59

Re: Resizing a window regardless of how it is launched

12 Jul 2017, 06:51

Thank you SO much for your incredibly helpful and detailed response. I really do appreciate it.

Sorry it took me so long to reply to you (I've been away and I was not able to check the forum).
Lem2001
Posts: 127
Joined: 27 Jun 2017, 17:59

Re: Resizing a window regardless of how it is launched

22 Jul 2017, 19:55

This has been working well, but is there anyway to use it without having to specify all of the values?

If I use the WinMove command directly then there is the the option to leave certain values blank (comma separated). When doing so, the program that is being launched will determine its own values for those values that have not been defined, while at the same time still obeying the values that have been set.

For example: , , 900, 680 will set window width to 900 and window height to 680, but WinMove will let the program determine x and y position for itself (because those values have not been specified).

This is particularly important for applications where several instances may be run. Most programs will automatically diagonally stagger each new window as it's opened so that you can see all of them, whereas if you are forced to specify the onscreen placement (i.e. x & y) then all instances of that program will have its windows appear in the exact same position directly on top of each other so that they can't be seen.

Therefore, my question is:

How should I amend the script below to allow the option to leave certain values blank (e.g. x, y, width, height) so that the program being launched can handle those blank values itself as it normally would, and only those values that have been specified are imposed by AHK's WinMove.

Code: Select all

; cmdprog.ahk
arguments = %0% ; total number of arguments
if not (arguments == 5)
ExitApp
Path = %1% ; first argument is the path to run
x =%2% ; second argument is x position
y =%3% ; third argument is y position
w =%4% ; fourth argument is width
h =%5% ; fifth argument is height

run, % path,, UseErrorLevel, PID ; run the path and store the program ID (PID) in PID variable
if (ErrorLevel) {
MsgBox, 64,, % "Failed to lunch program.`r`nThe program will exit.'"
ExitApp
}
WinWait, ahk_pid %PID% ; waits for the matching window to appear
ID := WinExist() ; stores its ID
WinMove, ahk_id %ID%,, % x, % y, % w, % h ; move this window according to the command line parameters
ExitApp

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Chunjee, jeves and 165 guests