Is it possible to create this kind of data in AutoHoktey?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Ralf_Reddings200244
Posts: 121
Joined: 11 Mar 2023, 14:16

Is it possible to create this kind of data in AutoHoktey?

05 May 2024, 08:58

I need to create a function that will take a window and resize it or restore its initial size. I am stuck on the part of recording the windows initial position and size, so I can restore its initial position later on.

Here is what I have:

Code: Select all

win_snapToMonitor(winTitle := "a", subCommand := ""){
	static history := []
	hwnd := winExist(winTitle)
	monNo	:= coord_OnMonitor(hwnd)														;this function simply return a monitor No, such as "1" or "2"
	SysGet, mon, Monitor,% monNo
	WinGetPos, winX, winY, winW, winH,% winTitle
	
	;if (history has hwnd in it){															;this block is pseudo code
	;	restore window
	;	remove it from the "history" array										
	;	return
	;}
	;else{
	;	history.push(hwnd:{x:winX, y:winY, w:winW, h:winH})
	;}

	if (subCommand = "toLeft")
		WinMove,% "ahk_id" hwnd,,% monLeft                                   ;snap to left corner of monitor
	else if (subCommand = "toTop")                                           ;snap to top corner of monitor  
		WinMove,% "ahk_id" hwnd,,,% monTop
	else if (subCommand = "toRight")
		winMove,% "ahk_id" hwnd,,,% monTop
	else if (subCommand = "toBottom")
		winMove,% "ahk_id" hwnd,,,% monBottom - winW
	else if (subCommand = "Horiz")
		winMove,% "ahk_id" hwnd,,% monLeft,,% monRight 							   ;fill all horizontal
	else if (subCommand = "Vert")
		WinMove,% "ahk_id" hwnd,,,% monTop,,% (montop - monBottom) * -1	  ;fill all vertical
}
The line history.push(hwnd:{x:winX, y:winY, w:winW, h:winH}) keeps throwing an ==> Unexpected "{" error.

I am trying to store a dictionary (map?) whose name is a window handle. Looking at it on its own, it looks like this

Code: Select all

hwnd := winExist("code")
WinGetPos, winX, winY, winW, winH,% winTitle
history := []
history.push(hwnd{x:winX, y:winY, w:winW, h:winH})
history.push(%hwnd%:{x:winX, y:winY, w:winW, h:winH})      ;this does not work either
history.push((%hwnd%):{x:winX, y:winY, w:winW, h:winH})    ;this does not work either
history.push((hwnd):{x:winX, y:winY, w:winW, h:winH})      ;this does not work either
msgbox,% json.dump(history)                                ;print a strinfied json of the AHK object
I am trying to create data like this

Code: Select all

[
	"0x13f11" : {
		"x"		: 100, 
		"y"		: 100,
		"w"	: 1000,
		"w"	: 1000,
	}
	"0x13f722" : {
		"x"		: 200, 
		"y"		: 200,
		"w"	: 2000,
		"w"	: 2000,
	}
...
]
I decided to have the data in this structure, because I am thinking I can query history and return a matching handle in a list of handles it has, doing arrayMatch(history, hwnd) should return a array element

The code for arrayMatch

Code: Select all

;Match a value in a array using case-insensitive string matching, with the option to return first or all matches and value or index
arrayMatch(array, match, all := 0, index := 0){
	out := []
	loop,% array.Length()
		{
			if (index){
				if (array[A_Index] = match){
						return A_Index
					}	
				}
			else{
				if (array[A_Index] == match){
					if (all)
						out.push((array[A_Index]))
					else
						return (array[A_Index])
					}	
				}
		}
	return out
}
I am a very novice programmer, so any suggestions for a better data structure would be most welcome.

PS: I know windows kind of does this feature, but this is a programming project for me.

Any help would be greatly appreciated!
Last edited by Ralf_Reddings200244 on 05 May 2024, 09:14, edited 1 time in total.
User avatar
mikeyww
Posts: 28871
Joined: 09 Sep 2014, 18:38

Re: Is it possible to create this kind of data in AutoHoktey?

05 May 2024, 09:02

Hello,

This is the v1 forum. You should decide whether you would like to write a script in v1 or v2. You cannot mix the syntax of the different major versions within a script. After you decide on a version, find the syntax in the corresponding documentation.
Last edited by mikeyww on 05 May 2024, 09:06, edited 1 time in total.
User avatar
Chunjee
Posts: 1589
Joined: 18 Apr 2014, 19:05
Contact:

Re: Is it possible to create this kind of data in AutoHoktey?

05 May 2024, 09:03

Code: Select all

history := []
history.push({hwnd: {x: winX, y: winY, w: winW, h: winH}})
Should work but I haven't WinGetPos() to test with
User avatar
Chunjee
Posts: 1589
Joined: 18 Apr 2014, 19:05
Contact:

Re: Is it possible to create this kind of data in AutoHoktey?

05 May 2024, 09:07

Ralf_Reddings200244 wrote:
05 May 2024, 08:58
I decided to have the data in this structure, because I am thinking I can query history and return a matching handle in a list of handles it has, doing arrayMatch(history, hwnd) should return a array element
find all matches: https://biga-ahk.github.io/biga.ahk/#/?id=filter
find just the first match: https://biga-ahk.github.io/biga.ahk/#/?id=find

under the hood they utilize https://biga-ahk.github.io/biga.ahk/#/?id=matches for shallow matching
Ralf_Reddings200244
Posts: 121
Joined: 11 Mar 2023, 14:16

Re: Is it possible to create this kind of data in AutoHoktey?

05 May 2024, 09:16

@mikeyww
@Chunjee
Pardon me, I was using my personal v1 function wrappers for commands, I fixed them now.


@Chunjee
Thanks for that! I will take a look at the library you linked to!
User avatar
Chunjee
Posts: 1589
Joined: 18 Apr 2014, 19:05
Contact:

Re: Is it possible to create this kind of data in AutoHoktey?

05 May 2024, 09:24

Code: Select all

history := []
history.push({"hwnd": {"x": winX, "y": winY, "w": winW, "h": winH}})
This is functionally the same and the forum highlighting agrees with it a bit better :thumbup:

Cool project I think

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: AlFlo and 137 guests