Skip to content

Instantly share code, notes, and snippets.

@navakelvin
Forked from getify/INFO.md
Created December 25, 2021 15:29
Show Gist options
  • Save navakelvin/3cc35fd212b89ae0d4e38f24b7df2c8f to your computer and use it in GitHub Desktop.
Save navakelvin/3cc35fd212b89ae0d4e38f24b7df2c8f to your computer and use it in GitHub Desktop.

Revisions

  1. @getify getify revised this gist Jul 27, 2020. 1 changed file with 12 additions and 4 deletions.
    16 changes: 12 additions & 4 deletions SwitchAppFocus.ahk
    Original file line number Diff line number Diff line change
    @@ -113,7 +113,7 @@ FocusApp(appLabel,selectedApp) {
    whichWin := apps[currentApp].pattern

    if (!WinActive(whichWin)) {
    WinActivate, % (whichWin)
    WinActivate, %whichWin%
    }
    }

    @@ -142,9 +142,17 @@ CycleApps(cycleDir) {
    return
    }

    ; find next/previous app
    nextIdx := Mod((currentApp + cycleDir + numApps - 1),numApps) + 1
    FocusApp(currentApp[nextIdx].label,nextIdx)
    nextIdx := currentApp

    ; find next/previous app (that's not currently active) to activate
    Loop, %numApps%
    {
    nextIdx := Mod((nextIdx + cycleDir + numApps - 1),numApps) + 1
    if (!WinActive(apps[nextIdx].pattern)) {
    FocusApp(apps[nextIdx].label,nextIdx)
    return
    }
    }
    }

    ShowToolTip(msg) {
  2. @getify getify created this gist Jul 27, 2020.
    15 changes: 15 additions & 0 deletions INFO.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,15 @@
    Download Icon: https://jumpshare.com/v/T5yuKIP3Zc6Rr29GH5ye

    ## How To Use

    1. While focused on an open window (like a browser or editor), hit the "record" hotkey (default: `win+ctrl+shift+?`) to add an app to the focus cycle list. Tooltips will pop up notifying you of recorded apps, or if there's an issue recording.

    2. To cycle forward through recorded apps, use the "cycle forward" hotkey (default: `win+ctrl+shift+>`), or click the system-tray icon, or click the menu Item "Cycle Apps" menu item.

    3. To cycle backward, use the "cycle backward" hotkey (default: `win+ctrl+shift+<`).

    ## To Compile

    ```cmd
    Ahk2Exe.exe /in "SwitchAppFocus.ahk" /icon "main.ico"
    ```
    170 changes: 170 additions & 0 deletions SwitchAppFocus.ahk
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,170 @@
    #SingleInstance force
    #WinActivateForce
    SetTitleMatchMode RegEx
    DetectHiddenWindows, On

    apps := []
    currentApp := 0
    SetupMenu()

    ; win + ctrl + shift + >
    #^+>::
    CycleForward()
    return

    ; win + ctrl + shift + <
    #^+<::
    CycleBackward()
    return

    ; win + ctrl + shift + ?
    #^+?::
    RecordCurrentActiveApp()
    return


    ; ***************************************

    SetupMenu() {
    Menu, Tray, Tip, Switch App Focus

    Menu, Tray, UseErrorLevel
    Menu, Tray, NoMainWindow

    ; remove all standard menu items
    Menu, Tray, NoStandard

    ; add a cycle-apps-forward menu item (initially disabled)
    Menu, Tray, Add, Cycle Apps, CycleForward
    Menu, Tray, Disable, Cycle Apps
    ; add a reset-apps-list menu item
    Menu, Tray, Add, Reset Apps List, ResetAppsList
    ; add a quit menu item
    Menu, Tray, Add, Quit, Quit
    ; set the cycle-apps menu item to be the "default" menu item
    Menu, Tray, Default, Cycle Apps
    ; set single clicks on the tray icon to activate the default menu item
    Menu, Tray, Click, 1

    SetupAppsList()
    }

    SetupAppsList() {
    global apps
    global currentApp

    apps := []
    currentApp := 0

    Menu, Tray, Disable, Cycle Apps

    ; reset apps menu
    try Menu, AppsMenu, DeleteAll
    try Menu, Tray, Delete, Apps
    Menu, AppsMenu, Add, --, Quit
    Menu, Tray, Insert, Cycle Apps, Apps, :AppsMenu
    Menu, AppsMenu, DeleteAll
    Menu, Tray, Disable, Apps
    }

    ResetAppsList() {
    SetupAppsList()
    ShowToolTip("Recorded apps reset.")
    }

    RecordCurrentActiveApp() {
    global apps

    WinGetActiveTitle, winTitle
    if (winTitle = "") {
    ShowToolTip("No app focused!!!")
    return
    }

    ; construct pattern for app name
    RegExMatch(winTitle,"O)(?:.+- )?(.+)$",winTitleParts)
    appName := winTitleParts.Value(1)
    StringLower, appNameLower, appName
    appNamePattern := % ("i)" appNameLower "$")

    ; ensure this app isn't already in the list
    Loop, % (apps.Length())
    {
    if (appNamePattern = apps[A_Index].pattern) {
    ShowToolTip("App '" appName "' already recorded.")
    return
    }
    }

    apps.Push({ "label": appName, "pattern": appNamePattern })

    Menu, Tray, Enable, Cycle Apps
    Menu, Tray, Enable, Apps
    Menu, AppsMenu, Add, %appName%, FocusApp

    ShowToolTip("App '" appName "' recorded.")
    }

    FocusApp(appLabel,selectedApp) {
    global apps
    global currentApp

    currentApp := selectedApp
    whichWin := apps[currentApp].pattern

    if (!WinActive(whichWin)) {
    WinActivate, % (whichWin)
    }
    }

    CycleForward() {
    global apps

    if (apps.Length() > 0) {
    CycleApps(1)
    }
    }

    CycleBackward() {
    global apps

    if (apps.Length() > 0) {
    CycleApps(-1)
    }
    }

    CycleApps(cycleDir) {
    global apps
    global currentApp

    numApps := apps.Length()
    if (numApps = 0) {
    return
    }

    ; find next/previous app
    nextIdx := Mod((currentApp + cycleDir + numApps - 1),numApps) + 1
    FocusApp(currentApp[nextIdx].label,nextIdx)
    }

    ShowToolTip(msg) {
    ; cancel previous timer in case it's still running
    try SetTimer,, Off
    HideToolTip()

    ToolTip, %msg%
    ; hide the tooltip (once) 1000ms from now
    SetTimer, HideToolTip, -1000
    }

    HideToolTip() {
    ToolTip
    }

    ReloadApp() {
    Reload
    }

    Quit() {
    ExitApp
    }