Copy selected link(s) from browser as markdown?

Hey Rob,

This is not entirely true now with advent of ASObjC:

use framework "Foundation"

set myRecord to {var1:"User", var2:"Record"}
set recordKeyList to (current application's NSDictionary's dictionaryWithDictionary:myRecord)'s allKeys() as list

Unfortunately this method will only work with user-records and not system-records.

In this particular instance with the clipboard we can do something like this:


on clipboardContainsUnicodeText()
  repeat with i in (get clipboard info)
    if i contains Unicode text then
      return true
    end if
  end repeat
  return false
end clipboardContainsUnicodeText

Although I will admit it is seriously kludgey.

Or to be a bit more flexible:

recordLabelsContain(Unicode text)

on recordLabelsContain(labelName)
  set labelList to {}
  set clipboardInfoRec to clipboard info
  repeat with i in clipboardInfoRec
    set end of labelList to item 1 of i
  end repeat
  return labelName is in labelList
end recordLabelsContain


1 Like

Thanks Chris – that’s helpful, and it would be interesting to experiment with reading and writing NSPasteboard through the Yosemite ObjC bridge (built into the AS and JXA libraries).

OK, after much help from @ComplexPoint, @peternlewis, & @ccstone, I have compiled an AppleScript that achieves #1 on my ideal solution list: Copy selection of hyperlink from any document and convert into clean RTF hyperlink and Plain Text Markdown Link.

Download the AppleScript from here:
CB MD Put Hyperlink & MD Link on (15.8 KB)

Here's just the top of the file:

property gstrScriptName : "CB MD Put Hyperlink & MD Link on Clipboard"
property gstrScriptVer : "BETA 0.2"
property gstrScriptDate : "Wed, Jul 22, 2015"

STATUS:  This script is BETA.  It is INCOMPLETE and is a work in progress.  Use for testing/commenting only.

	• Based on the User's selection, create and put on Clipboard:
		• RTF formatted Hyperlink
		• Plain Text Markdown text for hyperlink

	• The data for the OUTPUT hyperlinks will be determined as follows:
1st BETA	• IF Selection is a RTF/HTML Hyperlink from a Web page or RTF document
			• URL        = URL of the hyperlink
			• Link Text = Link Text of the hyperlink
NOT Done	• IF Selection is only Text with no link:
			• Link Text = Selected text
			• URL        =
				• IF Web page, THEN URL of page
				• ELSE set to "NoURL"
NOT Done	• IF NO Selection is made AND Current App is a Web app:
			• Link Text = Web page Title
			• URL        = Web page URL

NOT Done	• IF NO Selection is made AND Current App is NOT a Web app:
			• Display ERROR msg, asking user to select a hyperlink or a web page.

It is still very much a BETA, actually an ALPHA, since it is INCOMPLETE, and has had only very limited test. I have NOT yet put any error checking/handling into it.

@ComplexPoint, I know you preferred to use Javascript to parse the hyperlink, but since I want this to work with both RTF documents and web pages, I have chosen to use RegEx. Seems to work OK, but definitely needs more testing.

If anyone has a better RegEx pattern, please let me know.

This requires Satimage AppleScript Extensions

Please give me your bug reports, comments, suggestions, improvements. Feel free to be as blunt as you'd like -- I have a thick skin. :slight_smile: I just want to make this be a great tool for all of us.

As I said, it is INCOMPLETE and doesn't have any error checking/handling.
It is a rough draft of the 1st use case. At this point I'm not sure what the best integration with KM is, so let me know if you have any ideas about that.

@ComplexPoint, cases #2 and #3 will require access to the web app/page. I know you've given a number of code examples and suggestions, but let me know you think is best at this point. I'm not sure how to best use JavaScript (web) from AppleScript. Go through KM?

I decided not to post the code here because it is so long due to header comments. It's about 4 screen, but I have a lot of comments and white space.
You guys let me know if you'd like it posted here.

I was on my way for a little bit of reading (old spy novels) and then bed when it hit me on how to make best use of KM.

These are my late night thoughts, so you guys feel free to jump in.

Start the process with KM.
Using KM Actions, determine the front app.
If it is a web app, get the page title and URL into KM variables, using whatever Javascript is best. Or can KM Actions get this?
IAC, pass to AppleScript as KM variables.
Also determine whether or not the user has selected anything, and set a KM variable.

Basically pass to AppleScript via KM variable all the data that’s needed to decide how to build the links, and the required data.

OK, I’m rambling now, so I’m off to reading/bed.
Look forward to your feedback tomorrow (or later today actually) :smile:

