Need script to always start program in system tray

Get help with using AutoHotkey and its commands and hotkeys
noylopa
Posts: 12
Joined: 25 Aug 2014, 08:46

Need script to always start program in system tray

25 Aug 2014, 08:49

I apologize if this is not the right forum. I have a video capture program that I have launched at startup, but I would like to have it minimized to system tray when it launches. The program has no such option in its settings unfortunately. Given how many incredible and complex things autohotkey seems capable of, I figured there is probably a way to do this.

The program is located at -- C:\Program Files (x86)\NCH Software\debut.exe

Sorry for being such a noob and programming moron, I tried but couldn't figure it out myself. Any help would be greatly appreciated -- thanks in advance!!
Coco
Posts: 771
Joined: 29 Sep 2013, 20:37
GitHub: cocobelgica

Re: Need script to always start program in system tray

25 Aug 2014, 10:06

This is possible, using a combination of WinHide/WinShow + Shell_NotifyIcon. Haven't tested this yet but you might have to specify a dummy window (perhaps the script's window) for Shell_NotifyIcon -> script receives notification messages(on icon click, hover etc.) then perform the necessary task on the icon and the target window (video capture program).
noylopa
Posts: 12
Joined: 25 Aug 2014, 08:46

Re: Need script to always start program in system tray

25 Aug 2014, 11:05

Coco wrote:This is possible, using a combination of WinHide/WinShow + Shell_NotifyIcon. Haven't tested this yet but you might have to specify a dummy window (perhaps the script's window) for Shell_NotifyIcon -> script receives notification messages(on icon click, hover etc.) then perform the necessary task on the icon and the target window (video capture program).
Okay -- I tried this, which I got from the WinHide page here: http://www.autohotkey.com/docs/commands/WinHide.htm


Run, debut.exe
WinWait, Debut Video Capture Software
Sleep, 500
WinHide ; use the window found above
Sleep, 1000
WinShow

This does hide the window at first, then the window comes back up and the script is terminated. What am I doing wrong? I want the window to stay hidden until I click on it in the system tray. Is this possible at all?
FanaticGuru
Posts: 1514
Joined: 30 Sep 2013, 22:25

Re: Need script to always start program in system tray

25 Aug 2014, 17:35

noylopa wrote:
Coco wrote:This is possible, using a combination of WinHide/WinShow + Shell_NotifyIcon. Haven't tested this yet but you might have to specify a dummy window (perhaps the script's window) for Shell_NotifyIcon -> script receives notification messages(on icon click, hover etc.) then perform the necessary task on the icon and the target window (video capture program).
Okay -- I tried this, which I got from the WinHide page here: http://www.autohotkey.com/docs/commands/WinHide.htm


Run, debut.exe
WinWait, Debut Video Capture Software
Sleep, 500
WinHide ; use the window found above
Sleep, 1000
WinShow

This does hide the window at first, then the window comes back up and the script is terminated. What am I doing wrong? I want the window to stay hidden until I click on it in the system tray. Is this possible at all?
There could be something odd about your program where it has like a splash screen then another window opens which it forces to the front. I don't know.

But for Notepad as an example you can do this:

Code: Select all

Run, NotePad.exe,,Min
This starts NotePad minimized. Which seems like what you want.

Also it is important to understand that WinHide, hides a window which is different than minimizing it. It is hidden from the user in most normal ways and not just minimized to the taskbar. It is not in the taskbar, tray icons, applications list, etc. It is kind of like a hidden system's file on your computer. A window that the operating system believes a general user should never needs to interact with. You can still see it in the processes list though.

FG
Hotkey Help - Help Dialog for Currently Running AHK Scripts

AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon

[Function] Timer - Create and Manage Timers
ozzii
Posts: 412
Joined: 30 Oct 2013, 06:04

Re: Need script to always start program in system tray

26 Aug 2014, 01:17

Code: Select all

Run, NotePad.exe,,Min
This is minimizing the window but not in the system stray. At least if the app doesn't include this function already (something like 'put in system tray if minimized').
noylopa
Posts: 12
Joined: 25 Aug 2014, 08:46

