{"id":7386,"date":"2021-10-14T07:36:36","date_gmt":"2021-10-14T07:36:36","guid":{"rendered":"https:\/\/max-drake.cc\/?p=7386"},"modified":"2021-10-14T07:36:41","modified_gmt":"2021-10-14T07:36:41","slug":"maxlauncher-how-to-close-programs","status":"publish","type":"post","link":"https:\/\/max-drake.cc\/?p=7386","title":{"rendered":"MaxLauncher- How to Close programs"},"content":{"rendered":"\n<p>I was really exited when I found this program launcher as it has the same interface as my Stream Deck. So COOL!!!<\/p>\n\n\n\n<p>But, with stream deck I can close programs as well with hotkeys assigned to a button. That is great as I don&#8217;t have to remember the hotkey, all I have to do is hit the button. <\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Hotkeys to PC<\/h3>\n\n\n\n<p>If you are on desktop or in any program you can invoke a AutoHotKey script with a hotkey. <\/p>\n\n\n\n<p>So I thought if I could programmatically send a hotkey by calling a hotkey from a MaxLauncher(ML) button then I could make a hotkey that would target an ExitApp of a script I had running. So the ML button would point to a script that only had say   <strong>send, ^3<\/strong>  and that Ctrl+3 hotkey would work. <\/p>\n\n\n\n<p>Not so simple it seems. <\/p>\n\n\n\n<p>The area of particular interest to me is in the Browser. On  my Stream Deck I have a Close Tab button that I use all the time, its really handy as you don&#8217;t have to go to the tab and click on the x to close it, but only have the tab active, then hit the button, the key is Ctrl+W, a Firefox Shortcut key. <\/p>\n\n\n\n<p>So my main focus was on getting this to work as its such a handy button. Yes, you could just remember Ctrl+W but I have found the button so handy as its next to all the common web pages I use, so in the middle of the day I&#8217;d check  Tides &amp; weather just when I was thinking of taking the dog for a walk and its easier just to hit the close tab on the fly without using the mouse. <\/p>\n\n\n\n<p>So the focus was on assigning a hotkey to a ML button. <\/p>\n\n\n\n<h4 class=\"wp-block-heading\">ML target argument<\/h4>\n\n\n\n<p>I had tried adding an argument to the Target program, so target AHK Command Picker and then using the close hotkey for that program, it seemed to work first time but subsequently would not, and if it did then it was erratic. <\/p>\n\n\n\n<p>Also that wouldn&#8217;t work for the browser. <\/p>\n\n\n\n<h4 class=\"wp-block-heading\">AutoHotKey 1st attempt<\/h4>\n\n\n\n<p>So, first I tried AutoHotKey to write <strong>send ^3<\/strong> in a AutoHotKey script assigned to a key, that didn&#8217;t work. I tried the SendRaw, SendInput and all the other variations of this method that were on the send page. <\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Bat &amp; vba <\/h4>\n\n\n\n<p>Then .Bat file but that goes into a control console, so that didn&#8217;t work<\/p>\n\n\n\n<p>next with .vba script called by .bat and that didn&#8217;t work either<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Python<\/h4>\n\n\n\n<p>next with python script, after loading latest version (3.9) and using &#8220;<strong>keyboard<\/strong>&#8221; and &#8220;<strong>pyautogui&#8221;<\/strong> libraries, that didn&#8217;t work either<\/p>\n\n\n\n<p>All of them seemed to work initially then they didn&#8217;t, I&#8217;m wondering if there is a buffer that gets filled and then doesn&#8217;t flush? Not sure as it works all the time on the stream deck. <\/p>\n\n\n\n<p>Fun to be playing with Python again. I suppose its like AHK, the script will run if you have python  running in the background as AHK scripts have AutoHotKey.exe running in the background. <\/p>\n\n\n\n<p>That was what put me off python before, you couldn&#8217;t make a stand alone .exe file to pass around (actually you can, but when I tried it it wasn&#8217;t amazingly successful). <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import pyautogui\n\npyautogui.hotkey('ctrl', 'w')\nquit()\n\n#or with keyboard library\nimport keyboard\nkeyboard.send('ctrl+w')\nexit\n\n#Neither work consistently<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">PowerShell to Computer HotKeys<\/h4>\n\n\n\n<p>I also got some PowerShell  scripts to work a couple of times, then they stopped working. I suppose a bit liker .BAT being the old Dos format (that&#8217;s what I played with years ago, pre windows). I just found some of these this morning. A bit like Python &amp; AutoHotKey. They start to work and then stop working. <\/p>\n\n\n\n<p>Interestingly, these worked in the basic computer, so I&#8217;d have an script open and its close hotkey is Alt8 and it worked. I started a script and closed it by calling the PowerShell script. But it didn&#8217;t seem to work with Ctrl+W for tabs in firefox browser. I even found a script for running in a browser (https:\/\/sumtips.com\/software\/browsers\/automatically-cycle-through-tabs-in-any-browser\/) but replacing firefox for opera and the sendkey didn&#8217;t seem to work. <\/p>\n\n\n\n<p>There is no &#8220;win key&#8221;, just shift + escape, which does the same thing.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">SendKeys.Send(\"^{ESC}\")<\/pre>\n\n\n\n<p>Another issue is I can get script running in VS Code but not as a run on the file, apparently this is to do with permissions of PowerShell. To resove this you have to start PowerShell in admin mode and choose one of the following after typing in    PowerShell > <strong>Set-ExecutionPolicy <\/strong><\/p>\n\n\n\n<p><strong>Set-ExecutionPolicy<\/strong> : Cannot bind parameter &#8216;ExecutionPolicy&#8217;. Cannot convert value &#8220;&#8221; to type<br>&#8220;Microsoft.PowerShell.ExecutionPolicy&#8221;. Error: &#8220;The identifier name cannot be processed because it is either too<br>similar or identical to the following enumerator names:<span style=\"background-color:#e69d69\" class=\"background-color\"><strong> <\/strong><\/span><strong><span style=\"background-color:#ff6900\" class=\"background-color\"><\/span><\/strong><span style=\"background-color:#e69d69\" class=\"background-color\"><strong>Unrestricted, RemoteSigned, AllSigned, Restricted, Default,<br>Bypass, Undefined.<\/strong> Use a more specific identifier name.&#8221;<\/span><\/p>\n\n\n\n<p>In looking at Powershell><strong>Get-Process <\/strong><\/p>\n\n\n\n<p>You see that all the AutoHotKey scripts are listed as AutoHotKey with different PID&#8217;s that you need to target (see below) so for testing use an .EXE file as it comes up with its individual name. I was testing this with TimeCapture++.exe for closing the program with Alt+8 script hotkey and it worked fine and consistently. <\/p>\n\n\n\n<p>In  In looking at Powershell><strong>Get-Process <\/strong>  AutoHotKey scripts are unidentified, even if you go into Task Manager and look at the details tab it only shows you that there are a number of instances of AutoHotKey running and it doesn&#8217;t tell you which is which, although they do have different ID&#8217;s. <\/p>\n\n\n\n<p>In AutoHotKey there is WinGet that will tell you the tiltle of each instance and its PID , but that is only in AutoHotKey. <\/p>\n\n\n\n<p>So from PowerShell point of view, it doesn&#8217;t work with AutoHotKey unless the scripts are complied into .EXE files, then you can target with  Powershell><strong>Get-Process <\/strong> <\/p>\n\n\n\n<h3 class=\"wp-block-heading\">End result of 3 days effort<\/h3>\n\n\n\n<p>Although I was persistent, I didn&#8217;t seem to get much closer to getting this to work, the hotkeys all ended up in the ether, they may work one or 2 times and then they wouldn&#8217;t. <\/p>\n\n\n\n<p>All of the above programs are pretty powerful and should be able to do something with sending keys to the computer, but all didn&#8217;t. I have yet to figure out why that is. <\/p>\n\n\n\n<p>It was bugging me that I couldn&#8217;t get this working <\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"996\" height=\"605\" data-src=\"https:\/\/max-drake.cc\/wp-content\/uploads\/2021\/10\/2021-10-14-15_07_17-MaxLauncher-C__Users_Max_Documents_MaxLauncher_Main.mld_.jpg\" alt=\"\" class=\"wp-image-7391 lazyload\" data-srcset=\"https:\/\/max-drake.cc\/wp-content\/uploads\/2021\/10\/2021-10-14-15_07_17-MaxLauncher-C__Users_Max_Documents_MaxLauncher_Main.mld_.jpg 996w, https:\/\/max-drake.cc\/wp-content\/uploads\/2021\/10\/2021-10-14-15_07_17-MaxLauncher-C__Users_Max_Documents_MaxLauncher_Main.mld_-300x182.jpg 300w, https:\/\/max-drake.cc\/wp-content\/uploads\/2021\/10\/2021-10-14-15_07_17-MaxLauncher-C__Users_Max_Documents_MaxLauncher_Main.mld_-768x467.jpg 768w, https:\/\/max-drake.cc\/wp-content\/uploads\/2021\/10\/2021-10-14-15_07_17-MaxLauncher-C__Users_Max_Documents_MaxLauncher_Main.mld_-50x30.jpg 50w, https:\/\/max-drake.cc\/wp-content\/uploads\/2021\/10\/2021-10-14-15_07_17-MaxLauncher-C__Users_Max_Documents_MaxLauncher_Main.mld_-82x50.jpg 82w, https:\/\/max-drake.cc\/wp-content\/uploads\/2021\/10\/2021-10-14-15_07_17-MaxLauncher-C__Users_Max_Documents_MaxLauncher_Main.mld_-100x61.jpg 100w, https:\/\/max-drake.cc\/wp-content\/uploads\/2021\/10\/2021-10-14-15_07_17-MaxLauncher-C__Users_Max_Documents_MaxLauncher_Main.mld_-165x100.jpg 165w, https:\/\/max-drake.cc\/wp-content\/uploads\/2021\/10\/2021-10-14-15_07_17-MaxLauncher-C__Users_Max_Documents_MaxLauncher_Main.mld_-640x389.jpg 640w\" data-sizes=\"(max-width: 996px) 100vw, 996px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 996px; --smush-placeholder-aspect-ratio: 996\/605;\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">Targeting the program and then sending input to the program- Finally Close Tab works<\/h3>\n\n\n\n<p>After thinking about it a bit more, I decided that I needed to focus on having the program activated prior to running the hotkey. I found this article on Reddit (https:\/\/www.reddit.com\/r\/AutoHotkey\/comments\/eo43tr\/sending_keys_to_firefox_not_having_any_effect\/) that pointed the way forward. <\/p>\n\n\n\n<p>First you need to activate the program window then use <strong>ControlSend<\/strong> instead of send and it works. So I wrote a script that does that and Hurray Hurray I can close tabs with MaxLauncher. <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>; https:\/\/www.reddit.com\/r\/AutoHotkey\/comments\/eo43tr\/sending_keys_to_firefox_not_having_any_effect\/\n\nWinActivate   , Mozilla Firefox\t\t\t\t\t\t;activate FF    \n\nSetKeyDelay, 10, 10   ; this seems improtant so typing not too fast\nControlSend, ahk_parent, ^w\t, ahk_class MozillaWindowClass    ;close tab\n\nsend, ^W<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"618\" height=\"391\" data-src=\"https:\/\/max-drake.cc\/wp-content\/uploads\/2021\/10\/2021-10-14-15_35_16-MaxLauncher-C__Users_Max_Documents_MaxLauncher_Main.mld_.jpg\" alt=\"\" class=\"wp-image-7393 lazyload\" data-srcset=\"https:\/\/max-drake.cc\/wp-content\/uploads\/2021\/10\/2021-10-14-15_35_16-MaxLauncher-C__Users_Max_Documents_MaxLauncher_Main.mld_.jpg 618w, https:\/\/max-drake.cc\/wp-content\/uploads\/2021\/10\/2021-10-14-15_35_16-MaxLauncher-C__Users_Max_Documents_MaxLauncher_Main.mld_-300x190.jpg 300w, https:\/\/max-drake.cc\/wp-content\/uploads\/2021\/10\/2021-10-14-15_35_16-MaxLauncher-C__Users_Max_Documents_MaxLauncher_Main.mld_-50x32.jpg 50w, https:\/\/max-drake.cc\/wp-content\/uploads\/2021\/10\/2021-10-14-15_35_16-MaxLauncher-C__Users_Max_Documents_MaxLauncher_Main.mld_-79x50.jpg 79w, https:\/\/max-drake.cc\/wp-content\/uploads\/2021\/10\/2021-10-14-15_35_16-MaxLauncher-C__Users_Max_Documents_MaxLauncher_Main.mld_-100x63.jpg 100w, https:\/\/max-drake.cc\/wp-content\/uploads\/2021\/10\/2021-10-14-15_35_16-MaxLauncher-C__Users_Max_Documents_MaxLauncher_Main.mld_-158x100.jpg 158w\" data-sizes=\"(max-width: 618px) 100vw, 618px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 618px; --smush-placeholder-aspect-ratio: 618\/391;\" \/><\/figure>\n\n\n\n<h4 class=\"wp-block-heading\">Stop Scripts for restart after edit<\/h4>\n\n\n\n<p>I&#8217;m editing scripts, mainly tweeking them, and I can do this by opening Notepad++ and having the argument as the file I want to edit. I can set this up for autocorrect, CommandPcker &amp; EmailTap scripts. After editing I want to reload the scripts so that I can use the updated version. <\/p>\n\n\n\n<p>I found there was a method using AutoHotKey process but that only allowed you to close the program, not restart it, but that said, having a start script button is no issue as I&#8217;ve lots of keys on MaxLauncher at the moment, so I can assign a key to start the program too. Maybe later I can just include a restart in a bat file so that you can chose to close or close then start the program (maybe worth exploring later) so that you can fit it onto one button.  <\/p>\n\n\n\n<p><strong>An Aside- AutoCorrect has a hotkey for adding a new single text expander line<\/strong>  Win5 or CtrlShift5<\/p>\n\n\n\n<p>I&#8217;ve since found out that AutoCorrect has a hotkey for adding a new single text expander line and it restarts script automatically. So not so much use for the Editing in Notepad++ as its so much easier, I just need to make a key so I don&#8217;t have to remember it, also I think it works if you select a bit of text first. <\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Get PID of script to then action<\/h4>\n\n\n\n<p>Finding an active Script to then stop was a bit of a challenge. The reason being each time the script is started it has a new ProcessID (PID) so you have to find its number so you can action it. Also if you have a couple of instances open (in AutoHotKey scripts you can put <strong>#SingleInstance<\/strong> to stop this from happening)  but for programs like file Explorer you can have multiple instances each with a different window id, but all having the same Process ID. <\/p>\n\n\n\n<p>Anyway, to target the scripts, which are running in the background, so do not have an active window you need to loop through all of the AutoHotKey open scripts to find its individual PID, and once you have this you can then close that process.  <strong>The script is below ONLY STOPS A SCRIPT<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>DetectHiddenWindows, ON\nSetTitleMatchMode, 2\n\n;find the script that you want to close\nProcName = AHKCommandPicker.ahk  ;EmailDateMultiTap.ahk   ;AutoCorrect.ahk ;TimeCapture++.exe\n\n;get the id of the script\nWinGet, id, List, %ProcName% \n\n;find the PID of the script (this is the new active process)\n; each time a new instance is started it has  a different PID\nLoop, %id%\n{\n    this_id := id%A_Index%\n    WinActivate, ahk_id %this_id%\n    WinGet, PID, PID, ahk_id %this_id%    \n}\n\n;Use the PID as the ID of the instance  and close it\n  Process, Close,  %PID%\n \nexitapp\n<\/code><\/pre>\n\n\n\n<p>Enlarging on the above, we can use the PID to activate the identified window and then can just use <strong>send,  #!XYZ<\/strong> to that program , thereby triggering the hotkeys in that script, thusly: <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>DetectHiddenWindows, ON\n;SetTitleMatchMode, 2\n\n;find the script that you want to close\nProcName = AutoCorrect.ahk ;TimeCapture++.exe; AHKCommandPicker.ahk  ;EmailDateMultiTap.ahk   ;\n\n;get the id of the script\nWinGet, id, List, %ProcName% \n\n;find the PID of the script (this is the new active process)\n; each time a new instance is started it has  a different PID\nLoop, %id%\n{\n    this_id := id%A_Index%\n    WinActivate, ahk_id %this_id%\n    WinGet, PID, PID, ahk_id %this_id%    \n}\n\nWinActivate, ahk_id %PID%\n;Use the PID as the ID of the instance  and close it\nSend,  ^!5\t<\/code><\/pre>\n\n\n\n<p>The above script gets a Hidden Window and uses <strong> WinActivate, <span style=\"background-color:#8ed1fc\" class=\"background-color\">ahk_id <\/span>%PID% <\/strong> so its looking for <strong>ahk_id<\/strong> instead of the usual <strong>ahk_class<\/strong> which I would normally use. <\/p>\n\n\n\n<p>So after messing around with PowerShell most of the day AutoHotKey has been the main solution for all the actions I want so far. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Lessons learned (painfully)<\/h2>\n\n\n\n<ol class=\"wp-block-list\"><li>Focus on an active window of a program before sending hotkeys. Sending them into the ether doesn&#8217;t seem to work<\/li><li>For AutoHotKey scripts, which are all bundled together, you need to find the PID (Process ID) (https:\/\/documentation.help\/AutoHotkey-GUI\/WinTitle.htm#ahk_id) and then use that to active the window, especially if it&#8217;s a hidden script sitting in your Task Bar Tray. <\/li><li>After going around the houses with Bat, VBA, python &amp; PowerShell, AutoHotKey seems to be able to meet all my needs<\/li><li>I do not need to break all the scripts down into individual functions but can keep the running script and make Hotkey calls to that. Far tidier. <\/li><\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">End comment<\/h2>\n\n\n\n<p>This has been a painful journey and not that much learning along the way. I&#8217;m glad I have the basic function buttons for Firefox tab manipulation up and running and also AutoHotKey scripts controllable using Hotkey calls to running scripts. That simplifies using my existing code. <\/p>\n\n\n\n<p>Now I have the main rudementary buttons that I use most of the time on my  Stream Deck I can now start looking at other keys that I use on that too. Another post for that. <\/p>\n\n\n\n\n\n\n\n\n","protected":false},"excerpt":{"rendered":"<p>I was really exited when I found this program launcher as it has the same interface as my Stream Deck. So COOL!!! But, with stream deck I can close programs as well with hotkeys assigned to a button. That is great as I don&#8217;t have to remember the hotkey, all I have to do is [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":7393,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[466,204,42],"tags":[],"class_list":["post-7386","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-autohotkey","category-automation","category-productivity"],"featured_image_src":"https:\/\/max-drake.cc\/wp-content\/uploads\/2021\/10\/2021-10-14-15_35_16-MaxLauncher-C__Users_Max_Documents_MaxLauncher_Main.mld_-600x400.jpg","featured_image_src_square":"https:\/\/max-drake.cc\/wp-content\/uploads\/2021\/10\/2021-10-14-15_35_16-MaxLauncher-C__Users_Max_Documents_MaxLauncher_Main.mld_-600x600.jpg","author_info":{"display_name":"Max Drake","author_link":"https:\/\/max-drake.cc\/?author=1"},"_links":{"self":[{"href":"https:\/\/max-drake.cc\/index.php?rest_route=\/wp\/v2\/posts\/7386","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/max-drake.cc\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/max-drake.cc\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/max-drake.cc\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/max-drake.cc\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=7386"}],"version-history":[{"count":0,"href":"https:\/\/max-drake.cc\/index.php?rest_route=\/wp\/v2\/posts\/7386\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/max-drake.cc\/index.php?rest_route=\/wp\/v2\/media\/7393"}],"wp:attachment":[{"href":"https:\/\/max-drake.cc\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=7386"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/max-drake.cc\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=7386"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/max-drake.cc\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=7386"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}