Help updating simple 1.1 script to 2.0

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
JNTL
Posts: 1
Joined: 07 Feb 2023, 15:01

Help updating simple 1.1 script to 2.0

Post by JNTL » 07 Feb 2023, 15:09

Hi there,

I recently updated to AHK 2.0, and am having trouble adapating old scripts to the new syntax.

This script lets me use the capslock key to open a new tab in my default browser. If the browser isn't open, the script runs the browser. If a new tab is already open, the capslock key closes the tab instead.

Code: Select all

Capslock::

SetTitleMatchMode, 2

If WinExist("ahk_exe [browser].exe")

{
WinActivate, ahk_exe [browser].exe
wingettitle, currentwindowtitle, ahk_exe [browser].exe
	{
	If currentwindowtitle contains [New Page Title]
		{
		Send ^w
		Exit
		}
	else
		{	
		Send ^t
		Send ^l
		}
	}
}	

else

{
Run "[browser location]"
sleep 100
WinActivate, ahk_exe [browser].exe
Send ^l
}

return

+Capslock::Capslock
When attempting to run this script in AHK 2.0, I receive a syntax error right in the beginning (SetTitleMatchMode, 2). The error messages says something vague about parentheses and commas, but after reading through the update notes from 1.1 to 2.0, it is not clear to me what needs to be changed.

Thanks in advance!

swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Help updating simple 1.1 script to 2.0

Post by swagfag » 07 Feb 2023, 16:57

the first error is

Code: Select all

Error: Function calls require a space or "(".  Use comma only between parameters.

Text:	SetTitleMatchMode, 2
Line:	3
in v2 there are no longer any commands, just functions(although, in this case ure afforded a function call statement which u can read more about in the v2 docs)
the fix is

Code: Select all

SetTitleMatchMode(2)
or

Code: Select all

SetTitleMatchMode 2

the second error is

Code: Select all

Error: Hotkey or hotstring is missing its opening brace.

Line:	3
in v2, for multi-line hotkeys/hotstrings, their subroutines are unnamed functions(a similar usage was already possible in v1, except with named functions). so u need to add braces in to delineate the function's body:

Code: Select all

Capslock::{
	...
	; return ; this is also not necessary(unless u want to return early)
}

+Capslock::Capslock

the next errors are

Code: Select all

Error: Function calls require a space or "(".  Use comma only between parameters.

Text:	WinActivate, ahk_exe [browser].exe
Text:	wingettitle, currentwindowtitle, ahk_exe [browser].exe
u already know how to fix those
the next error is

Code: Select all

Error: Unexpected reserved word.

Text:	contains [New Page Title]
contains is not yet implemented in v2, so ull have to use a different way of checking(eg with InStr or regex)
the next error is

Code: Select all

Error: Function calls require a space or "(".  Use comma only between parameters.

Text:	WinActivate, ahk_exe [browser].exe
u know how to fix it
the next error is

Code: Select all

Error: Syntax error.

Specifically: ^w)

	010: If InStr(currentwindowtitle, '[New Page Title]')
	011: {
▶	012: Send(^w)
	013: Exit()
	014: }
unquoted string literals generally no longer exist in v2(except for a few directives which still allow them), so Send's parameter needs to be a quoted string literal

Code: Select all

Send('^w')

when uve fixed all sends, the next error is

Code: Select all

Warning: This variable appears to never be assigned a value.

Specifically: local ahk_exe

	004: If WinExist("ahk_exe [browser].exe")
	006: {
▶	007: WinActivate(ahk_exe [browser].exe)
	008: wingettitle(currentwindowtitle, ahk_exe [browser].exe)
	009: {
luckily, u already know what to do with unquoted string literals
the next error is

Code: Select all

Warning: This variable appears to never be assigned a value.

Specifically: local currentwindowtitle

	006: {
	007: WinActivate('ahk_exe [browser].exe')
▶	008: wingettitle(currentwindowtitle, ahk_exe [browser].exe)
	009: {
	010: If InStr(currentwindowtitle, '[New Page Title]')
here u need to check the WinGetTitle docs since the param order is different. generally, u will find fewer functions rely on OutputVar outparams(unless theyre returning more than 1 thing). and also u need to do something about that unquoted string literal there

Code: Select all

currentwindowtitle := wingettitle('ahk_exe [browser].exe')

and lastly

Code: Select all

Warning: This variable appears to never be assigned a value.

Specifically: local ahk_exe

	026: Run("[browser location]")
	027: sleep(100)
▶	028: WinActivate(ahk_exe [browser].exe)
	029: Send('^l')
	030: }
which u already know how to fix
congratulations, ur first working v2 script

Post Reply

Return to “Ask for Help (v2)”