Re: Need script to always start program in system tray

26 Aug 2014, 08:23

ozzii wrote:

Code: Select all

Run, NotePad.exe,,Min
This is minimizing the window but not in the system stray. At least if the app doesn't include this function already (something like 'put in system tray if minimized').

Debut doesn't have that function, which is why I was asking if maybe there was a way to use AHK to force the app into the system tray when minimized. Minimizing on startup in Windows is easy enough with a properly configured shortcut, but forcing the app into the system tray when minimized requires third-party assistance. I could use something like TrayIt, but I already have AHK setup with some other handy scripts so I thought maybe I could just use AHK for this as well.

But thanks anyway!
Coco
Posts: 771
Joined: 29 Sep 2013, 20:37
GitHub: cocobelgica

Re: Need script to always start program in system tray

26 Aug 2014, 09:28

We can totally forget about Shell_NotifyIcon. The trick is for the script to run your app hidden and then to use the script's tray icon as a dummy to act as the app's icon. You'll basically use the script to launch your app. Below is sample script using notepad, try double clicking on the tray icon -> it'll show/hide notepad:

Code: Select all

#NoTrayIcon
#Persistent

global hNotepad

/* Setup Tray icon and add item that will handle
 * double click events
 */
Menu Tray, Icon
Menu Tray, Icon, notepad.exe
Menu Tray, Add, Show / Hide Notepad, TrayClick
Menu Tray, Default, Show / Hide Notepad

;// Run notepad hidden
DetectHiddenWindows On
Run Notepad,, Hide, PID
WinWait ahk_pid %PID%
hNotepad := WinExist()
DetectHiddenWindows Off
return

TrayClick:
OnTrayClick()
return

;// Show / hide notepad on double click
OnTrayClick() {
	if DllCall("IsWindowVisible", "Ptr", hNotepad) {
		WinHide ahk_id %hNotepad%
	
	} else {
		WinShow ahk_id %hNotepad%
		WinActivate ahk_id %hNotepad%
	}
}
noylopa
Posts: 12
Joined: 25 Aug 2014, 08:46

Re: Need script to always start program in system tray

26 Aug 2014, 10:05

That looks awesome!! Just what I was looking for!
I'm on my work computer and it works perfectly with notepad -- will try this at home tonight with Debut and see if I can match this behavior with that app.

-----

I just tried replicating this script with Calculator on my Win 7 work machine -- just as a test to see if anything else beside the app name needed to be changed-- and I wasn't able to get the tray icon to display.

Here is the script taken from the Notepad script you sent, with 'Notepad' replaced with 'Calculator' wherever possible.


#NoTrayIcon
#Persistent

global hCalculator

/* Setup Tray icon and add item that will handle
* double click events
*/
Menu Tray, Icon
Menu Tray, Icon, calc.exe
Menu Tray, Add, Show / Hide Calculator, TrayClick
Menu Tray, Default, Show / Hide Calculator

;// Run calculator hidden
DetectHiddenWindows On
Run Calculator,, Hide, PID
WinWait ahk_pid %PID%
hCalculator := WinExist()
DetectHiddenWindows Off
return

TrayClick:
OnTrayClick()
return

;// Show / hide calculator on double click
OnTrayClick() {
if DllCall("IsWindowVisible", "Ptr", hCalculator) {
WinHide ahk_id %hCalculator%

} else {
WinShow ahk_id %hCalculator%
WinActivate ahk_id %hCalculator%
}
}



When I tried this I got this error -- see attached.


Any ideas?
Attachments
calculator ahk error.jpg
Coco
Posts: 771
Joined: 29 Sep 2013, 20:37
GitHub: cocobelgica

Re: Need script to always start program in system tray

26 Aug 2014, 11:18

Try replacing Run Calculator with Run calc.exe
noylopa
Posts: 12
Joined: 25 Aug 2014, 08:46

Re: Need script to always start program in system tray

26 Aug 2014, 11:32

