From b499460549b3640d0d6fe9a6d1d8a137ee431197 Mon Sep 17 00:00:00 2001 From: Sergio Dias Date: Sun, 26 Feb 2017 19:57:23 +0000 Subject: [PATCH] Added ability to run programs on desktop entry/exit. --- README.md | 26 +++++++++++++++- settings.ini | 24 +++++++++++++++ virtual-desktop-enhancer.ahk | 60 ++++++++++++++++++++++++++++-------- 3 files changed, 97 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index f628c4a..58f6922 100644 --- a/README.md +++ b/README.md @@ -285,7 +285,31 @@ The following shortcuts are available: ### Mouse Shortcuts -You switch between desktops by scrolling over the taskbar, if that setting is enabled. +You can switch between desktops by scrolling over the taskbar, if that setting is enabled. + +### Running Programs When Switching Desktops + +You can make it so a program is executed every time you enter or leave each desktop. For that you just need to set the settings under the "[RunProgramWhenSwitchingToDesktop]" and "[RunProgramWhenSwitchingFromDesktop]" sections. Please see the example below for clarification. + +
+[General]
+DefaultDesktop=2
+
+[RunProgramWhenSwitchingToDesktop]
+1=C:/Batch Files/Open Chrome.bat
+2=C:/Batch Files/Open Outlook.bat
+3=
+
+[RunProgramWhenSwitchingFromDesktop]
+1=C:/Batch Files/Close Chrome.bat
+2=C:/Batch Files/Close Outlook.bat
+3=
+
+ +With the configuration above, once this app starts, it will switch to desktop 2, and it will run the "Open Outlook.bat" program. +If you then switch to the first desktop, it will run the "Close Outlook.bat" and "Open Chrome.bat" programs. +If you then switch to the second desktop, it will run the "Close Chrome.bat" and "Open Outlook.bat" programs. +If you then switch to the third desktop, it will run the "Close Outlook.bat" program. ### Tray Icon diff --git a/settings.ini b/settings.ini index 26945a9..5635933 100644 --- a/settings.ini +++ b/settings.ini @@ -74,4 +74,28 @@ DesktopAlt10=Numpad0 7= 8= 9= +10= + +[RunProgramWhenSwitchingToDesktop] +1= +2= +3= +4= +5= +6= +7= +8= +9= +10= + +[RunProgramWhenSwitchingFromDesktop] +1= +2= +3= +4= +5= +6= +7= +8= +9= 10= \ No newline at end of file diff --git a/virtual-desktop-enhancer.ahk b/virtual-desktop-enhancer.ahk index 4426728..3b97ce8 100644 --- a/virtual-desktop-enhancer.ahk +++ b/virtual-desktop-enhancer.ahk @@ -43,6 +43,8 @@ VWMess(wParam, lParam, msg, hwnd) { ; Auto Execute ; ====================================================================== +; Set up tray tray menu + Menu, Tray, NoStandard Menu, Tray, Add, &Manage Desktops, OpenDesktopManager Menu, Tray, Default, &Manage Desktops @@ -50,6 +52,8 @@ Menu, Tray, Add, Reload Settings, Reload Menu, Tray, Add, Exit, Exit Menu, Tray, Click, 1 +; Read and groom settings + ReadIni("settings.ini") global TooltipsEnabled := (TooltipsEnabled != "" and TooltipsEnabled ~= "^[01]$") ? TooltipsEnabled : 1 @@ -61,9 +65,20 @@ global TooltipsFontColor := (TooltipsFontColor != "" and TooltipsFontColor ~= "^ global TooltipsBackgroundColor := (TooltipsBackgroundColor != "" and TooltipsBackgroundColor ~= "^0x[0-9A-Fa-f]{1,6}$") ? TooltipsBackgroundColor : "0x1F1F1F" global GeneralUseNativePrevNextDesktopSwitchingIfConflicting := (GeneralUseNativePrevNextDesktopSwitchingIfConflicting ~= "^[01]$" && GeneralUseNativePrevNextDesktopSwitchingIfConflicting == "1" ? true : false) +; Initialize + +global taskbarID=0 +global previousDesktopNo=0 +global doFocusAfterNextSwitch=0 +global hasSwitchedDesktopsBefore=1 + +initialDesktopNo := _GetCurrentDesktopNumber() + SwitchToDesktop(GeneralDefaultDesktop) -; Update everything again, if the default desktop matches the current one -OnDesktopSwitch(GeneralDefaultDesktop) +; Call "OnDesktopSwitch" since it wouldn't be called otherwise, if the default desktop matches the current one +if (GeneralDefaultDesktop == initialDesktopNo) { + OnDesktopSwitch(GeneralDefaultDesktop) +} ; ====================================================================== ; Set Up Key Bindings @@ -114,7 +129,7 @@ for index in arrayS { setUpHotkey(hk, handler, settingPaths) { Hotkey, %hk%, %handler%, UseErrorLevel if (ErrorLevel <> 0) { - MsgBox, 16, Error, One or more keyboard shortcut settings have been defined incorrectly in the settings file: `n%settingPaths%. `n`nPlease read the README and reconfigure them. + MsgBox, 16, Error, One or more keyboard shortcut settings have been defined incorrectly in the settings file: `n%settingPaths%. `n`nPlease read the README for instructions. Exit } } @@ -302,18 +317,18 @@ OnDesktopSwitch(n:=1) { _ChangeAppearance(n) _ChangeBackground(n) _FocusIfRequested() -} -Noop() { - Hotkey, LWin UP, Off + if (previousDesktopNo) { + _RunProgramWhenSwitchingFromDesktop(previousDesktopNo) + } + _RunProgramWhenSwitchingToDesktop(n) + previousDesktopNo := n } ; ====================================================================== ; Functions ; ====================================================================== -global doFocusAfterNextSwitch=0 - SwitchToDesktop(n:=1) { doFocusAfterNextSwitch=1 _ChangeDesktop(n) @@ -346,8 +361,6 @@ _IsPrevNextDesktopSwitchingKeyboardShortcutConflicting(hkModifiersSwitch, hkIden return ((hkModifiersSwitch == "<#<^" || hkModifiersSwitch == ">#<^" || hkModifiersSwitch == "#<^" || hkModifiersSwitch == "<#>^" || hkModifiersSwitch == ">#>^" || hkModifiersSwitch == "#>^" || hkModifiersSwitch == "<#^" || hkModifiersSwitch == ">#^" || hkModifiersSwitch == "#^") && (hkIdentifierNextOrPrevious == "Left" || hkIdentifierNextOrPrevious == "Right")) } -global taskbarID=0 - _IsCursorHoveringTaskbar() { MouseGetPos,,, mouseHoveringID if (!taskbarID) { @@ -444,11 +457,34 @@ _GetIsAppPinned(windowID:="") { return _CallWindowProc(IsPinnedAppProc, windowID) } +_RunProgram(program:="", settingName:="") { + if (program <> "") { + if (FileExist(program)) { + Run, % program + } + else { + MsgBox, 16, Error, The program "%program%" is not valid. `nPlease reconfigure the "%settingName%" setting. `n`nPlease read the README for instructions. + } + } +} + +_RunProgramWhenSwitchingToDesktop(n:=1) { + if (n == 0) { + n := 10 + } + _RunProgram(RunProgramWhenSwitchingToDesktop%n%, "[RunProgramWhenSwitchingToDesktop] " . n) +} + +_RunProgramWhenSwitchingFromDesktop(n:=1) { + if (n == 0) { + n := 10 + } + _RunProgram(RunProgramWhenSwitchingFromDesktop%n%, "[RunProgramWhenSwitchingFromDesktop] " . n) +} + _ChangeBackground(n:=1) { line := Wallpapers%n% - isHex := RegExMatch(line, "^0x([0-9A-Fa-f]{1,6})", hexMatchTotal) - if (isHex) { hexColorReversed := SubStr("00000" . hexMatchTotal1, -5)