Jump to content
Macro Express Forums

Activate a Macro with Window Title only once


Recommended Posts

Sometimes, I try to write a window-title activated macro. For example, I might write a macro to check a few boxes on a particular website when I visit. The frustration is the macro keeps activating over and over. To manage this frustrating situation, I may add a delay at the end of the macro to slow down the repeat activations.

 

Does anyone know how to set up a window-activated macro so that it works just once, that is to say only when the window opens and gets focus?

 

Thanks for any tips!

Link to comment
Share on other sites

Hard to advise without seeing an example macro. For instance, are you activating it when a window is opened with that title? Or when it gains focus as @acantor assumes? And must it be an exact title or a partial one? What operations is it performing after starting? If for instance it's using a Text Box Display (TBD) to give you a message then as soon as you close that TBD it will probably regain focus and start the macro again.

 

However, I've found that MEP sometimes does not respond reliably to window title commands. Timing or other obscure conflicts with the OS may be the cause. In which case other 'recognition' methods may be needed.

 

Could you share a very simple macro that consistently exhibits 'activating over and over'? (Ask if you don't know how to do that.)

 

 

  • Like 1
Link to comment
Share on other sites

For some reason, it never occurred to me to activate with "Window is Opened." I guess I didn't scroll through the options. I am trying it now, and it seems to do the trick. Thank you @acantor!

 

@terrypin, thank you for the info! Time will tell if MEP continues to behave over time. And, I don't know how to share the macro.

Link to comment
Share on other sites

So now is the time to let us see your macro.

 

1. Edit/rewrite  it to remove any private content and to make it 'universally accessible'', not needing any of your files or other applications.

2. Include any comments necessary to make its purpose and operation clear.

3. Run it to ensure it consistently exhibits the problem behaviour.

4. In the Script Editor: Select all your script (Ctrl+a)>Right click>Copy Command Text.

5. In the forum post you are preparing, on a fresh line: Click the code icon '<>'  and in the resultant Code window paste the command text from the clipboard and click 'Insert into post'.

6. Back in your script, with all lines still selected: Ctrl+c to copy it to clipboard in its 'raw' form

7. Repeat step 5, this time pasting the actual code.

 

We will then be able to see your script and duplicate your macro with minimal effort.

 

--------------------

 

I took the above steps to show you my macro which appears to work reliably, running just once whenever this thread gets focus in my browser:

 

// This macro displays a text message whenever a window containing the string

// '8465-activate-a-macro'

// has just gained focus.
Text Box Display: Macro started
Delay: 0.5 seconds

 

<COMMENT Value="This macro displays a text message whenever a window containing the string\r\n"/>
<COMMENT Value="'8465-activate-a-macro'\r\n"/>
<COMMENT Value="has just gained focus."/>
<TEXT BOX DISPLAY Title="Macro started" Content="{\\rtf1\\ansi\\ansicpg1252\\deff0\\deflang2057{\\fonttbl{\\f0\\fnil\\fcharset0 Tahoma;}{\\f1\\fnil Tahoma;}}\r\n{\\colortbl ;\\red255\\green0\\blue0;}\r\n\\viewkind4\\uc1\\pard\\qc\\b\\f0\\fs24 A Waterfox or Firefox or Chrome or Edge browser window title containing the string\r\n\\par \\cf1 '8465-activate-a-macro'\\cf0 \r\n\\par has just gained focus.\\b0\\f1\\fs20 \r\n\\par }\r\n" Left="660" Top="498" Width="477" Height="195" Monitor="0" OnTop="TRUE" Keep_Focus="TRUE" Mode="\x00" Delay="0"/>
<DELAY Flags="\x01" Time="0.5"/>

 

Link to comment
Share on other sites

The macro is very simple. Here it is. I've attached an image to show the activation.

//This macro launches WNBA League Pass

Lock Player: Wait for running macros to stop
 
// Get computer name and open control panel.

Macro Run: !NA GLB Mini Tool, Get Computer Name
 
// Home Computer
 
If Variable %T[90]% Equals "ABRAHAM"
  Delay: 500 milliseconds
  // Next 3 lines to push the "Launch" button  
  Mouse Move: 960, 960 Relative to Screen
  Delay: 500 milliseconds
  Mouse Left Click
  Delay: 500 milliseconds
  // Next 3 lines to click on the "Adobe Flash Player" shield
  Mouse Move: 70, 190 Relative to Screen
  Delay: 1000 milliseconds
  Mouse Left Click
  // Next 3 lines to click on the "Allow" button
  Mouse Move: 380, 209 Relative to Screen
  Delay: 750 milliseconds
  Mouse Left Click
  // Next line positions the cursor
  Mouse Move: 960, 960 Relative to Screen
End If

// Next line keeps the macro from running over and over again.
Delay: 30000 milliseconds

Unlock Player

<COMMENT Value="This macro launches WNBA League Pass"/>
<COMMENT/>
<LOCK PLAYER Wait="TRUE"/>
<COMMENT/>
<COMMENT Value="Get computer name and open control panel."/>
<COMMENT/>
<MACRO RUN Use_ID="FALSE" Name="!NA GLB Mini Tool, Get Computer Name" ID="-1" Wait="TRUE"/>
<COMMENT/>
<COMMENT Value="Home Computer"/>
<COMMENT/>
<IF VARIABLE Variable="%T[90]%" Condition="\x00" Value="ABRAHAM" IgnoreCase="FALSE"/>
<DELAY Flags="\x02" Time="500"/>
<MOUSE MOVE Option="\x01" X="960" Y="960" _PROMPT="0x000A"/>
<DELAY Flags="\x02" Time="500"/>
<COMMENT Value="Next 3 lines to push the \"Launch\" button"/>
<MOUSE LEFT CLICK/>
<DELAY Flags="\x02" Time="500"/>
<MOUSE MOVE Option="\x01" X="70" Y="190" _PROMPT="0x000A"/>
<DELAY Flags="\x02" Time="1000"/>
<COMMENT Value="Next 3 lines to click on the \"Adobe Flash Player\" shield"/>
<MOUSE LEFT CLICK/>
<MOUSE MOVE Option="\x01" X="380" Y="209" _PROMPT="0x000A"/>
<DELAY Flags="\x02" Time="750"/>
<COMMENT Value="Next 3 lines to click on the \"Allow\" button"/>
<MOUSE LEFT CLICK/>
<COMMENT Value="Next line positions the cursor"/>
<MOUSE MOVE Option="\x01" X="960" Y="960" _PROMPT="0x000A"/>
<END IF/>
<COMMENT/>
<COMMENT Value="Next line keeps the macro from running over and over again."/>
<DELAY Flags="\x02" Time="30000"/>
<COMMENT/>
<UNLOCK PLAYER/>
 

2019-08-11_0529.png

Link to comment
Share on other sites

What exactly happens when you

- open that web page?

- activate it again later?

 

Ditto without the Lock/Unlock commands?

 

Ditto in F9 mode?

 

Ditto in F8 mode?

 

Do any of those indicate the source of whatever problem you’re still having?

 

I don’t want to subscribe to the WNBA League Pass site to test your macro. So please edit as requested to make it  as short and simple as possible and using a web site widely accessible. But still exhibiting whatever symptoms you describe.

 

EDIT: Include your script code for multiple macros in separate Code boxes.

 

Link to comment
Share on other sites

Nothing surprising happens. It just runs each time the window regains focus. If I tab away and back, for example. I simplified the macro code and changed the activation to the Google search page, "Google - Google Chrome".

Lock Player: Wait for running macros to stop
 
Delay: 500 milliseconds
Mouse Move: 960, 960 Relative to Screen
Delay: 500 milliseconds
Mouse Left Click
Delay: 500 milliseconds
Mouse Move: 70, 190 Relative to Screen
Delay: 1000 milliseconds
Mouse Left Click
Mouse Move: 380, 209 Relative to Screen
Delay: 750 milliseconds
Mouse Left Click
Mouse Move: 960, 960 Relative to Screen
 
Delay: 1000 milliseconds
Unlock Player

<LOCK PLAYER Wait="TRUE"/>
<COMMENT/>
<DELAY Flags="\x02" Time="500"/>
<MOUSE MOVE Option="\x01" X="960" Y="960" _PROMPT="0x000A"/>
<DELAY Flags="\x02" Time="500"/>
<MOUSE LEFT CLICK/>
<DELAY Flags="\x02" Time="500"/>
<MOUSE MOVE Option="\x01" X="70" Y="190" _PROMPT="0x000A"/>
<DELAY Flags="\x02" Time="1000"/>
<MOUSE LEFT CLICK/>
<MOUSE MOVE Option="\x01" X="380" Y="209" _PROMPT="0x000A"/>
<DELAY Flags="\x02" Time="750"/>
<MOUSE LEFT CLICK/>
<MOUSE MOVE Option="\x01" X="960" Y="960" _PROMPT="0x000A"/>
<COMMENT/>
<DELAY Flags="\x02" Time="1000"/>
<UNLOCK PLAYER/>
 

Link to comment
Share on other sites

One kludgy workaround might be to not have it activated by window title.  Instead just have a link on the desktop.   Then the macro is like:

 

1 open webpage

2 wait for window to be active

3 check boxes and stuff

 

Then use that link as a shortcut to the website.  It runs only once, only when you click on it.

 

EDIT: 

-You can then (optionally) right click the link link and change its icon to look like your web browser.   

-Also note: As has been pointed out before on this forum, it is usually best to do web checkboxes via TextType <Tab><Tab><Space> rather than MoveMouse, Click, when possible.  

 

Link to comment
Share on other sites

Try this.

 

Variable Restore: Restore Text Variables // Restores previously set status of the flag
If Variable %T[99]% Equals "Stop" // If 'Stop' then end the macro
  Macro Stop
End If
Delay: 0.1 seconds
Text Box Display: Macro is continuing
Delay: 0.1 seconds
// Your macro commands here
// Set the 'flag' T[99] to 'Stop' now that the macro has been run once
Variable Set String %T[99]% to "Stop"
Variable Save: Save Text Variables // Saves the status of the flag

CODE

<VARIABLE RESTORE Option="\x01" _COMMENT="Restores previously set status of the flag"/>
<IF VARIABLE Variable="%T[99]%" Condition="\x00" Value="Stop" IgnoreCase="FALSE" _COMMENT="If 'Stop' then end the macro"/>
<MACRO STOP/>
<END IF/>
<DELAY Flags="\x01" Time="0.1"/>
<TEXT BOX DISPLAY Title="Macro is continuing" Content="{\\rtf1\\ansi\\ansicpg1252\\deff0\\deflang2057{\\fonttbl{\\f0\\fnil\\fcharset0 Tahoma;}{\\f1\\fnil Tahoma;}}\r\n\\viewkind4\\uc1\\pard\\f0\\fs20 T[99] = flag = %T[99]%\r\n\\par \r\n\\par The macro will now continue to run your commands, then set the flag T[99] to 'Stop'.\r\n\\par \r\n\\par So it will not run fully again unless you either disable the macro or write and run another macro to restart it.\r\n\\par \r\n\\par \\b Close this to continue.\\b0\\f1 \r\n\\par }\r\n" Left="505" Top="485" Width="594" Height="240" Monitor="0" OnTop="TRUE" Keep_Focus="TRUE" Mode="\x00" Delay="0"/>
<DELAY Flags="\x01" Time="0.1"/>
<COMMENT Value="Your macro commands here"/>
<COMMENT Value="Set the 'flag' T[99] to 'Stop' now that the macro has been run once"/>
<VARIABLE SET STRING Option="\x00" Destination="%T[99]%" Value="Stop" NoEmbeddedVars="FALSE"/>
<VARIABLE SAVE Option="\x01" _COMMENT="Saves the status of the flag"/>

(Note: As described in my earlier post, it is best to place your code in a Code box, as I've done above.)

 

The devil as usual is in the details. As you've found in this case, MEP's activation by 'Window is opened' is unfortunately unreliable. 'Window Gains Focus' usually works but adds complications.

 

If my approach or @kunkel321's neat idea doesn't work then consider an additional macro to change the 'flag' under other conditions that meet your requirements, such as after a certain longish period of time, or when tab has been reactivated a certain number of times.

 

 

OnceOnly-Activation.jpg

Link to comment
Share on other sites

I ended up going with the kludgy workaround suggested by @kunkel321. At the same time, I replaced some of the mouse move and clicks with keyboard commands. I considered adding the flag suggested by @terrypin, but decided against it, because it seems to me another scheduled macro would be needed to reset the flag after some delay.

 

Anyway, I now have a desktop icon that opens the webpage and launches the games.

 

I really appreciate you guys helping me out. Thanks again.

Link to comment
Share on other sites

Re "wait for..." command.  It occurred to me after posting, that "wait for webpage" might be better than "wait for window."  I'm not sure.

 

Also Re Terrypin's use of "restore text variable."  Pretty cool!   I did not know that this command was a thing.  :- }

Link to comment
Share on other sites

I found that "wait for webpage" did not work well so I switched to "wait for window". It is doing the job. I also like @terrypin's idea. The problem is that the flag has to be cleared sometime later and before the next time the webpage is opened again. That can be done with another "scheduled" macro that runs later or by manually running a macro before opening the webpage again.

 

Regards

Link to comment
Share on other sites

  • 2 months later...

I don't know if this got answered, but can't you save the window title and/or date n the registry and use that as a condition to bail out of the macro if it activates? Like if "Notepad" opens and there's an entry for Notepad and it's date is 5 seconds ago, exit. Then set the condition to be whatever time limit you want. 

Sorry, I didn't read every message in this thread. 

Link to comment
Share on other sites

Another way to accomplish this is to not let the macro halt until the window has been closed. If a specific macro is running it cannot be activated again. This demonstrates using Notepad.

Text Box Display: Note_pad  is running
 
Repeat Until %Done% Equals "Done"
  If Not Window "Notepad" is running
    Variable Set String %Done% to "Done"
    Macro Stop
  End If
  Delay: 1000 milliseconds
End Repeat
<TEXT BOX DISPLAY Title="Note_pad  is running" Content="{\\rtf1\\ansi\\ansicpg1252\\deff0\\deflang1033{\\fonttbl{\\f0\\fnil Tahoma;}}\r\n{\\colortbl ;\\red0\\green0\\blue255;}\r\n\\viewkind4\\uc1\\pard\\qc\\cf1\\f0\\fs28 Notepad is running\\cf0\\fs20 \r\n\\par \\pard \r\n\\par }\r\n" Left="Center" Top="Top" Width="455" Height="76" Monitor="0" OnTop="TRUE" Keep_Focus="TRUE" Mode="\x02" Delay="0"/>
<COMMENT/>
<REPEAT UNTIL Variable="%Done%" Condition="\x00" Value="Done"/>
<IF NOT WINDOW Option="\x01" Title="Notepad" Partial="TRUE" Wildcards="FALSE"/>
<VARIABLE SET STRING Option="\x00" Destination="%Done%" Value="Done" NoEmbeddedVars="FALSE"/>
<MACRO STOP/>
<END IF/>
<DELAY Flags="\x02" Time="1000"/>
<END REPEAT/>

In this example the text box display has a window title of "note_pad" because if that window title is "notepad" the macro never ends.

Link to comment
Share on other sites

Samrae,

 

I am trying out your suggestion, and it seems to work the way I want it to. See the last part of the code. I added the 15 seconds delay at the end to slow down the cycling of the code while the Bookmarks webpage is active.

 

// Get computer name!
Macro Run: !NA GLB Mini Tool, Get Computer Name
// Home Computer
If Variable %T[90]% Equals "ABRAHAM"
  Mouse Move: 135, 245 Relative to Screen
  Delay: 250 milliseconds
  Mouse Left Click
  Text Type (Simulate Keystrokes): <HOME>
  Text Type (Simulate Keystrokes): <ARROW DOWN><ARROW DOWN><ARROW DOWN><ARROW DOWN><ARROW DOWN><ENTER>
  Delay: 250 milliseconds
  Text Type (Simulate Keystrokes): <TAB>
  Mouse Move: 920, 276 Relative to Screen
End If
// Work Computer
If Variable %T[90]% Equals "LUT4477"
  Mouse Move: 2700, 243 Relative to Screen
  Delay: 250 milliseconds
  Mouse Left Click
  Text Type (Simulate Keystrokes): <HOME>
  Text Type (Simulate Keystrokes): <ARROW DOWN><ARROW DOWN><ARROW DOWN><ARROW DOWN><ARROW DOWN><ENTER>
  Delay: 250 milliseconds
  Text Type (Simulate Keystrokes): <TAB>
  Mouse Move: 3480, 276 Relative to Screen
End If
// Run the macro until the window is closed.
Variable Set String %T[77]% to "Not"
Repeat Until %T[77]% Equals "Done"
  If Not Window "Bookmarks - Google Chrome" is running
    Variable Set String %T[77]% to "Done"
    Macro Stop
  End If
End Repeat
Delay: 15 milliseconds

 

<COMMENT Value="Get computer name!"/>
<MACRO RUN Use_ID="FALSE" Name="!NA GLB Mini Tool, Get Computer Name" ID="-1" Wait="TRUE"/>
<COMMENT Value="Home Computer"/>
<IF VARIABLE Variable="%T[90]%" Condition="\x00" Value="ABRAHAM" IgnoreCase="FALSE"/>
<MOUSE MOVE Option="\x01" X="135" Y="245" _PROMPT="0x000A"/>
<DELAY Flags="\x02" Time="250"/>
<MOUSE LEFT CLICK/>
<TEXT TYPE Action="0" Text="<HOME>"/>
<TEXT TYPE Action="0" Text="<ARROW DOWN><ARROW DOWN><ARROW DOWN><ARROW DOWN><ARROW DOWN><ENTER>"/>
<DELAY Flags="\x02" Time="250"/>
<TEXT TYPE Action="0" Text="<TAB>"/>
<MOUSE MOVE Option="\x01" X="920" Y="276" _PROMPT="0x000A"/>
<END IF/>
<COMMENT Value="Work Computer"/>
<IF VARIABLE Variable="%T[90]%" Condition="\x00" Value="LUT4477" IgnoreCase="FALSE"/>
<MOUSE MOVE Option="\x01" X="2700" Y="243" _PROMPT="0x000A"/>
<DELAY Flags="\x02" Time="250"/>
<MOUSE LEFT CLICK/>
<TEXT TYPE Action="0" Text="<HOME>"/>
<TEXT TYPE Action="0" Text="<ARROW DOWN><ARROW DOWN><ARROW DOWN><ARROW DOWN><ARROW DOWN><ENTER>"/>
<DELAY Flags="\x02" Time="250"/>
<TEXT TYPE Action="0" Text="<TAB>"/>
<MOUSE MOVE Option="\x01" X="3480" Y="276" _PROMPT="0x000A"/>
<END IF/>
<COMMENT Value="Run the macro until the window is closed."/>
<VARIABLE SET STRING Option="\x00" Destination="%T[77]%" Value="Not" NoEmbeddedVars="FALSE"/>
<REPEAT UNTIL Variable="%T[77]%" Condition="\x00" Value="Done"/>
<IF NOT WINDOW Option="\x01" Title="Bookmarks - Google Chrome" Partial="TRUE" Wildcards="FALSE"/>
<VARIABLE SET STRING Option="\x00" Destination="%T[77]%" Value="Done" NoEmbeddedVars="FALSE"/>
<MACRO STOP/>
<END IF/>
<END REPEAT/>
<DELAY Flags="\x02" Time="15"/>
 

Link to comment
Share on other sites

The delay after End Repeat will never be used. It should look like this:

    Macro Stop
  End If
  Delay: 15 Seconds
End Repeat

Without the delay inside the repeat loop Macro Express will run these macro commands as quickly as possible without slowing or stopping. It will use 100% of one of your CPU cores. This puts your macro into what your computer considers an infinite loop. I wouldn't be surprised if more than Chrome freezes. And Terry is right, your delay command contains 15 milliseconds instead of 15 seconds.

Edited by Samrae
Added more explanation
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...
×
×
  • Create New...