Jump to content
Macro Express Forums

Experiment that turned out useful


Recommended Posts

OK, nothing earthshaking here, but this project was kind of fun.  I have a hot key macro that closes whatever application/window is focused, by entering keystrokes <ALTD><SPACE>c<ALTU>.  If Firefox or Chrome is focused with one or more tabs open, it types <CTRLD>w<CTRLU> instead, to close the current tab.  Anyhow, I was frustrated because many Windows 10 applications block ME from seeing the hot key, so the "close application" macro doesn't run when I would like it to.  

 

The macro below runs in a continuous loop, sampling the mouse location frequently.  If I flick the mouse to the right, then back to the left, the macro recognizes that pattern and runs the "close application" macro.  Seems to work fine in Windows 10 screens where hotkeys are blocked.  The macro is handier than I thought it would be -- I seldom reach for my hot key any more.  I'm still tinkering with the timing and distance of the mouse moves, to avoid accidentally reacting to normal mouse movements.  Also there are a few other macros that have been caught because they themselves do mouse movements that fit the pattern -- still weeding out a few of those.  The macro running continuously uses about six percent of the CPU, but the darned processor is so fast it's just loafing most of the time anyway.  

 

// Track mouse, cancel application for rapid movement left to right and back
// This macro is intended to run virtually all the time, in a continuous loop
//  
Variable Set Integer %movement% to 150 // Set minimum distance that qualifies as a mouse movement

//  
Variable Set String %repeater% to "0"
Repeat Until %repeater% Equals "1"
  If Window "Program Manager" is focused
    Delay: 1000 milliseconds, without ability to halt
  Else
  // Clear previous mouse positions
    Variable Set Integer %left1% to 0
    Variable Set Integer %left2% to 0
    Variable Set Integer %right1% to 0
    Variable Set Integer %spanright1% to 0
    Variable Set Integer %spanleft1% to 0
    // Check for first right/left movement
    Get Mouse Position into (%left1%, ypos) Relative to Screen
    Delay: 150 milliseconds, without ability to halt
    Get Mouse Position into (%right1%, ypos) Relative to Screen
    Variable Modify Integer: %spanright1% = %right1% - %left1%
    If Variable %spanright1% Is Greater Than "%movement%" // Did mouse move right?
      Delay: 400 milliseconds, without ability to halt
      Get Mouse Position into (%left2%, ypos) Relative to Screen
      Variable Modify Integer: %spanleft1% = %right1% - %left2%
      If Variable %spanleft1% Is Greater Than "%movement%" // Did mouse move left?
         Macro Run: 1_Close_Application_or_Browser_Window
      End If
    End If
    //  
  End If
End Repeat
//  
Macro Return
//  

 


<COMMENT Value="Track mouse, cancel application for rapid movement left to right and back"/>
<COMMENT Value="This macro is intended to run virtually all the time, in a continuous loop"/>
<COMMENT Value=" "/>
<VARIABLE SET INTEGER Option="\x00" Destination="%movement%" Value="150" _COMMENT="Set minimum distance that qualifies as a mouse movement\r\n"/>
<COMMENT Value=" "/>
<VARIABLE SET STRING Option="\x00" Destination="%repeater%" Value="0" NoEmbeddedVars="FALSE"/>
<REPEAT UNTIL Variable="%repeater%" Condition="\x00" Value="1"/>
<IF WINDOW Option="\x00" Title="Program Manager" Partial="TRUE" Wildcards="FALSE"/>
<DELAY Flags="\x13" Time="1000"/>
<ELSE/>
<COMMENT Value="Clear previous mouse positions "/>
<VARIABLE SET INTEGER Option="\x00" Destination="%left1%" Value="0"/>
<VARIABLE SET INTEGER Option="\x00" Destination="%left2%" Value="0"/>
<VARIABLE SET INTEGER Option="\x00" Destination="%right1%" Value="0"/>
<VARIABLE SET INTEGER Option="\x00" Destination="%spanright1%" Value="0"/>
<VARIABLE SET INTEGER Option="\x00" Destination="%spanleft1%" Value="0"/>
<COMMENT Value="Check for first right/left movement "/>
<GET MOUSE POSITION Option="\x00" X="%left1%" Y="ypos"/>
<DELAY Flags="\x13" Time="150"/>
<GET MOUSE POSITION Option="\x00" X="%right1%" Y="ypos"/>
<VARIABLE MODIFY INTEGER Option="\x01" Destination="%spanright1%" Value1="%right1%" Value2="%left1%"/>
<IF VARIABLE Variable="%spanright1%" Condition="\x03" Value="%movement%" IgnoreCase="FALSE" _COMMENT="Did mouse move right?"/>
<DELAY Flags="\x13" Time="400"/>
<GET MOUSE POSITION Option="\x00" X="%left2%" Y="ypos"/>
<VARIABLE MODIFY INTEGER Option="\x01" Destination="%spanleft1%" Value1="%right1%" Value2="%left2%"/>
<IF VARIABLE Variable="%spanleft1%" Condition="\x03" Value="%movement%" IgnoreCase="FALSE" _COMMENT="Did mouse move left?"/>
<MACRO RUN Use_ID="FALSE" Name="1_Close_Application_or_Browser_Window" ID="-1" Wait="TRUE"/>
<END IF/>
<END IF/>
<COMMENT Value=" "/>
<END IF/>
<END REPEAT/>
<COMMENT Value=" "/>
<MACRO RETURN/>
<COMMENT Value=" "/>

 

 

 

