Page 1 of 1

A simple "Console.log" for debugging or developping time.

Posted: 21 May 2019, 04:38
by FredOoo
This is usefull for me. May be for you too ?

Code: Select all

/*
	; AHK v2
	global Console := new CConsole
	Console.hotkey := "^+c"  ; to show the console
	Console.log "Hello", "world", "Mina", "konnichiha", "Bonjour tout le monde"
	Console.show
	Console.log "Point", {x:100,y:200}
	;Hello world Mina konnichiha Bonjour tout le monde
	;Point {
	;	x: 100
	;	y: 200
	;}
*/


class CConsole {
    ahkPID  := ""
    ahkHWND := ""

	__New( title := "Console" ) {
		HWND := WinExist( title " ahk_class Notepad" )
		if ( HWND ) {
			PID := WinGetPID( "ahk_id " HWND )
			this.ahkPID  := "ahk_pid " PID
			this.ahkHWND := "ahk_id  " HWND
			this.clear
		} else {
			DetectHiddenWindows 1
			Run "Notepad",,"Hide", PID
			this.ahkPID := "ahk_pid " PID
			HWND := WinWait(this.ahkPID)
			if HWND=0
				Return
			this.ahkHWND := "ahk_id  " HWND
			WinMove(0, 0, A_ScreenWidth/4, A_ScreenHeight, this.ahkHWND)
			WinSetTitle( title, this.ahkHWND )
			WinShow( this.ahkHWND )
			;WinActivate( this.ahkHWND )
		}
		Return this
    }

	hotkey{
		set {
			Hotkey value, ObjBindMethod(this, "show")
		}
	}


	log( texts* ) {
		if ( !WinExist( this.ahkHWND ) )
			Return
		last := texts.Length()
		if last == 0
			ControlEditPaste( "`r`n", "Edit1", this.ahkHWND )
		for idx, txt in texts {
			if (Type(txt)="Object") {
				ControlEditPaste( "{`r`n", "Edit1", this.ahkHWND )
				for key, value in txt {
					ControlEditPaste( "`t" key ": " value "`r`n", "Edit1", this.ahkHWND )
				}
				ControlEditPaste( "}`r`n", "Edit1", this.ahkHWND )
			} else {
				rc := (idx=last? "`r`n" : " ")
				ControlEditPaste( txt rc, "Edit1", this.ahkHWND )  ; ControlSendText ? ControlEditPaste
			}
		}
	}

	show() {
		WinSetAlwaysOnTop( true, this.ahkHWND )
		WinSetAlwaysOnTop( false, this.ahkHWND )
	}

	clear() {
		ControlSetText( "", "Edit1", this.ahkHWND )
	}

}

shebang(0.1;"test/doc.php");

Re: A simple "Console.log" for debugging or developping time.

Posted: 21 May 2019, 23:37
by Ridwan
For v 1.1?

Re: A simple "Console.log" for debugging or developping time.

Posted: 23 May 2019, 11:51
by DataLife
Is there anyone willing to convert this to v 1.1?

Re: A simple "Console.log" for debugging or developping time.

Posted: 23 May 2019, 12:33
by jeeswg
I used a partial AHK v2 -> AHK v1 converter, which I will release ..., and I double-checked other lines. It might work as is, possibly you need to check the Hotkey command, and/or other lines. Cheers.

Code: Select all

/*
	; AHK v1.1
	global Console := new CConsole
	Console.hotkey := "^+c"  ; to show the console
	Console.log("Hello", "world", "Mina", "konnichiha", "Bonjour tout le monde")
	Console.show()
	Console.log("Point", {x:100,y:200})
	;Hello world Mina konnichiha Bonjour tout le monde
	;Point {
	;	x: 100
	;	y: 200
	;}
*/


class CConsole {
    ahkPID  := ""
    ahkHWND := ""

	__New( title := "Console" ) {
		HWND := WinExist( title " ahk_class Notepad" )
		if ( HWND ) {
			WinGet, PID, PID, % "ahk_id " HWND
			this.ahkPID  := "ahk_pid " PID
			this.ahkHWND := "ahk_id  " HWND
			this.clear()
		} else {
			DetectHiddenWindows, On
			Run, Notepad,, Hide, PID
			this.ahkPID := "ahk_pid " PID
			WinWait, % this.ahkPID
			HWND := WinExist()
			if HWND=0
				return
			this.ahkHWND := "ahk_id  " HWND
			WinMove, % this.ahkHWND,, 0, 0, % A_ScreenWidth/4, % A_ScreenHeight
			WinSetTitle, % this.ahkHWND,, % title
			WinShow, % this.ahkHWND
			;WinActivate, % this.ahkHWND
		}
		return this
    }

