For context, every month I get an email with a list of links that I need to visit to download some invoices.
What I am trying to do is copy those links to the system clipboard, iterate over each line and open the corresponding url in a new tab.
This is all within chrome and gmail, and so far I haven't been able to get the value of href.
So far I have tried using an AppleScript to get the urls and using the action to set variable with regex. href=["']?(https?://[^"'>]+)
Nothing worked and I am starting to think that maybe I'm trying something that goes beyond Keyboard Maestro.
Has anyone ever done something similar?
Hi and welcome to the forum!
I think this should do it:
Open Any URLs in Clipboard Text.kmmacros (21 KB)
Macro screenshot
Thank you, @noisneil. It didn't work but your approach was helpful. Digging around the documentation for pbpaste, none of the options was showing anything else different from plain text.
The workaround I found was to use a python script from chatGPT:
import sys
from PyQt5.QtWidgets import QApplication
from bs4 import BeautifulSoup
def extract_urls_from_html(html_content):
soup = BeautifulSoup(html_content, 'html.parser')
anchors = soup.find_all('a')
urls = [a['href'] for a in anchors if a.get('href')]
return urls
def main():
app = QApplication(sys.argv)
clipboard = app.clipboard()
mime_data = clipboard.mimeData()
if mime_data.hasHtml():
html_content = mime_data.html()
urls = extract_urls_from_html(html_content)
if urls:
for url in urls:
print(url)
else:
print("No URLs found in clipboard content.")
else:
print("Clipboard does not contain HTML content.")
# You don't need to run the main loop of the QApplication, so we can exit here.
sys.exit()
if __name__ == "__main__":
main()
After, I compiled it with pyinstaller and changed the execute shell script action to it:
#!/bin/bash
/usr/local/bin/extract-urls
It's slow to run but gets a clean list of urls.
Open Any URLs in Clipboard Text.kmmacros (2.8 KB)
Thank you again !
The example I gave was based on manually copying the contents of an email that contains explicit URLs as text, as I thought that was what you meant.
Glad you figured it out.
1 Like