Generate a Markdown Link From Selected Text with Chrome's Current Url Using Alfred

John Lindquist
InstructorJohn Lindquist
Share this video with your friends

Social Share Links

Send Tweet
Published 4 years ago
Updated 3 years ago

Alfred is able to take your currently selected text and manipulate it in a variety of ways. You can even link it up to bash scripts which can grab data from other applications on your system. This lesson will walk you through generating a markdown link from the text you select and the currently open URL in Chrome.

url=$(osascript -e 'tell application "Google Chrome" to return URL of active tab of front window')

echo "[$1]($url)"

John Lindquist: [0:00] Create a blank workflow in Alfred. We'll call this md-link and pick a Bundle ID. I'll just call mine mdlink, hit Create, and right click and pick a Trigger of hotkey.

[0:13] We'll choose the hotkey of Command M, which I'll just type on my keyboard and the argument from the drop down should be selection in macOS. This will allow us to pass the currently selected text into the workflow. Hit Save here, then our output will be a copy to clipboard, and we'll show it pasting that same text three times.

[0:36] You'll also want to check automatically paste the frontmost app, click save here, and then connect these together by clicking and dragging. Now, when I hit Command M, it should copy and paste three times.

[0:48] I'll switch over to a text editor, I'll type something, select it, hit Command M. You'll see the result is type something three times which was the selection in Mac OS. Then Alfred copied it and pasted it three times.

[1:05] Now, what we want is access to the current URL inside of Chrome. That's something we can grab using a tool called osascript. I have the code here, which I'm going to copy and paste into a script action.

[1:19] I'll create that here, action being a Run Script. If I paste that here, this will grab the current URL inside of Chrome. This essentially invokes osascript and tells it to grab Google Chrome and the current URL of the active tab.

[1:37] If I hit Save here and connect these together like this, I'll delete this one to select it and hit Delete, and then connect these. You'll see Command M will trigger grabbing the current URL, which will trigger copying that to the clipboard three times.

[1:53] Now, if I delete all this, I'll type something, I'll select it, hit Command M, and you'll see the URL will come out three separate times. To bring the current selection and the URL together, we'll do that inside of our script by opening this. Instead of echoing out the result, we'll set this to a variable called URL with no whitespace.

[2:16] Now, we'll echo-out the markdown syntax for a link. Two square brackets will be the text and two parens will make up the link. Inside of the square brackets, we'll put $1 which represents the first argument or the argument coming in as the selected text.

[2:32] For the link, we'll put $url, which is this URL right here. I'll hit Save here, now when I go into here and type out something. I'll select it, I'll hit Command M, and you'll see it did something inside of a link and it pasted all of that three times.

[2:50] Let's remove those three. It's just a single one. Now, we'll hit Save. Now the workflow will be something like, "I enjoy creating node.js CLI tools." I would select this text, hit Command M and it would generate the markdown link for me with the current Chrome URL.

egghead
egghead
~ 40 minutes ago

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today