Skip to content
This repository has been archived by the owner on Dec 23, 2018. It is now read-only.

Commit

Permalink
Merge pull request #34 from sdias/release-0.10.2
Browse files Browse the repository at this point in the history
Added ability to run programs on desktop entry/exit.
  • Loading branch information
sdias authored Feb 26, 2017
2 parents b1a8d34 + 3c68f74 commit 7527698
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 13 deletions.
26 changes: 25 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

<pre>
[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=
</pre>

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

Expand Down
24 changes: 24 additions & 0 deletions settings.ini
Original file line number Diff line number Diff line change
Expand Up @@ -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=
60 changes: 48 additions & 12 deletions virtual-desktop-enhancer.ahk
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,17 @@ 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
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
Expand All @@ -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
Expand Down Expand Up @@ -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
}
}
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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)

Expand Down

0 comments on commit 7527698

Please sign in to comment.