Recently, I was rereading Vivek Haldar's post on the seven levels of Emacs proficiency and suddenly realized that I'd regressed in one aspect of my Emacs skills. Haldar says
Up until now, you probably had one large Emacs window plus many other
shell windows spread out on your screen. Also, if you are a typical
developer, you often had to cut and paste text between those windows.
And that was a major road bump, because you had to use the mouse to
select text in an xterm.
I thought I'd long since stopped committing that sin but my epiphany upon rereading Haldar's post was that I was still guilty of it in one important case: grabbing links to Web pages. Almost all my blog posts link to at least one Web page and often several. I generated those links in exactly the way that Haldar deprecates: change focus to the Web page, cut the URL from the title bar, switch back to Emacs, paste the URL into the Org link. Clearly, I needed a better method; something that would allow me to grab the URL of the currently displayed browser page and turn it into a link in my Org buffer without leaving Emacs.
The org-mac-link package almost does what I needed but it fills in the link description with the Web page title, which was not what I wanted. So I looked at the org-mac-link code and Kris Jenkins' excellent video on using AppleScript to play Spotify tracks from Emacs and put together a bit of Elisp that does just what I want:
(defun jcs-get-link (link) "Retrieve URL from current Safari page and prompt for description. Insert an Org link at point." (interactive "sLink Description: ") (let ((result (shell-command-to-string "osascript -e 'tell application \"Safari\" to return URL of document 1'"))) (insert (format "[[%s][%s]]" (org-trim result) link))))
Of course, this works only on OS X but that's OK for me because I write all my blog posts on one of my Macs. Doubtless other operating systems have a way to do the same thing1. You could, for example, find a way of doing this in Python and then call Python instead of AppleScript to retrieve the URL. If your main OS is Linux or Windows and this interests you, leave a comment for others if you find a way of doing it in your environment.