Open a new tab of Chrome, in an active window

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Tweva33
Posts: 7
Joined: 04 Feb 2021, 13:24

Open a new tab of Chrome, in an active window

14 Mar 2024, 13:21

So I am trying to create a shortcut that will allow me to open a specific website in a new tab, when chrome is already open. To start I was trying to open a new tab in chrome. I got a script to work that opens a new window of chrome with a blank tab, but I want it to be in a window that is already there. This is my working script:

Code: Select all

; Open new Chrome tab (Win+Alt+C)
;^+C::
;  Run, C:\Program Files\Google\Chrome\Application\chrome.exe --new-tab
;return
This code looks like it should work, but I keep getting an error about line 2
"Error at line 2.
Line Text: WinExist, ahk_class chrome.exe
This line does not contain a recognized action.
The program will exit."

Code: Select all

^+C::
  WinExist, ahk_class chrome.exe,  ; Check if Chrome is running (by class name)
  IfWinExist, ahk_class chrome.exe {
    WinActivate, ahk_class chrome.exe  ; Switch focus to existing Chrome
    Sleep, 100 ; Optional: Short delay to ensure Chrome activation (adjust if needed)
    Run, chrome.exe --new-tab        ; Open new tab in activated Chrome
  }
return
Help? lol. Not sure where to go from here. Thanks!

[Mod edit: Moved topic from AHK v2 help since this v1 code.]
User avatar
gregster
Posts: 9224
Joined: 30 Sep 2013, 06:48

Re: Open a new tab of Chrome, in an active window

14 Mar 2024, 13:48

WinExist() is a function, and would need function syntax with parentheses and quotes for literal strings (see docs for examples). This would make sense with a regular If.

But you are checking the same anyway on the next line with the (deprecated) command IfWinExist. But the opening brace { shouldn't be on the same line; this (OTB style for code blocks) is only allowed for If statements with expressions.

Please also note that I moved your topic from AHK v2 help to v1 help since you posted AHK v1 code.
Tweva33
Posts: 7
Joined: 04 Feb 2021, 13:24

Re: Open a new tab of Chrome, in an active window

14 Mar 2024, 13:58

Ah, I checked what version using the Window Spy, but I see now that I am actaully using v1. Sorry about that
Tweva33
Posts: 7
Joined: 04 Feb 2021, 13:24

Re: Open a new tab of Chrome, in an active window

14 Mar 2024, 14:00

I will admit, I know nothing of any of this. I was using ai to help me write all of this. The last suggestion it made was to use a slightly modified version of the original code, and guess what? It worked!

Code: Select all

^+C::
  Run, chrome.exe --new-tab https://app.innergy.com/#/projects/grid
return
User avatar
gregster
Posts: 9224
Joined: 30 Sep 2013, 06:48

Re: Open a new tab of Chrome, in an active window

14 Mar 2024, 14:04

Ah okay... but please note for future requests that questions about AI-generated code are currently not allowed on these forums (see forum rules) since the AI results for AHK code are often nonsense and just waste everyone's time.
(Your original post might confirm this suspicion.)

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Bing [Bot] and 129 guests