Best Way to Use a Browser URL as a Macro Trigger?

Applescript noob here. I added something to pass the variable result onto KM. If I run it in Script Debugger, it updates the variable in KM, but when I try and run it in an Execute AppleScript action, nothing happens. Any suggestions?

tell application "Brave Browser"
  set tabURL to URL of active tab of front window
end tell

tell application "Keyboard Maestro Engine"
  try
    set value of variable "BraveURL" to tabURL
  on error
    make new variable with properties {name:"BraveURL", value:tabURL}
  end try
end tell

I saw a preset in BetterTouchTool. It does something like this:

  1. While Browser X is the front app, check the current url every 1s.
  2. If the current url contains AAA, then show set-A buttons on the touch bar. If the current url contains BBB, then show set-B buttons on the touch bar. . . . otherwise, show the standard set buttons on the touch bar.

I think you can do a similar thing with KM. But the KM engine will be constantly shown as running when Browser X is the front app (i.e., checking the url every 1s). Therefore, only the BTT is a better solution here. If you have BTT, you may give it try.

Another way is to use a switch action to control the behavior (put the macro in a macro group only when a certain browser is the front app).

periodically checking should works, but I recall that it will have overall slow down since it is checking a lot and really try to avoid that.

Yes, I am already doing that as shown in the screenshot, but seems not working reliably.

Try this approach below and see if it works reliably. The information in the parenthesis is intended to work with this approach. You don't need to use the "Focused window title contains" condition.

Hey Neil,

Here's the canonical method (since KM8 I think). It will handle global, local, and instance variables.

set kmInstance to system attribute "KMINSTANCE"

tell application "Keyboard Maestro Engine"
   
   # Set AppleScript variable from Keyboard Maestro variable.
   set asVarName to getvariable "local_copiedText" instance kmInstance
   
   # set Keyboard Maestro variable from AppleScript variable
   setvariable "local_KM_VarName" instance kmInstance to dataStr
   
end tell

If that doesn't work then you've probably done something wrong.

Put something like:

beep 3

At the end of your script to make sure it's running.

You can also have the Execute an AppleScript Action display the result, so you can do something like this at the end of your script:

return "AppleScript Action to Set KM Variable Completed!"

If this doesn't work then you've probably done something wrong somewhere such as forgetting to enable the AppleScript action.

On another note – when working with AppleScript it's a good idea to have some sort of error handler, so you know if there was a problem – expected or otherwise.

try
   
   set kmInstance to system attribute "KMINSTANCE"
   
   tell application "Keyboard Maestro Engine"
      
      # Set AppleScript variable from Keyboard Maestro variable.
      set asVarName to getvariable "local_copiedText" instance kmInstance
      
      # set Keyboard Maestro variable from AppleScript variable
      setvariable "local_KM_VarName" instance kmInstance to dataStr
      
   end tell
   
on error errMsg number errNum
   set errMsg to errMsg & linefeed & linefeed & "Num: " & errNum
   if errNum ≠ -128 then
      try
         tell application (path to frontmost application as text) to set ddButton to button returned of ¬
            (display dialog errMsg with title "ERROR!" buttons {"Copy Error Message", "Cancel", "OK"} ¬
               default button "OK" giving up after 30)
         if ddButton = "Copy Error Message" then set the clipboard to errMsg
      end try
   end if
end try

-Chris

1 Like

Did I mention I was a noob? I've probably done this all wrong...

I just pasted your codes after mine. Script Debugger is shown on the left so you can see the whole thing. Is the "beep 3" thing meant to make my mac beep? Cause it didn't.

So what happens when I run it is I get that error, I dismiss it and the "completed" message pops up. Sure enough, I've got the variable! Nice! How do I get rid of that error?

This works. It triggers on each switch of chrome tab/window, hope it won't have impact on performance.

Thanks.

It won't do anything in the background. The macro only runs when it is triggered. I don't think it will impact performance.

Yep.

try
   
   tell application "Brave Browser"
      set tabURL to URL of active tab of front window
   end tell
   
   # set Keyboard Maestro variable from AppleScript variable
   set kmInstance to system attribute "KMINSTANCE"
   tell application "Keyboard Maestro Engine"
      setvariable "BraveURL" instance kmInstance to tabURL
   end tell
   
on error errMsg number errNum
   set errMsg to errMsg & linefeed & linefeed & "Num: " & errNum
   if errNum ≠ -128 then
      try
         tell application (path to frontmost application as text) to set ddButton to button returned of ¬
            (display dialog errMsg with title "ERROR!" buttons {"Copy Error Message", "Cancel", "OK"} ¬
               default button "OK" giving up after 30)
         if ddButton = "Copy Error Message" then set the clipboard to errMsg
      end try
   end if
end try
1 Like

Nailed it!!! Thanks Chris, you're a :star:

1 Like