Coco wrote:Try replacing Run Calculator with Run calc.exe
That did it!!
I'm so gonna make this work at home -- thank you!!
FanaticGuru
Posts: 1514
Joined: 30 Sep 2013, 22:25

Re: Need script to always start program in system tray

26 Aug 2014, 12:37

Coco wrote:We can totally forget about Shell_NotifyIcon. The trick is for the script to run your app hidden and then to use the script's tray icon as a dummy to act as the app's icon. You'll basically use the script to launch your app. Below is sample script using notepad, try double clicking on the tray icon -> it'll show/hide notepad:

Code: Select all

#NoTrayIcon
#Persistent

global hNotepad

/* Setup Tray icon and add item that will handle
 * double click events
 */
Menu Tray, Icon
Menu Tray, Icon, notepad.exe
Menu Tray, Add, Show / Hide Notepad, TrayClick
Menu Tray, Default, Show / Hide Notepad

;// Run notepad hidden
DetectHiddenWindows On
Run Notepad,, Hide, PID
WinWait ahk_pid %PID%
hNotepad := WinExist()
DetectHiddenWindows Off
return

TrayClick:
OnTrayClick()
return

;// Show / hide notepad on double click
OnTrayClick() {
	if DllCall("IsWindowVisible", "Ptr", hNotepad) {
		WinHide ahk_id %hNotepad%
	
	} else {
		WinShow ahk_id %hNotepad%
		WinActivate ahk_id %hNotepad%
	}
}
This is an interesting idea (run a regular program hidden, with a tray icon to toggle it hidden and unhidden). I don't have a use for it at the moment but it seems like something that could be useful in the future if I had a script to do it easily.

I believe I will fool around with rewriting this to be more general. Maybe a script that uses command line parameters to run other programs in hidden mode.

Thanks for the idea Coco.

FG
Hotkey Help - Help Dialog for Currently Running AHK Scripts

AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon

[Function] Timer - Create and Manage Timers
ozzii
Posts: 412
Joined: 30 Oct 2013, 06:04

Re: Need script to always start program in system tray

27 Aug 2014, 02:40

Like you said FanaticGuru this is an interesting idea.
I will keep this for a later use.
Thanks Coco
ozzii
Posts: 412
Joined: 30 Oct 2013, 06:04

Re: Need script to always start program in system tray

27 Aug 2014, 07:00

Problem with this:
When you first start the script it's ok.
But if you close notepad, the icon stay on the tray witout any possibility to do anything with it.
Coco
Posts: 771
Joined: 29 Sep 2013, 20:37
GitHub: cocobelgica

Re: Need script to always start program in system tray

27 Aug 2014, 07:23

You can use WinWaitClose to detect this and automatically quit the script

Code: Select all

#NoTrayIcon
#Persistent

global hNotepad

/* Setup Tray icon and add item that will handle
 * double click events
 */
Menu Tray, Icon
Menu Tray, Icon, notepad.exe
Menu Tray, Add, Show / Hide Notepad, TrayClick
Menu Tray, Default, Show / Hide Notepad

;// Run notepad hidden
DetectHiddenWindows On
Run Notepad,, Hide, PID
WinWait ahk_pid %PID%
hNotepad := WinExist()
WinWaitClose ahk_id %hNotepad%
MsgBox,,, Notepad was closed. Quitting script.., 1
ExitApp

TrayClick:
OnTrayClick()
return

;// Show / hide notepad on double click
OnTrayClick() {
    if DllCall("IsWindowVisible", "Ptr", hNotepad) {
        WinHide ahk_id %hNotepad%
    
    } else {
        WinShow ahk_id %hNotepad%
        WinActivate ahk_id %hNotepad%
    }
}
Coco
Posts: 771
Joined: 29 Sep 2013, 20:37
GitHub: cocobelgica

Re: Need script to always start program in system tray

27 Aug 2014, 07:33

@ozzii, regarding the Notepad example, you can further extend it by:
  • Adding context sensitive hotkeys/hotstrings to provide simple auto-complete for notepad
  • Run AHK code entered in notepad (using DynaRun())
  • Add custom items to the tray menu
  • etc..