There are text tokens for Safari/Chrome front window title and URL (eg SafariTitle).

Whatever works is usually the approach, I think : -)
and whatever you want to learn is a good one too …

( if you hit any bumps in the regex path, you can still use RTF → HTML → Browser JS )

I'm not sure how to best use JavaScript (web) from AppleScript. Go through KM?

It always makes sense to take full advantage of the KM tools. The do script method of the "Keyboard Maestro Engine" scripting interface works well with XML versions of the built-in actions.

(Just export a prefilled Execute JS in Chrome|Safari action, open the file in a text editor, and extract the parts you want)


(The direct script-to-browser JS approach is also possible, but easier in JXA than in AS)

Thanks for all your support, Rob. When I get done with the entire macro/applescript, I'm definitely going to take a look at using Browser JS to parse the link.

I have learned a lot through this process, and a lot of it because of you. I did learn how to setup RegEx using the Satimage AS extension. It was a bit tricky.

Thanks Peter. Any ideas on how do this with the FireFox browser?

That sounds tough … Firefox may not be a good browser to reach for if it’s scriptability that you hope for.

1 Like

Wow! What a PITA Firefox is.

But I may have figured out a way to get Page Title & URL.
Haven't tried it yet in a KM macro, but just by typing:

CMD-I     [opens the Info window]
TAB       [moves to the Page Title, and selects]
CMD-C     [copy Title]
TAB       [moves to the Page URL, and selects]
CMD-C     [copy URL]
CMD-W     [closes the Info window]

See any issues with this approach?

1 Like

Hey Michael,

You can simplify that a bit.

To get the title:

tell application "Firefox" to return name of front window



To get the URL:


Pause 0.05



Thanks Chris. That is much simpler and better.
I'll make use of those in my next update.

BTW, you should post that out on the MacScripter board. I searched far and wide and didn't find what you just posted. But maybe I missed it in all the noise. :smile:

Chris, thanks again for your help and explicit instructions on
How to Get FireFox Page Title & URL

I just wanted to upload the AppleScript I created using your design for anyone that's interested:

Original version here -- see below for revised script:
FF How to Get FireFox Page Title & (3.6 KB)

UPDATE Thu, Jul 23, 2015 at 4:44 PM

After a discussion with @ccstone, he came up with a technique to maintain the user's selection while still getting the page data.
The the revised script, with the code being provided by Chris, and I just added a few comments.

FF How to Get FireFox Page Title & URL & Maintain (4.2 KB)

While it's a great idea in most cases, I can't use this approach because I need to get and maintain the user's selection on the page. After I get the Browser data, I call an AppleScript to process the Clipboard.

I could save and restore the System Clipboard, but that would not restore the user's selection.

So looks like I will have to stick with what I have using the Info window.

Hey Michael,

Okay, try this.

tell application "System Events"
  tell application process "Firefox"
    set frontmost to true
    set strPageTitle to name of front window
    keystroke "l" using command down -- CMD-L to select URL in Address bar
    keystroke "c" using command down -- CMD-C
    delay 0.1 -- small delay to allow app to complete COPY command.
    keystroke "f" using command down
    key code 53
  end tell
  set strURL to the clipboard
end tell


OK, my friend. Well done! :smile:

Can you please explain to me how you restored the selection?

Hey Michael,

I noticed that when you type Cmd-L the selection is maintained, and only when you tab back into the page-body is it lost — so I wondered if there was a way to maintain it and tried some of the obvious keyboard shortcuts without success.

Then I wondered about find (Cmd-F), and sure enough find followed by <Escape> maintained the selection.

key code 53 types <Escape>.


You are a genius! Many thanks for another great solution!

I updated my post above to add the AppleScript download revised per @ccstone's changes above. This is a real keeper. Anyone who has a need to pull data from FireFox should download/archive this script.

Rob, I was trying to use your JavaScript (at bottom of this post) to pull the href from a selected link in Chrome.
It works fine on a normal web page, but on an web app form, it returns nothing.
Can you please suggest any changes to make your code pull the HREF from the below HTML? Many thanks.

Using the Chrome Inspector, I copied the HTML code for this link:

<a data-cke-saved-href="file:///Users/Shared/Dropbox/Test-File-Link.scpt"

Just so you can see the big picture, here's a screen shot of the web form:

##Here's your JavaScript:

(function (oNode) {

	var oLink = document.evaluate(
		null, 0, 0

	return oLink ? 
		"[" + oLink.text + "](" + oLink.href + ")" :