Generate a Markdown Link From Selected Text with Chrome's Current Url Using Alfred
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...
Duration
3 min
Access
Free
Transcript
Available
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.
Terminal
url=$(osascript -e 'tell application "Google Chrome" to return URL of active tab of front window')echo "[$1]($url)"
Video Transcript
John Lindquist: 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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.