Start/Toogle/Activate App/Program/Window Quickly - Including Chrome WebApps - Single Hotkey

Post your working scripts, libraries and tools for AHK v1.1 and older
DigiDon
Posts: 178
Joined: 19 May 2014, 04:55
Contact:

Start/Toogle/Activate App/Program/Window Quickly - Including Chrome WebApps - Single Hotkey

13 Jun 2017, 10:23

Hi, I think this can interest some people :P
EDIT: This could maybe be moved in Tutorials

This is my simple way of starting/toogling Apps Quicly - Including quick Google Chrome WebApps.
What's really interesting is how you can start it in a smart way to toogle or start quickly your favorite pgrms or ... Google chrome web apps
(Yeah that's what I really like as I figured out the parameter "--app" to do it! :dance: )
With web apps it means you can toogle or start every website as an external app having its own window/icon.
The second interesting thing IMHO is the single hotkey example to start your subroutine or menu.
See at bottom for examples

By Toogling I mean : if the window exist then it is brought to foreground. If it is already in foreground it is Minimized (although this won't be of much use except with single hotkey).
If it is not found then the program is started with optional parameters.

It relies on a simple function to Toogle a window based on some Wintitle parts and the .exe process OR Start the .exe with some parameters :
ToogleOrStart Function

Code: Select all

;               T O O G L E    O R    S T A R T   P G R M
;-------------------------------------------------------------------------------
;ToogleOrStart(PgrmProcessPathorName,WindowPartTitle="",ExeCommands="")
;***************************************************************************************
;PgrmProcessPathorName : ex : Notepad or Notepad.exe or C:\Program Files (x86)\....process.exe
;------------------------------------
;(Optional)WindowPartTitle : Some parts of the title of the window you want to toogle. e.g: "- Notepad"
;This is mainly useful in case you want to toogle or start a specific window of your program.
;Ex : with Chrome apps, the exe will always be Chrome.exe but you can detect if the specific website is opened using WindowPartTitle
;------------------------------------
;(Optional)ExeCommands : Some ex commands to start the pgrm in case it was not detected
;We use it in these examples to start web apps : e.g.: "--app=http://www.website.com"
;------------------------------------
ToogleOrStart(PgrmProcessPathorName,WindowPartTitle="",ExeCommands="")
{
    SetTitleMatchMode 2
    global CWindow, CProcessName
    global FastMenuStarted
    if FastMenuStarted!=1 ;For use with a contextmenu , see below in original post
        {
        WinGetTitle, CWindow, A
        WinGet, CProcessName, ProcessName, A
        }
    if InStr(PgrmProcessPathorName, "\")
        SplitPath, PgrmProcessPathorName , PgrmProcess
    else
        PgrmProcess:=PgrmProcessPathorName

    if (SubStr(PgrmProcess,-3)!=".exe")
        PgrmProcess.=".exe"

    IfWinExist, %WindowPartTitle% ahk_exe %PgrmProcess%
    {
        if (CProcessName=PgrmProcess && InStr(CWindow,WindowPartTitle))
            {
            WinMinimize, %WindowPartTitle% ahk_exe %PgrmProcess%
            }
        else
            {
            WinActivate, %WindowPartTitle% ahk_exe %PgrmProcess%
            }
    }
    else
        Run % PgrmProcessPathorName . "  " ExeCommands
    FastMenuStarted= ;For use with a contextmenu
}
How to use the function
Somewhere in your code to Toogle or Start you have to call the function
e.g. For Notepad, simply use :

Code: Select all

ToogleOrStart("Notepad")
;or
;ToogleOrStart("Notepad.exe")
Other software which did not register in Windows environnement Path will need the full path to the .exe

Code: Select all

ToogleOrStart("C:\Program Files\CustomSoftware.exe")
Ifyou want to Detect a specific window put some Wintitle Parts in the second parameters:
e.g. For Toogling a new Notepad (untitled) or starting one that could be :

Code: Select all

ToogleOrStart("Notepad","Untitled")
...With Google Chrome Web Apps
Now let's say you want to Toogle or start some music at 8tracks.com as a web app (I'm sure many of you could be interested by using it for Facebook and many other web apps)
You can use :

Code: Select all

ToogleOrStart("chrome.exe","8tracks","--app=http://8tracks.com/login")
How to implement it?
Here are some ideas:

We will start by the simpliest way with a standard hotkey, which I quite DISlike.
EXAMPLE WITH HOTKEY: CTRL + SHIFT + T

Code: Select all

^+t::Gosub Toogle8tracks

Toogle8tracks:
ToogleOrStart("chrome.exe","8tracks","--app=http://8tracks.com/login")
return
Here are some more interesting ways:

EXAMPLE WITH SINGLE KEY HOTKEY !!!
This will start 8tracks as a web app by simply holding down the "_" key (This is the 8 at top of my French Keyboard, that's why ;) )
!WARNING! THESE SINGLE KEY HOTKEYS TEND TO SLOW DOWN THE USED KEY A BIT AND LONG PRESS FOR FAST REAPEAT WON'T WORK ANYMORE
SO I SUGGEST YOU JUST USE SPECIAL KEYS SUCH AS THE ONE ON TOP OF KEYBOARDS OR SPECIAL CHARACTERS
EXAMPLE WITH LONG "_" KEY PRESS
IF YOU CHANGE THE HOTKEY YOU NEED TO MODIFY IT IN 4 PLACES : The Hotkey label the 2 KeyWait and the SendInput

Code: Select all

;----------------------------------------
;8tracks by "_" (8) long
;IF YOU CHANGE THE HOTKEY YOU NEED TO MODIFY IT IN 4 PLACES
;----------------------------------------
$_::
    KeyWait, _, T0.5
    If (ErrorLevel=1) {
    ToogleOrStart("chrome.exe","8tracks","--app=http://8tracks.com/login")
        KeyWait, _
    }
    Else {
        SendInput _
    }
Return

EXAMPLE WITH A CONTEXT MENU
STARTED BY CTRL+SHIFT+T (but could be single key hotkey as well!!)

Code: Select all

FastPgrmMenu()

FastPgrmMenu()
{
    Menu, FastPgrmMenu, Add, Firefox, Firefox
    Menu, FastPgrmMenu, Add, 8tracks, Toogle8tracks
    Menu, FastPgrmMenu, Add, Notepad, Notepad
}

^+t::
    global CWindow,CProcessName,FastMenuStarted
    WinGetTitle, CWindow, A
    WinGet, CProcessName, ProcessName, A
    FastMenuStarted=1
    Menu, FastPgrmMenu, Show
return

Firefox:
ToogleOrStart("Mozilla Firefox", "firefox.exe")
Return

Notepad:
ToogleOrStart("Notepad")
Return

Toogle8tracks:
ToogleOrStart("chrome.exe","8tracks","--app=http://8tracks.com/login")
return
Do you like it ? :xmas:
Don't hesitate to comment for ideas and related things ;)
EverFastAccess : Take Notes on anything the Fast way: Attach notes, Set reminders & Speed up research in 1 gesture - AHK topic
AHK Dynamic Obfuscator L - Protect your AHK code by Obfuscation - AHK topic
QuickModules for Outlook : Sort Outlook emails very quickly to multiple folders - AHK topic
Coding takes lots of time and efforts. If I have helped you or if you enjoy one of my free projects, please consider a small donation :thumbup:
Sorry I am working hard at the moment at a new job and can't commit on delays of answers & updates

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: No registered users and 117 guests