Open Chrome Link Based on Partial URL

I want to automate opening a partially known Chrome link. The link will always be safe, on full display, in a similar location, and start with "https://app.slack.com/client/”. The last 20 *characters will be different each time.

The Xpath varies slightly every time, and a CRM generates the link.

Can anyone help me save hundreds of mouse clicks a week?

"Charters"... Ah, characters! :slight_smile:

Your macro could pass the URL to Chrome by using the Open a URL action.

If you had the client identifier in a variable, say in local variable local client, the URL would be: https://app.slack.com/client/%Variable%local client%

I hope that will help you get started.

I'm trying to understand exactly what you're physically doing in order to figure out how/if it's solvable. I think what you're doing is this:

You have a window open in your CRM, and it contains a link that's been generated on the fly. You can see the full text of the link at a location on your screen, and that location is similar every time.

You need to click that link to have it open in your browser, but you can't get the actual link with any intelligence, because the Xpath is never the same, and there's no way to programmatically interact with the CRM. Is that correct?

If that is correct, considering the CRM for a second ... can you interact with that link in the CRM in any way to copy the link? Right click on it and choose Copy Link? Menu bar command? Something else?

-rob.

The link is pre-generated. The ideal solution would be to open and join the Slack channel in the Slack app (then move and resize but I can do that), but opening the channel and joining in Chrome is the next best option. The CRM is clickable, right-clickable, and copyable—it's just a link at the bottom of the page in the job history. I do not have permissions to interact with the CRM (Service Titan) beyond the web interface.

Will there ever only be one there in that section of the page? If so, you could probably use OCR on that region of the screen, then just search the text for the link—a regex that found a line starting with https://app.slack.com/client... through the end of the line would work.

Extract it as a variable, and then use it to open the channel in Slack or whatever.

-rob.

There will rarely be another link- I'll look into OCR, thanks!

Make sure you set the language to "Apple Text Recognition." It's the fastest and most accurate, with very little CPU hit. You can either OCR the front window, or first take a screenshot of the region with the link to the clipboard, then OCR the clipboard—that might be better than OCRing the entire window.

-rob.

But the web interface does include getting the page's content, possibly JavaScripting "get the innerText of any element of class whatever" and grepping that for the URL, etc.

It's going to be tricky to do without seeing the page source. But try turning on Chrome's Developer Tools, right-clicking the link, then selecting "Inspect". You're looking for something -- anything -- in that element or its parents that you can hook onto.

And there's always the brute force method -- use AppleScript to get either the entire text or source of the page, then search that for any Slack URLs.

Does anything obvious stand out to you? This is several levels beyond my capability.

Mine too! You could probably target via the list (li) great-great-grand-parent element 18 lines above the highlight, but that seems like a lot of work (as in "googling to find out how to do that"!).

Probably easier to grab all the links on the page and extract everything pointing to Slack -- and a quick google on that led straight back to this Forum :wink:

@ComplexPoint will be round to correct my typos soon, but this might work (testing is a little difficult, obviously!):

Grab Slack Link(s).kmmacros (2.4 KB)

If I've got that right it should give you a list of the page's Slack links, one per line, which you can use in a "For Each" action to open them in new Chrome tabs/windows.

1 Like

The only real issue there is that you are using the old (insecure) syntax which writes all the Keyboard Maestro variables and values into the document object, leaving them visible, even after the macro has finished, to other scripts.

The first trick is to:

  1. Choose the modern syntax from the drop-down menu behind the little chevron to the left of the code field
  2. Restrict the variables that will be written to a custom (and temporary) kmvar object (checking the name(s) of any specific variable(s) which you will use in the script.
  3. In your code, reference the exported KM variables with a simple kmvar. prefix (not document.kmvar)

Screenshot 2024-10-27 at 9.25.50 pm


Once you have chosen modern syntax, you will need an explict return for any value that will be returned from the action (for display, binding to a variable name, etc)

The simplest way to harvest a full (and subsequently filterable) JS Array of href strings for the front page is to write:

return [...document.links].map(
    x => x.href
)
.join("\n")

(Where the [... ] spread operator derives an Array from the document.links collection).

So, if you are looking for links, on the front Chrome page, which contain the string leibniz,
you might write something like:

Filtered links.kmmacros (3.1 KB)


const pattern = kmvar.local_SearchString;

return [...document.links].flatMap(link =>  {
    const href = link.href;

    return href.includes(pattern)
        ? [href]
        : [];
})
.join("\n");
2 Likes

Which just goes to show how things have changed in the last eight years!

1 Like

THANK YOU! ComplexPoint and Nige_S- I used Filtered links.kmmacros, saved the results to variable and opened the variable as a URL. It works perfect.

1 Like

After fumbling through your solution (which works exactly as I inquired), I think it would be better to harvest only the Slack channel ID that begins with /C, which allows me to open the channel in the Slack app. I figured out how to do the second half, but is there an easy way to only grab the ID?

Screenshot 2024-10-28 at 5.25.47 AM

Yep -- do it in KM after you've run the JavaScript. You probably want "everything after the last /" since (I think) the Channel ID includes the leading C.

So:

image

"Split the contents of the variable Local_slackLink on the / character and put the last (the -1th) item into the variable Local_chanID."

(It's good practice to use local variables unless there's a need (persistence, data sharing) for a global, so I've used a local here. Have a read of the manual's section about Scope for more.)

You'll have to add your own "do something in Slack" action(s), but:

Filtered links (Channel ID only).kmmacros (4.0 KB)

Image