Link to comment
Share on other sites

2 hours ago, rberq said:

I have a hot key macro that closes whatever application/window is focused, by entering keystrokes <ALTD><SPACE>c<ALTU>.  ... Anyhow, I was frustrated because many Windows 10 applications block ME from seeing the hot key, so the "close application" macro doesn't run when I would like it to.  

 

I find that pressing "Alt+spacebar" followed by "c" closes almost all applications, including many of those pesky Windows 10 apps. I wonder whether your macro would work in more contexts if you did this:

 

<ALTD><SPACE><ALTU>c

 

My understanding is that the Alt key shouldn't be held down while pressing "c". There are ginormous inconsistencies in Windows about when the underlined accelerator key means "press and HOLD Alt to activate the accelerator" or "press and RELEASE Alt to activate the accelerator" or "DON'T TOUCH Alt! Press the accelerator on its own!"

 

I wonder whether Alt + F4 would work more consistently. Ditto for the Macro Express "Window Close" instruction.

 

I'm going to have a closer look at the part of your script that analyzes and interprets mouse movements. Looks very cool!

Link to comment
Share on other sites

11 hours ago, acantor said:

I find that pressing "Alt+spacebar" followed by "c" closes almost all applications

That's what my "kill" macro does -- courtesy of a previous post where you pointed out that method, thank you.  But I find that to be a clumsy key combination to press.  Maybe like the piano it would get easier with practice.  It's also pretty simple logic in a macro to locate the upper right corner of a window and click the X.  So the problem is not how to kill the application; the problem is how to get the macro to run in the first place when Windows blocks hotkey use.  Since Windows apparently does NOT block Macro Express from seeing the mouse, and since I often am using the mouse anyway, making use of it's movements is convenient.  It's a bit like flicking an app away on my wife's iPad or on the Android phone.  I wonder if there are others distinctive movements I could use for other functions ....

Link to comment
Share on other sites

Once upon a time a long time ago -- during the age when Microsoft adhered to its own design rules -- users could close ANY window by pressing the three keys in sequence:

 

Press Alt. Release it.

Press spacebar. Release it.

Press c. Release it.

 

No finger contortions required! No need to simultaneously hold down two keys. The sequence could be done with one finger. And the keys were close to each other, forming the outline of a triangle -- as long as you're using the Alt on the left side of the keyboard instead of the one on the right.

 

The sequence still works in Notepad. It works in Window 10's File Explorer. It continues to work in many dialog boxes, including the Macro Express "Create New Macro" dialog. But in most of today's applications, you can't do it as a sequence of three key presses. You must hold Alt while pressing the spacebar.

Link to comment
Share on other sites

Your idea reminds of a macro I tried to script years ago.


Some windows close if you press the Escape key (Esc). That makes sense: you're wanting to "escape" from that application.


For example, while working in the Macro Express "Script Editor," if you chose an instruction from the left column and a dialog box appears (e.g., "Repeat Start," "Mouse Move," "Text Type," "Variable Set Integer", and so on), you can close those dialog box by pressing Esc.


Although many windows close when you "escape" them, most windows don't behave this way. Nothing happens when you press Esc. Instead you must press Alt + F4, Alt + spacebar, C, etc. to close them. Or to close browser windows and Word documents, you must use Ctrl + F4, Ctrl + W, etc.


An impatient person might repeatedly press Esc in a futile attempt to force the hotkey to work. But what if the effort wasn't futile? The idea for the macro was that the window would close if you quickly pressed Esc enough times. I tried with three, four, and five presses in quick succession.

 

It was a fun project, and I eventually got the script to work. But I didn't find the solution at all practical.

Link to comment
Share on other sites

10 hours ago, acantor said:

An impatient person might repeatedly press Esc in a futile attempt to force the hotkey to work. But what if the effort wasn't futile? The idea for the macro was that the window would close if you quickly pressed Esc enough times. I tried with three, four, and five presses in quick succession.

Not so far-fetched.  I have a macro that is sort of a panic-button to kill runaway macros.  (I use it more often than you might think!)  It simulates typing the clumsy Macro Express key sequence to abort running macros, and does it multiple times "just in case", logging each time.  In fact what actually happens, almost always, is that the runaway, plus this abort macro itself, are both cancelled on the very first pass.  One odd thing is: if there are no other macros running, the ME box pops up telling me the abort macro itself has been halted; if there ARE other macro(s) running, everything is aborted but no pop-up appears.

 

// Repeat abort sequence until this macro itself is aborted
// Keystrokes defined in Macro Express Options | Preferences to abort macros
Variable Set Integer %countr% to 0
Repeat Start (Repeat 100 times)
  Variable Modify Integer %countr%: Increment
  Log Messages to "C:\Temp\MacroExpressProLogFiles\MacroExpressPro_Macro_Log_File.txt"
  "Macro Kybd_Abort_Running_Macros issuing abort %countr% time(s)"
  Text Type (Simulate Keystrokes): <WIN>`
  Delay: 500 milliseconds, without ability to halt
End Repeat

 

Link to comment
Share on other sites

Quote

  I have a macro that is sort of a panic-button to kill runaway macros. 

 

Me too. But mine isn't a Macro Express script. At one point it was a desktop shortcut, but then I found it worked more reliably as an AutoHotkey script that runs a handy Windows command:

 

taskkill.exe /f /im macexp.exe

 

(taskkill.exe is located in C:\Windows\System32)

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

×
×
  • Create New...