More Making Things Easier

Responding to yesterday’s post, Sacha asks if I could post the code for jcs-insert-url for others to use. I thought I’d already done that but apparently not. That’s probably because except for the part identical to jcs-get-link, which I did write about, it’s pretty trivial. In any event, here it is:

(defun jcs-insert-url ()
  "Insert URL of current browser page into Emacs buffer."
  (interactive)
  (insert (jcs-retrieve-url)))

The jcs-retrieve-url function does all the work, of course, and is just the code that I abstracted out of jcs-get-link to actually retrieve the URL from Safari:

(defun jcs-retrieve-url ()
  "Retrieve the URL of the current Safari page as a string."
  (org-trim (shell-command-to-string
    "osascript -e 'tell application \"Safari\" to return URL of document 1'")))

One obvious problem with all this is that it works only for macOS. Not to despair, though, because in the comments to the original post, Brad Collins suggests a solution that uses grab-x-link to do the same thing for FireFox and Chrome on other systems. Be sure to read Brad’s comment because there is—or at least was—an issue with the MELPA version.

Finally, Sacha took the part about looking for ways to make your workflow easier seriously and came up with a bit of Elisp to insert a function definition at the point, regardless of where it’s defined. That’s very handy and I immediately stole her code and used it to insert the two functions above. My old method was to switch to init.el, find the function, copy it to the kill ring, switch back to the original buffer, add the source block fences, and insert the code between them. Sacha’s code did all of that for me and I didn’t even have to leave my current buffer. That’s splendid. If you find yourself having to add function definitions to your text, be sure to read Sacha’s post. It will save you a lot of time.

This entry was posted in General and tagged . Bookmark the permalink.