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.?