using a trigger to activate a script

251 views
Skip to first unread message

cori schlegel

unread,
May 9, 2020, 9:38:26 AM5/9/20
to iterm2-discuss
Hey folks, I've read over the triggers and scripting documentation in the docs, but one thing I haven't been able to process is what to put in the parameters to correctly call a script with the output of the trigger.

In my case I want to pass the matched string to my script. I have a script (let's say it's called script.py) that takes a "msg" parameter. I've set the Action to "Invoke Script Function" and tried setting the Parameters to variations on "script.py(\(matches[0]))" but I keep getting various errors when the trigger executes. Is there some page in the documentation that explains this in more detail or shows an example that I'm missing?

Thanks in advance for any help!

Richard Mitchell

unread,
May 9, 2020, 11:03:38 AM5/9/20
to iterm2-discuss
I have this in my dynamic profile:

         "Triggers" : [
            {
               "parameter" : "~/bin/ssay \\\"\\1\\\"",
               "partial" : 0,
               "regex" : "TellMe (.*)",
               "action" : "ScriptTrigger"
            },

When the phrase "TellMe" appears in the terminal, the remainder of the line is passed to my script '~/bin/ssay'

Does that help?

cori schlegel

unread,
May 9, 2020, 11:42:53 AM5/9/20
to iterm2-discuss
Thanks for your reply, Richard. It offers some hints, but I'm running python3 and using interpolated strings (so I think the script invocation is different, although I am not that great at python). Also for the triggers I've registered through the UI the "Invoke Script Function" action shows up in the exported JSON as "iTermRPCTrigger" instead of as "ScriptTrigger" o I wonder if that Action type is expecting a different set of params.

Based on that additional information, though, I've been able to get to a parameter that looks like "script.py(msg:matches[0])" which gets me to a different place, at least - now I get a message telling me that there's "No function registered for invocation "script.py()"". script.py shows up in my Scripts menu, so I guess I'm missing a different piece of the puzzle about registering functions.

My loaded script looks like this

<snip>
#!/usr/bin/env python3.7

import iterm2
import os

async def main(connection, msg):

    # do some os.systems calls with the passed-in "msg"

iterm2.run_until_complete(main)
</snip>

cori schlegel

unread,
May 9, 2020, 5:13:51 PM5/9/20
to iterm2-discuss
With a bit of a hint from Richard's response I was able to come up with a working process, although I'm not sure if it's the best or even correct one

I refactored the script I was using to be more general and moved it to auto load, and inside that script I created the function I wanted to call and registered it using async_register(). The function looks like this now:

@iterm2.RPC
async def pingme(msg):
    os.system('echo {mesg} | mail -s "From iTerm2" "**********@mms.carrier.net"'.format(mesg = msg))
    os.system("slackme \"{mesg}\"".format(mesg = msg))
await pingme.async_register(connection)

the enclosing script has a wrapping `async def main()` function and closes with `iterm2.run_forever(main)`.

The I added an "Invoke Script Function" trigger with a parameter `pingme(msg:matches[0])` which looks like this in the Profile export

{
  "action" : "iTermRPCTrigger",
  "parameter" : "pingme(msg:matches[0])",
  "regex" : "^Tell me about this message\\!",
  "partial" : true
}

et voila, when that message shows up in my terminal I get a slack message and a text-via-email.

Thanks!

George Nachman

unread,
May 15, 2020, 2:42:01 AM5/15/20
to iterm2-...@googlegroups.com
This is exactly how it was meant to work, sorry I didn't get back to you sooner. How could the process of discovering this have been easier? I'd like to reduce the amount of friction the next person in your position has.

--
You received this message because you are subscribed to the Google Groups "iterm2-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to iterm2-discus...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/iterm2-discuss/86c26744-f968-47ab-aea4-847e7867a0c9%40googlegroups.com.

cori schlegel

unread,
May 15, 2020, 9:03:03 AM5/15/20
to iterm2-...@googlegroups.com
I’ve been thinking about that on and off - I’m not sure what piece of the puzzle I was missing at the outset because it’s largely obvious how it fits together from the inside.

Let me go through my “discovery” process again and see if there are some dots that could be more obviously connected from the outside.

cori

OtterFlip

unread,
Aug 14, 2021, 7:39:06 PM8/14/21
to iterm2-discuss
This was VERY helpful.  Thank you!

OtterFlip

unread,
Aug 14, 2021, 7:42:28 PM8/14/21
to iterm2-discuss
I got this working by placing my python script file into the Scripts/AutoLaunch directory.  My file has this content:


#!/usr/bin/env python3.7

import iterm2

async def main(connection):
# Register a function to be called by the trigger
@iterm2.RPC
async def mytriggerhandler():
print("Hello from mytriggerhandler")

await mytriggerhandler.async_register(connection)

app = await iterm2.async_get_app(connection)
window = app.current_terminal_window
if window is not None:
print("Hello from main!")

iterm2.run_forever(main)


I then created a new trigger and specified a regex string and specified that it was to invoke a script and for the parameter I merely input the following value into that field in the GUI:

mytriggerhandler()

Now when a matching string is found, in the Script Console I see the output of the handler function.  Interestingly, I see it twice if I type the matching pattern and only once if the matching pattern is printed by something I'm running.  I am guessing it is matching once on input and once on output.  Would be nice to have the ability to specify which you want to match (input, output, both).

OtterFlip

unread,
Aug 14, 2021, 7:45:13 PM8/14/21
to iterm2-discuss
Sorry, for some reason that completely trashed the formatting of the code.  The spaces are essential in Python.  Unfortunately Google Groups doesn't have a nice code formatting feature so you'll have to add spaces as needed.  It'll look like this with proper spacing:

iTermTrigger.png

Reply all
Reply to author
Forward
0 new messages