Of course you can write your own carry-anywhere quick AHK/Text editor using AHK's GUI commands but you would have to take care of file opens/saves, word wrapping, font dialog etc. too much hassle. You can also use the portable version of your fave editor. However this alternative is fat-free, I'm leaning towards to actually using this. :-). My idea on the setup is to copy AutoHotkey.exe and rename it to this_script_name.exe, put them both in the same directory (e.g. flash drive etc.). In that way I have a copy of AutoHotkey to run scripts and I can just double click the renamed exe to run this script...
Last edited by Coco on 27 Aug 2014, 08:11, edited 1 time in total.
User avatar
joedf
Posts: 7958
Joined: 29 Sep 2013, 17:08
Facebook: J0EDF
Google: +joedf
GitHub: joedf
Location: Canada
Contact:

Re: Need script to always start program in system tray

27 Aug 2014, 07:48

Haha Very interesting examples, i see here!
FanaticGuru
Posts: 1514
Joined: 30 Sep 2013, 22:25

Re: Need script to always start program in system tray

28 Aug 2014, 19:27

Here is my generic version that uses command parameters.

RunHidden

Code: Select all

; RunHidden
; Fanatic Guru
; 2014 08 28
; Version: 1.0
;
; Script derived from and inspired by Coco at http://ahkscript.org/boards/viewtopic.php?f=5&t=4373
;
; Run program with hidden window
; Program can be toggled between hidden and shown by tray menu icon
;
;{-----------------------------------------------
; Designed to have path and/or name of program passed as a command parameter
;   Example for Notepad.exe
;     Create shortcut to RunHidden script
;     In properties of shortcut put Notepad.exe at very end of Target field as a command parameter
;       "D:\Users\Guru\Documents\AutoHotkey\My Scripts\RunHidden.ahk" Notepad.exe
;     The program to run in hidden mode could include the entire path in the command parameter if needed
;       "D:\Users\Guru\Documents\AutoHotkey\My Scripts\RunHidden.ahk" C:\Windows\System32\Notepad.exe
; The script gets the file title out of the full path for display purposes in the menu tray
;}

; INITIALIZATION - ENVIROMENT
;{-----------------------------------------------
;
#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
;}

; INITIALIZATION - VARIABLES
;{-----------------------------------------------
;
Program = %1%
SplitPath, Program,,,,Title
;}

; INITIALIZATION - GUI
;{-----------------------------------------------
;
; Setup Tray Menu
Menu Tray, DeleteAll
Menu Tray, NoStandard
Menu Tray, Icon
Menu Tray, Icon, %Program%
Menu Tray, Add, Show %Title%, TrayClick
Menu Tray, Default, Show %Title%
Menu Tray, Tip, %Title%
;}

; AUTO-EXECUTE
;{-----------------------------------------------
;
; Run Program hidden
Run %Program%,, Hide, PID
WinWait ahk_pid %PID%
Hwnd := WinExist()
WinWaitClose ahk_id %Hwnd%
ExitApp
;
;}-----------------------------------------------
; END OF AUTO-EXECUTE

; SUBROUTINES - GUI
;{-----------------------------------------------
;
; Show / Hide Program on Doubleclick
TrayClick:
    if DllCall("IsWindowVisible", "Ptr", Hwnd) 
    {
        Menu Tray, DeleteAll
        Menu Tray, Add, Show %Title%, TrayClick
        Menu Tray, Default, Show %Title%
        WinHide ahk_id %Hwnd%
    } 
    else 
    {
        Menu Tray, DeleteAll
        Menu Tray, Add, Hide %Title%, TrayClick
        Menu Tray, Default, Hide %Title%
        WinShow ahk_id %Hwnd%
        WinActivate ahk_id %Hwnd%
    }
return
;}
FG
Hotkey Help - Help Dialog for Currently Running AHK Scripts

AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon

[Function] Timer - Create and Manage Timers

Return to “Ask For Help”

Who is online

Users browsing this forum: alesyt0h, autohotkeycool, Bing [Bot], Black-Phoenix, Google [Bot], HyperLexus, sanmaodo and 61 guests