	hotkey{
		set {
			show_bind := ObjBindMethod(this, "show")
			Hotkey, % value, % show_bind
		}
	}


	log( texts* ) {
		if ( !WinExist( this.ahkHWND ) )
			return
		last := texts.Length()
		if last == 0
			Control, EditPaste, % "`r`n", Edit1, % this.ahkHWND
		for idx, txt in texts {
			;if (Type(txt)="Object") {
			if (IsObject(txt)) {
				Control, EditPaste, % "{`r`n", Edit1, % this.ahkHWND
				for key, value in txt {
					Control, EditPaste, % "`t" key ": " value "`r`n", Edit1, % this.ahkHWND
				}
				Control, EditPaste, % "}`r`n", Edit1, % this.ahkHWND
			} else {
				rc := (idx=last? "`r`n" : " ")
				Control, EditPaste, % txt rc, Edit1, % this.ahkHWND  ; ControlSendText ? ControlEditPaste
			}
		}
	}

	show() {
		WinSet, AlwaysOnTop, % true, % this.ahkHWND
		WinSet, AlwaysOnTop, % false, % this.ahkHWND
	}

	clear() {
		ControlSetText, Edit1,, % this.ahkHWND
	}

}

Re: A simple "Console.log" for debugging or developping time.

Posted: 24 May 2019, 08:00
by FredOoo
 »
 » I start learning AHK with v2 because v1 syntax is horrible, but I did it.
 »

Code: Select all

/*
	; AHK v1.1
	global Console := new CConsole
	Console.hotkey := "^+c"  ; to show the Console
	Console.log("Hello", "world", "Mina", "konnichiha", "Bonjour tout le monde")
	Console.show()  ; must use parentheses
	Console.log("Point", {x:100,y:200})
	;Hello world Mina konnichiha Bonjour tout le monde
	;Point {
	;	x: 100
	;	y: 200
	;}
*/

class CConsole
{
    ahkPID  :=
    ahkHWND :=

	__New( title := "Console" ) {
		HWND := WinExist( %title% " ahk_class Notepad" )
		if ( HWND ) {
			WinGet, PID, PID, % "ahk_id " HWND
			this.ahkPID  := "ahk_pid " PID
			this.ahkHWND := "ahk_id " HWND
			this.clear()
		} else {
			DetectHiddenWindows, On
			Run, Notepad,, Hide, PID
			this.ahkPID := "ahk_pid " PID
			WinWait, % this.ahkPID
			HWND := WinExist( this.ahkPID )
			if HWND=0
				return
			this.ahkHWND := "ahk_id " HWND
			WinMove, % this.ahkHWND,, 0, 0, % A_ScreenWidth/4, % A_ScreenHeight
			WinSetTitle, % this.ahkHWND,, %title%
			;WinActivate, % this.ahkHWND
			WinShow, % this.ahkHWND
		}
    }

	hotkey{
		set {
			show_bind := ObjBindMethod( this, "show" )
			Hotkey, % value, % show_bind
		}
	}

	log( texts* ) {
		if ( !WinExist( this.ahkHWND ) )
			return
		last := texts.Length()
		if last == 0
			Control, EditPaste, % "`r`n", Edit1, % this.ahkHWND
		for index, txt in texts {
			if (IsObject(txt)) {
				Control, EditPaste, % "{`r`n", Edit1, % this.ahkHWND
				for key, value in txt {
					Control, EditPaste, % "`t" key ": " value "`r`n", Edit1, % this.ahkHWND
				}
				Control, EditPaste, % "}`r`n", Edit1, % this.ahkHWND
			} else {
				sep := (index=last? "`r`n" : " ")
				Control, EditPaste, % txt sep, Edit1, % this.ahkHWND  ; ControlSendText ? ControlEditPaste
			}
		}
	}

	show() {
		WinSet, AlwaysOnTop, % true, % this.ahkHWND
		WinSet, AlwaysOnTop, % false, % this.ahkHWND
	}

