F. Vosnim
F. Vosnim

Reputation: 574

How to open a new chrome tab with Autohotkey?

This code opens a new chrome tab and place whole chrome window behind all other running programs:

Browser = "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"
URL = "http://cyberforum.ru"

Name = Mid(Browser, InStrRev(Browser, "\") + 1)
For Each P In GetObject("winmgmts:\\.\root\CIMV2")._
ExecQuery("Select ParentProcessID from Win32_Process Where Name = '" & Name & "'")
  PID = P.ParentProcessID : If InStr(PIDs & " ", " " & PID & " ") Then Exit For
  PIDs = PIDs & " " & PID
Next

With CreateObject("WScript.Shell")
  Set Exe = .Exec(Browser & " " & URL)
  If IsEmpty(PID) Then PID = Exe.ProcessID Else WSH.Sleep 1000
  Do : A = .AppActivate(PID) : Loop Until A
  .SendKeys "%{Esc}"
End With

How can I do the same thing with Autohotkey?

Upvotes: 4

Views: 5527

Answers (1)

Relax
Relax

Reputation: 10543

In AHK you can use a variable (e.g. chromePID) to store the program's unique Process ID (PID) and handle it as follows:

Browser = "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"
URL = "http://cyberforum.ru"

IfWinExist ahk_exe chrome.exe
{
    WinGet, chromePID, PID, ahk_exe chrome.exe
    Run %Browser% "%URL%"
}
else
    Run %Browser% "%URL%",,, chromePID
WinWait ahk_pid %chromePID%
WinSet, Bottom,, ahk_pid %chromePID% ; send the window beneath all other windows
return

https://autohotkey.com/docs/commands/WinGet.htm#PID https://autohotkey.com/docs/commands/Run.htm#Parameters https://autohotkey.com/docs/commands/WinSet.htm#Bottom

Upvotes: 6

Related Questions