how to boilerplate macros

Technical support and scripting issues

Moderators: Dorian (MJT support), JRL

Post Reply
mhcha
Junior Coder
Posts: 23
Joined: Sat Jan 01, 2022 11:10 am

how to boilerplate macros

Post by mhcha » Tue Aug 22, 2023 8:32 am

hello,
If ".hi" is written,
"hello, Thank you for your inquiry." I want to write less.
I can use the OnEvent function to assign a hotkey to a specific key,
but this is not boilerplate. Is there any way to solve this?

User avatar
Dorian (MJT support)
Automation Wizard
Posts: 1354
Joined: Sun Nov 03, 2002 3:19 am
Contact:

Re: how to boilerplate macros

Post by Dorian (MJT support) » Tue Aug 22, 2023 10:07 am

I hope I'm understanding correctly. I think it would be difficult to cover all eventualities, but maybe something like this with all your commonly typed sentences. These are the kind of script that tend to grow in time...

..and of course you could even write it to present a different set of options depending on window title.

Assign to a hotkey. Even better, assign to a hotkey on a programmable mouse.

Code: Select all

//Test in Notepad
RunProgram>Notepad.exe
WaitWindowOpen>Untitled*
wait>0.1

//Set up your options
Let>Opt1=Hello
Let>Opt2=Goodbye
Let>Opt3=Yesterday
Let>Opt4=Today

//Get the current window title
GetActiveWindow>title,xx,yy,ww,hh

//User input
input>res,1.%Opt1%%CRLF%2.%Opt2%%CRLF%3.%Opt3%%CRLF%4.%Opt4%

//Output according to chosen option
if>res>0
  SetFocus>title
  Send>Opt%res%
endif
Another more convoluted method might be to highlight .hi and then have a macro determine what's highlighted - then paste appropriate text in it's place.

Code: Select all

//Get Active Window Details
GAW>Win,x,y,w,h

//Copy selected text
Press Ctrl
Wait>0.05
Send>c
Wait>0.05
Release Ctrl
Wait>0.05

//Get the cipboard
GetClipBoard>NewClip,0


//Is the word ".hi" selected?
Let>Search=.hi
Pos>{Lower(%Search%)},{Lower(%NewClip%)},1,PosHi,

if>PosHi>0
  Setfocus>Win
  PutClipBoard>hello, Thank you for your inquiry.

  Setfocus>Win

  //Paste (instead of using sendtext - faster for sending longer text snippets) 
  Press Ctrl
  Wait>0.05
  Send>v
  Wait>0.05
  Release Ctrl
  Wait>0.05
Endif


//Is the word ".order" selected?
Let>Search=.order
Pos>{Lower(%Search%)},{Lower(%NewClip%)},1,PosOrder,

if>PosOrder>0
  Setfocus>Win
  PutClipBoard>Thank you for your order, we will be processing it today

  Setfocus>Win

  //Paste (instead of using sendtext - faster for sending longer text snippets) 
  Press Ctrl
  Wait>0.05
  Send>v
  Wait>0.05
  Release Ctrl
  Wait>0.05
Endif
I'd be interested to hear how others may do this.
Yes, we have a Custom Scripting Service. Message me or go here

mhcha
Junior Coder
Posts: 23
Joined: Sat Jan 01, 2022 11:10 am

Re: how to boilerplate macros

Post by mhcha » Tue Aug 22, 2023 2:41 pm

Dear Dorian, thank you as always.

The method you gave above gave me an idea.
Currently in testing, I am looking for a way to recognize ".hi" using the OnEvent function.

Code: Select all

Repeat>
OnEvent>KEY_DOWN,VK190,0,H
Until>

SRT>H
Repeat>
OnEvent>KEY_DOWN,VK72,0,I
Until>
End>H

SRT>I
Repeat>
OnEvent>KEY_DOWN,VK73,0,MyText
Until>
End>I

SRT>MyText
MDL>Done
End>MyText
I tried the above code but
OnEvent>KEY_DOWN,VK72,0,I
line doesn't work. Can you tell me why?

And I tried this too, but it failed because WaitKeyDown works too slow.

Code: Select all

Repeat>
OnEvent>KEY_DOWN,VK190,0,HI
Until>

