How to Trigger an App Independent Services Menu Item

Hello,

I wrote the highly sophisticated macro below which opens all the links in an email within highlighted text, even if the links are nested within text.

I find it very convenient. It's for sale, order today before midnight and get 50% off !

No seriously, it works in Mail and also in many other apps, like Pages and other document formats.

Is there any way to make the macro app independent, so that it works anytime the services menu contain Open URL ?

Note that the text display should read: highlight text containing links.

Thanks in advance for your time and help.

image

Hey @ronald,

Make the macro global in scope.

Change the app to the front application and the menu title to APPLICATION.

-Chris

1 Like

Thank you for your post.

As suggested:

  • I put the macro in the global macro group, always active, all apps
  • i edited the macro → Formost Application

it does not work. I wonder if there could be a step missing (red arrow)

image

Yes, there is. See how the Menu Title field was "Mail" in the original macro? You need to use APPLICATION when selecting a menu in the front application with this action:

Incidentally, is there a particular reason you're using a service for this? If you're already highlighting the text containing the links anyway, I might consider making a macro that copies the text to the clipboard and opens each link like this:

Open All Links in Selected Text.kmmacros (2.8 KB)
image

1 Like

works beautifully, and in the background. Fantastic !
thank you so much

1 Like

@gglick, nice solution!

But I have to say, that is one whale of a RegEx:
(?:(?:(?:https?|ftp):)?\/\/)(?:\S+(?::\S*)?@)?(?:(?!(?:10|127)(?:\.\d{1,3}){3})(?!(?:169\.254|192\.168)(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z0-9\u00a1-\uffff][a-z0-9\u00a1-\uffff_-]{0,62})?[a-z0-9\u00a1-\uffff]\.)+(?:[a-z\u00a1-\uffff]{2,}\.?))(?::\d{2,5})?(?:[/?#]\S*)

It seems to work fine in KM, but Regex101.com chokes on it, saying it doesn't like all of the \u metacharacters. I have no idea why.

image

I don't know if there is much of a speed penalty for a long RegEx like that, but I did find this one on stackoverflow.com that is much simpler, and seems to work in my testing:
(http|ftp|https):\/\/([\w_-]+(?:(?:\.[\w_-]+)+))([\w.,@?^=%&:\/~+#-]*[\w@?^=%&\/~+#-])?

1 Like

Thanks, @JMichaelTX! I found that regex on StackOverflow too, but went with this one on GitHub mainly because it seemed like it would cover more edge cases. I know it's a lot longer and more complex, but in practice the speed difference seems negligible, so as long as it works in KM, I figured I might as well go with it. Still, as they say, there's almost always more than one way to handle things like this, especially when it comes to regex, so having more options to choose from absolutely can't hurt :+1:

2 Likes

Thank you for all your research

1 Like

Hello Gabe,

I am creating a Services menu palette which works globally (not app specific). I reviewed your post above don't understand why this action does not work.

I tried Menu Title both Application and APPLICATION (like in your example), and I tried inverting menu item and submenu, but nothing works

If I replace Menu Title Application with the name of the application (example: Chrome), it works fine, so the problem seems to be with what I should write for Menu Title.

thanks very much !

1 Like

APPLICATION in all caps with Services as the submenu:

image

works for me in several apps I tested except for Chrome/Brave Browser, so I think the problem is most likely specific to Chrome. Given that, the way I'd get around this is to use a Switch/Case so that Chrome uses a specific action and every other app uses the generic APPLICATION:

Switch:Case.kmactions (1.7 KB)
image

1 Like

great solution ! very smart. thanks very much !

1 Like

WOW ! I downloaded and ran the .kmactions files to insert the macro. Very impressive. Had never seen that.

Heh, yes, that's what happens when you only export actions rather than a full macro. Usually the full macro is the way to go, but as you've seen, sometimes all you need is the actions (though of course, if you don't know in advance that .kmactions files open in the macro currently being edited, it can lead to a bit of confusion at first).

1 Like

thank you Gabe