	clear() {
		ControlSetText, Edit1,, % this.ahkHWND
	}

}

Re: A simple "Console.log" for debugging or developping time.

Posted: 24 May 2019, 09:40
by jeeswg
@FredOoo: Your conversion looks right apart from:

Code: Select all

HWND := WinExist( %title% " ahk_class Notepad" ) ;before
HWND := WinExist( title " ahk_class Notepad" ) ;after
Also: your conversion appears to lack return this, although maybe it doesn't need it, I haven't tested.

Re: A simple "Console.log" for debugging or developping time.

Posted: 24 May 2019, 09:54
by Ridwan
FredOoo wrote:
24 May 2019, 08:00
 »
 » I start learning AHK with v2 because v1 syntax is horrible, but I did it.
 »

Code: Select all

/*
	; AHK v1.1
	global Console := new CConsole
	Console.hotkey := "^+c"  ; to show the Console
	Console.log("Hello", "world", "Mina", "konnichiha", "Bonjour tout le monde")
	Console.show()  ; must use parentheses
	Console.log("Point", {x:100,y:200})
	;Hello world Mina konnichiha Bonjour tout le monde
	;Point {
	;	x: 100
	;	y: 200
	;}
*/

class CConsole
{
    ahkPID  :=
    ahkHWND :=

	__New( title := "Console" ) {
		HWND := WinExist( %title% " ahk_class Notepad" )
		if ( HWND ) {
			WinGet, PID, PID, % "ahk_id " HWND
			this.ahkPID  := "ahk_pid " PID
			this.ahkHWND := "ahk_id  " HWND
			this.clear()
		} else {
			DetectHiddenWindows, On
			Run, Notepad,, Hide, PID
			this.ahkPID := "ahk_pid " PID
			WinWait, % this.ahkPID
			HWND := WinExist( this.ahkPID )
			if HWND=0
				return
			this.ahkHWND := "ahk_id  " HWND
			WinMove, % this.ahkHWND,, 0, 0, % A_ScreenWidth/4, % A_ScreenHeight
			WinSetTitle, % this.ahkHWND,, %title%
			;WinActivate, % this.ahkHWND
			WinShow, % this.ahkHWND
		}
    }

	hotkey{
		set {
			show_bind := ObjBindMethod( this, "show" )
			Hotkey, % value, % show_bind
		}
	}

	log( texts* ) {
		if ( !WinExist( this.ahkHWND ) )
			return
		last := texts.Length()
		if last == 0
			Control, EditPaste, % "`r`n", Edit1, % this.ahkHWND
		for index, txt in texts {
			if (IsObject(txt)) {
				Control, EditPaste, % "{`r`n", Edit1, % this.ahkHWND
				for key, value in txt {
					Control, EditPaste, % "`t" key ": " value "`r`n", Edit1, % this.ahkHWND
				}
				Control, EditPaste, % "}`r`n", Edit1, % this.ahkHWND
			} else {
				sep := (index=last? "`r`n" : " ")
				Control, EditPaste, % txt sep, Edit1, % this.ahkHWND  ; ControlSendText ? ControlEditPaste
			}
		}
	}

	show() {
		WinSet, AlwaysOnTop, % true, % this.ahkHWND
		WinSet, AlwaysOnTop, % false, % this.ahkHWND
	}

	clear() {
		ControlSetText, Edit1,, % this.ahkHWND
	}

}
Thank you very much. This is really a big help since i write a script that become larger + buggy, and i'm i bit tired to debugging it with MsgBox.

Re: A simple "Console.log" for debugging or developping time.

Posted: 24 May 2019, 10:04
by Ridwan
@jeeswg
Did you really able to convert all [most] of v2 to v1? Can wait for the release. :superhappy:

Re: A simple "Console.log" for debugging or developping time.

Posted: 24 May 2019, 10:09
by FredOoo
To be honest, I don't feel very confident with the %var% or % expression syntax.
I got « title » as window's title so I tryed and tryed many syntaxes.
HWND := WinExist( title " ahk_class Notepad" ) ;; You're right

Also, I saw this in the doc:
return this ; This line can be omitted when using the 'new' operator.
https://www.autohotkey.com/docs/Objects.htm#Custom_Classes
(and the same with AHK v2)

By the way, thank's jeeswg: I wouldn't have done this if you have not done the first v1 conversion.