Posts: 9
Threads: 5
Joined: Jun 2018
06-17-2018, 05:47 AM
(This post was last modified: 06-18-2018, 05:54 AM by Gintaras.)
Hello, I need to restart an application running at background in regular intervals (or at predefined times of the day), but the application's window is usually not visible. It can only be exited from rightclick menu of status bar icon (always present while running). Which QM command can locate the process and simulate the taskbar context menu action? I would like to avoid simulating the quit by mouse emulation and rather use a WM or QM command to signal the app to close completely (not hide the main window).
Secondly, how to wait till the process finishes it's shutdown so that I can relaunch it?
Thank you for the solution
Posts: 1,000
Threads: 253
Joined: Feb 2008
06-17-2018, 02:25 PM
(This post was last modified: 06-17-2018, 02:38 PM by TheVig.)
http://www.quickmacros.com/forum/showthr...rocessesEx
Function RestartProcessNotepad
str ProcessName="notepad"
run F"{ProcessName}.exe"
int pid=ProcessNameToId(ProcessName)
if pid
,2
,ShutDownProcess pid 1 ;; or clo "process window"
,rep() 0.1; if(!ProcessNameToId("ProcessName")) break ;;waits
,mes "CLOSED, Click OK to relaunch"
,run F"{ProcessName}.exe"
You'll have to figure out what your process name is for the program. Use Task Manager for that.
Function
CreateScheduledTaskNotepad
ScheduleMacro "RestartProcessNotepad" "17:00" "" "" 0 1 ;;run every day at 5p
Above uses QM to create the task in Windows Task Scheduler.
Posts: 9
Threads: 5
Joined: Jun 2018
06-17-2018, 03:22 PM
(This post was last modified: 06-17-2018, 03:24 PM by Ethereal.)
It almost works but I have two problems:
1) ShutDownProcess kills the process without doing cleanup (orphan systray icon left)
I would prefer to shutdown the process regularly, ie. by simulating the Quit command from systray icon context menu
Probably it is done by PostMessage hwnd WM_COMMAND ? ?
Now I need to know how can I trigger which WM params are used on Quit menu item invoke
2) siz command isnot working tho hwnd has apaprently valid value and the window is visible
int pid = ProcessNameToId("pushbullet_client")
if pid
ShutDownProcess pid 1
rep() 0.1; if(!ProcessNameToId("pushbullet_client")) break ;;waits
run "C:\Program Files (x86)\Pushbullet\pushbullet.exe"
wait 0 WA "Pushbullet"
int hwnd = win("Pushbullet" "HwndWrapper[pushbullet_client.exe;*" "pushbullet_client")
if hwnd
siz 800 1100 hwnd 3
clo hwnd
Posts: 12,073
Threads: 140
Joined: Dec 2002
Quote:ShutDownProcess kills the process without doing cleanup (orphan systray icon left)
http://www.quickmacros.com/forum/showthr...4#pid31254
Quote:Probably it is done by PostMessage hwnd WM_COMMAND ?
Only if it is a classic menu, class name #32768. The code can be recorded.
Example 1 - close Task Manager through its tray icon, using WM_COMMAND.
Step 1 - record. Recorded code:
#region Recorded 2018-06-18 08:17:15
rig 1734 15 win("" "Shell_TrayWnd") 1 ;;tool bar 'User Promoted Notification ...', push button 'CPU 5%[]Memory 61%[]Disk 0%...'
lef 36 14 wait(15 WV win("" "#32768")) 1 ;;menu item 'Close'
;men 30435 win("" "TrayiconMessageWindow") ;;Close
#endregion
Step 2 - uncomment the men line and remove other code.
men 30435 win("" "TrayiconMessageWindow") ;;Close
---------------------
Else use accessible object functions. To create code use dialogs from the floating toolbar, button "Windows controls".
Example 2 - close Task Manager through its tray icon, using accessible objects.
;find tray icon using its tooltip text as wildcard
int w=win("" "Shell_TrayWnd")
Acc a.Find(w "PUSHBUTTON" "CPU *Memory *" "class=ToolbarWindow32[]id=1504" 0x1005) ;;Tested on Win10; else may be different tooltip text, id, etc.
;right-click the tray icon to show context menu
a.Mouse(2); mou
;sub.AccPostRightClickMessage a ;;or try this. Does not move the mous pointer, but may not work with some tray icons or on some Windows versions. Tested on Win10.
;wait for context menu
int w1=wait(5 WV win("" "#32768"))
;find the Close item and click
Acc a1.Find(w1 "MENUITEM" "Close" "" 0x1001)
a1.DoDefaultAction
#sub AccPostRightClickMessage
function Acc&a
int x y
a.Location(x y)
x+2; y+2
int w3=child(a)
ScreenToClient w3 +&x
int lParam=MakeInt(x y)
PostMessage w3 WM_RBUTTONDOWN MK_RBUTTON lParam
PostMessage w3 WM_RBUTTONUP 0 lParam