Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minimize to system tray instead of taskbar? #16

Open
EukerionGW2 opened this issue May 10, 2023 · 1 comment
Open

Minimize to system tray instead of taskbar? #16

EukerionGW2 opened this issue May 10, 2023 · 1 comment

Comments

@EukerionGW2
Copy link

Hi Juanma, first of all I'd like to thank you for your great work, it's a really neat script! Congrats!

I'm wondering if it's possible to add a feature (or an option) to the OpenOrShowAppBasedOnWindowTitle function, to minimize said window to the system tray instead of the taskbar? Maybe even with an icon corresponding to the app/window that was minimized?

I tried to adapt your code with the code from FantasticGuru found here but I couldn't make it work properly...

Thanks in advance!
Cheers
Eukerion

@EukerionGW2
Copy link
Author

For future reference, I managed to fiddle with both FG's code and yours (with my very limited knowledge of this particular programming language), and came up with the following, which defines a new function named (with little inspiration) OpenOrUnhideAppBasedOnWindowTitle, with two extra arguments:

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.
#NoTrayIcon
#Persistent
DetectHiddenWindows On

OpenOrUnhideAppBasedOnWindowTitle(WindowTitleWord, AppAddress, InitiallyHide, IconFile)
{
	SetTitleMatchMode, 2
	
    IfWinExist, %WindowTitleWord%
    {
		IfWinActive
		{
			WinHide
			Return
		}
		else
		{
			WinShow
			WinActivate
			Return
		}
	}
    else
    {
        Run, %AppAddress%, UseErrorLevel
		WinGet, Hwnd
		
		WinWait, %WindowTitleWord%
		WinMaximize, %WindowTitleWord%	; maximize window by default after launch
		
		; INITIALIZATION - GUI
		;{-----------------------------------------------
		;
		; Setup Tray Menu
		Menu, Tray, DeleteAll 							; reset menu
		Menu, Tray, NoStandard 							; ?
		Menu, Tray, Icon 								; add icon
		Menu, Tray, Icon, %IconFile% 					; icon location
		Menu, Tray, Add, %WindowTitleWord%, TrayClick	; add menu item and link it to routine TrayClick
		Menu, Tray, Default, %WindowTitleWord%			; set default menu item
		Menu, Tray, Tip, Show/Hide %WindowTitleWord%	; set tooltip string
		Menu, Tray, Click, 1							; number of clicks on icon to show/hide calendar
		;}
		
        If ErrorLevel
        {
            Msgbox, File %AppAddress% Not Found
            Return
        }
		else If %InitiallyHide%
		{
			WinWait, %WindowTitleWord%
			WinHide
			Return
		}
		else
		{
			WinWait, %WindowTitleWord%
			WinActivate
			Return
		}
    }
}

; Show/Hide Program on click
TrayClick(word) {
	SetTitleMatchMode, 2
	
    IfWinExist, %word%
    {
		if DllCall("IsWindowVisible", "UInt", WinExist(word))
		{
			WinHide, %word%
			Return
		}
		else
		{
			WinShow, %word%
			WinActivate
			Return
		}
	}
    else
    {
		Msgbox, Window %word% was closed. Shutting down.
		ExitApp
		Return
	}
Return
}
;}

And then the definition of the hotkey at the end of the program may be done like so, using the example of Google Agenda tied to hotkey F10:

F10:: OpenOrUnhideAppBasedOnWindowTitle("Google Agenda", "chrome.exe --app=https://calendar.google.com/calendar/u/0/r", FALSE, "Path\to\Google Agenda.ico") ; Hotkey
; Note: the last argument is the path to the desired tray icon, or if using a program which embeds its icon (e.g. Notepad.exe) it can be the path/name of the exe.

Of course it is a very rough code that should definitely be refined, but it's a good start and it works as I expect it to.

Cheers
Eukerion

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant