Help! I have no idea where I went wrong

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
newmodelno15
Posts: 1
Joined: 21 Mar 2023, 17:07

Help! I have no idea where I went wrong

Post by newmodelno15 » 21 Mar 2023, 17:14

Okay! So, I'm trying to, upon hitting ctrl+shift+o, have the computer open an Incognito Window in Google Chrome, then open a list of tabs.
I couldn't make it even open an incognito window, so I have it opening a normal window, opening an incognito window, then killing the original window. After that, it's supposed to open the desired sites. When I run it....it does nothing!
Here's my code:

Code: Select all

^+o::
{
	Run "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"
	WinWait "New Tab"
	WinActivate "New Tab"
	Send "^+n"
	WinActivate "New Tab"
	if WinExist("New Tab")
		WinKill
	WinWait "New Incognito Tab"
	WinActivate "New Incognito Tab"
	A_Clipboard := "mail.google.com"
	Send "^v"
	Sleep 500
	Send "+t"
	A_Clipboard := "youtube.com"
	Send "^v"
	Sleep 500
	Send "+t"
	A_Clipboard := "https://www.wikipedia.org/"
	Send "^v"
}
Where did I go wrong?
(Also, if someone knows how to make a Chrome Shortcut that opens more than one tab in incognito, that'd be even better... I've only figured out how to get 1 tab.)


[Mod edit: Replaced color tags with code tags. Please use them yourself when posting code. Besides the other advantages of the code box such as preserving indentation and syntax highlighting, your blue text is difficult for those using a dark theme to read.]

User avatar
mikeyww
Posts: 26929
Joined: 09 Sep 2014, 18:38

Re: Help! I have no idea where I went wrong

Post by mikeyww » 21 Mar 2023, 19:45

Welcome to this AutoHotkey forum!

Code: Select all

#Requires AutoHotkey v2.0
nav := (url := '') => Run('chrome.exe -incognito ' url)

^+o:: {
 SoundBeep 1500
 nav
 Sleep 200
 nav('http://www.autohotkey.com/')
 Sleep 200
 nav('https://www.autohotkey.com/boards/viewtopic.php?f=82&t=115287')
}

RussF
Posts: 1266
Joined: 05 Aug 2021, 06:36

Re: Help! I have no idea where I went wrong

Post by RussF » 22 Mar 2023, 06:45

@mikeyww, your example took me down a rabbit hole of Fat Arrow Functions, Closures and Free Variables just to try and figure out what you were doing. Thanks for the motivation to dig around in the V2 docs and learn something new!

Russ

User avatar
mikeyww
Posts: 26929
Joined: 09 Sep 2014, 18:38

Re: Help! I have no idea where I went wrong

Post by mikeyww » 22 Mar 2023, 07:03

Sure. A fat-arrow function is just shorthand for a short function, nothing more! It's not a special function, just a streamlined syntax. I think that a key concept of this type of function is that the function returns the value of the provided expression.

Concepts of the script:
1. The Run function can be used to navigate to a URL in a new tab.
2. Chrome has command-line parameters, including one for its incognito mode.

RussF
Posts: 1266
Joined: 05 Aug 2021, 06:36

Re: Help! I have no idea where I went wrong

Post by RussF » 22 Mar 2023, 07:52

I have to say that your particular use of the operator makes waaaay more sense than the example given in the docs.
Fat arrow function. Defines a simple function and returns a Func or Closure object. Write the function's parameter list (optionally preceded by a function name) to the left of the operator. When the function is called (via the returned reference), it evaluates the sub-expression expr and returns the result.

The following two examples are equivalent:

sumfn := Sum(a, b) => a + b
-------------------------
Sum(a, b) {
return a + b
}
sumfn := Sum

In both cases, the function is defined unconditionally at the moment the script launches, but the function reference is stored in sumfn only if and when the assignment is evaluated.
I can easily understand

Code: Select all

sumfn := (a, b) => a + b
but what exactly is the purpose of

Code: Select all

sumfn := Sum(a, b) => a + b
and why would you use it? Which is the function that you call, sumfn or sum()...or either? And if either, why?

Russ

User avatar
mikeyww
Posts: 26929
Joined: 09 Sep 2014, 18:38

Re: Help! I have no idea where I went wrong

Post by mikeyww » 22 Mar 2023, 08:27

You're right. I also found this passage (that you noted) in the documentation to require a lot of mental energy.
Specifying a name for the function allows it to be called recursively or by other nested functions without storing a reference to the closure within itself (thereby creating a problematic circular reference). It can also be helpful for debugging, such with Func.Name or when displayed on the debugger's call stack.
I've never needed to specify a name for the function (so far), but these are some of the potential needs. I haven't even tested it.

RussF
Posts: 1266
Joined: 05 Aug 2021, 06:36

Re: Help! I have no idea where I went wrong

Post by RussF » 22 Mar 2023, 09:10

Trying to wrap my head around that causes "a problematic circular reference" in my brain. :crazy:

Russ

User avatar
mikeyww
Posts: 26929
Joined: 09 Sep 2014, 18:38

Re: Help! I have no idea where I went wrong

Post by mikeyww » 22 Mar 2023, 10:21

I'm not sure, as the following worked, but others may have a real explanation here, or an example! The documentation needs improvement on this topic.

Code: Select all

#Requires AutoHotkey v2.0
sumfn := (a, b) => a + b < 10 ? sumfn(a, a + b) : (a + b)
MsgBox sumfn(1, 2)

Post Reply

Return to “Ask for Help (v2)”