mbert Posted August 21, 2021 Share Posted August 21, 2021 (edited) I am trying to solve the follwoing: Select some text in a browser window. The selection contains links. Extract all links from the selection and open each of them in a nw browser tab. The use case is being able to open all new posts in a forum or something similar in one go. Once I have a set of URLs I guess the rest is trivial. But how do I get the markup from which to extract the URLs? When I do a regular MacOS selection I still have rich text containing stuff like links. When assigning this to a hotkey in an Alfred workflow the meta data is lost, and I have only the plain text which is of course not what I want. Ideally I would even like to use a rectangular selection rather than the OS selection because that would allow me to limit the region from which to extract the URLs more precisely. But I haven't seen any custom selection in the workflow toolbox. Anyone got an idea how I could approach this? Edited August 21, 2021 by mbert Link to comment
deanishe Posted August 21, 2021 Share Posted August 21, 2021 If you want to do the link extraction in Alfred, you'll have to implement your own "get current selection", as Alfred will not give you rich text or HTML. I can point you at the code to for that if you like, but I think injecting JavaScript into the webpage is probably a better idea. Chrome and Safari have APIs for that. mbert 1 Link to comment
mbert Posted August 22, 2021 Author Share Posted August 22, 2021 (edited) Thank you. I probably tied my thinking too much to Alfred's APIs. Using a combination of AppleScript and shell scripting it is actually quite easy. The following shell script does it (and can also be used outside of Alfred): #!/bin/sh extractUrls() { cat | \ tr '>' '\n' | \ grep -i 'href=' | \ tr "'" '"' | \ perl -pe 's/.*href=["]+([^"]+).*/$1/' | \ sort | \ uniq } doHtml() { osascript -e 'the clipboard as «class HTML»' | \ perl -ne 'print chr foreach unpack("C*",pack("H*",substr($_,11,-3)))' | \ extractUrls } doRtfd() { osascript -e 'the clipboard as «class rtfd»' | \ perl -ne 'print chr foreach unpack("C*",pack("H*",substr($_,11,-3)))' | \ textutil -stdin -stdout -convert html -format rtfd | \ extractUrls } CLASSES="`osascript -e 'the clipboard as record' | \ tr '«' '\n' | \ grep 'class.*»' | \ perl -pe 's/^class +([^»]+)».*/$1/'`" #echo "classes: $CLASSES" URLS="" for class in $CLASSES; do case $class in HTML) URLS="`doHtml`"; break;; rtfd) URLS="`doRtfd`"; break;; esac done for url in $URLS; do # echo "$url" open "$url" done I have implemented support for two classes that can be in the clipboard HTML - this is what my browser gives me. rftd - from TextEdit.app Support for additional classes will be easy to add. In the end I simply run the 'open' command on all URLs. In my browser this will open all URLs in separate tabs. Maybe there's a more "alfredish" way of doing this, e.g. by triggering a loop of actions that are way more customisable, like using particular browsers etc.? Edited August 22, 2021 by mbert Link to comment
deanishe Posted August 22, 2021 Share Posted August 22, 2021 (edited) 10 minutes ago, mbert said: HTML - this is what my browser gives me. Firefox? Everything else uses RTF, AFAIK. 10 minutes ago, mbert said: triggering a loop of actions that are way more customisable, like using particular browsers etc.? Alfred doesn't support loops. That has to be done in code. The way you're doing it is the usual way. Edited August 22, 2021 by deanishe Link to comment
mbert Posted August 22, 2021 Author Share Posted August 22, 2021 Just now, deanishe said: Firefox? Everything else uses RTF, AFAIK. I am using Vivaldi. It's based on Chrome. But if something uses RTF adding a handler for it based on 'rtfd' should be straightforward. Link to comment
mbert Posted August 22, 2021 Author Share Posted August 22, 2021 3 minutes ago, mbert said: I am using Vivaldi. It's based on Chrome. But if something uses RTF adding a handler for it based on 'rtfd' should be straightforward. Just checked, Chrome returns HTML, and so does Firefox. Link to comment
deanishe Posted August 22, 2021 Share Posted August 22, 2021 11 minutes ago, mbert said: Just checked, Chrome returns HTML, and so does Firefox. Makes sense. I guess it's just pasting RTF that Firefox doesn't like. Link to comment
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now