SRT>HI
WaitKeyDown>h
WaitKeyDown>i
MDL>Done
End>HI
Looking forward to your good ideas. thank you.

User avatar
Dorian (MJT support)
Automation Wizard
Posts: 1354
Joined: Sun Nov 03, 2002 3:19 am
Contact:

Re: how to boilerplate macros

Post by Dorian (MJT support) » Tue Aug 22, 2023 3:47 pm

You have the OnEvent in an endless loop which doesn't seem like a good idea as you only need that to run once. It didn't work for me either so I changed it so it will look for ., then move on to look for the h, then the i.

tbh this approach doesn't make much sense to me as I have a feeling it could get a bit unwieldy and confusing, but here is what you asked for. You can add another endless loop at the end if you want it to run constantly :

Code: Select all

//.
OnEvent>KEY_DOWN,VK190,0,HH

repeat>hhh
Until>hhh,1


//h
OnEvent>KEY_DOWN,VK190,0,
OnEvent>KEY_DOWN,VK72,0,II

repeat>iii
Until>iii,1

//i
OnEvent>KEY_DOWN,VK72,0,
OnEvent>KEY_DOWN,VK73,0,MyText

repeat>jjj
Until>jjj,1


srt>HH
  let>hhh=1
END>HH

srt>II
  let>iii=1
END>II

srt>MyText
  let>jjj=1
  Send>This is some text
END>MyText


Run it through step by step in the debugger for better understanding (if needed).
Yes, we have a Custom Scripting Service. Message me or go here

User avatar
Grovkillen
Automation Wizard
Posts: 1024
Joined: Fri Aug 10, 2012 2:38 pm
Location: Bräcke, Sweden
Contact:

Re: how to boilerplate macros

Post by Grovkillen » Tue Aug 22, 2023 5:37 pm

Ok, this isn't working because of the bug with passing variables from OnEvent but I think this is kinda how mhcha want to roll. I could see a use for this type of script. I would imagine you may want to flush the "buffer string" after x amount of seconds. Also you would probably want to add some more "awareness" of the script's surroundings (maybe only trigger if notepad is active etc.). But I do like the idea presented by mhcha.

Code: Select all


Let>MAX_STRING_LENGTH=12
//just some different trigger combinations.. 
Let>TRIGGER_STRINGS=.hi|h.i|.hh
Let>BUFFER_STRING=

StringReplace>TRIGGER_STRINGS,.,\.,TRIGGER_STRINGS
StringReplace>TRIGGER_STRINGS,+,\+,TRIGGER_STRINGS
Let>REGEX_PATTERN=^(%TRIGGER_STRINGS%)

OnEvent>KEY_DOWN,VK190,0,COUNT_LETTERS,{"."}
OnEvent>KEY_DOWN,VK72,0,COUNT_LETTERS,{"h"}
OnEvent>KEY_DOWN,VK73,0,COUNT_LETTERS,{"i"}
//ESC=close app
OnEvent>KEY_DOWN,VK27,0,CLOSE_APP

Label>RESTART_LOOP
Wait>0.02
Goto>RESTART_LOOP

SRT>CLOSE_APP
  Exit>
END>CLOSE_APP

SRT>COUNT_LETTERS
  Let>BUFFER_STRING=%COUNT_LETTERS_Var_1%%BUFFER_STRING%
  MidStr>BUFFER_STRING,1,MAX_STRING_LENGTH,BUFFER_STRING
  RegEx>REGEX_PATTERN,BUFFER_STRING,0,TEMP_found,TEMP_count,0,,
  If>TEMP_count>0
    MessageModal>TEMP_found_1
  Endif>
END>COUNT_LETTERS
Let>ME=%Script%

Running: 15.0.24
version history

mhcha
Junior Coder
Posts: 23
Joined: Sat Jan 01, 2022 11:10 am

Re: how to boilerplate macros

Post by mhcha » Mon Aug 28, 2023 12:24 pm

Thank you Dorian and Grovkillen for responding to my post.

I just checked my post.

Dorian's method worked for me and is currently being used with some minor modifications. Thank you.

Grovkillen's idea was what I needed, but unfortunately it doesn't work as a bug.

I hope you both have a good day.
Thank you.

Post Reply
cron
Sign up to our newsletter for free automation tips, tricks & discounts