Jump to content

Workflows for Safari: create an HTML link with selected text or title


Recommended Posts

I asked ChatGPT to write Alfred workflows to create an HTML link with selected text or title from the current tab in Safari. It took about six tries for AI to get them right. Granted, it’s easy to create links in Markdown or with aText or TextExpander doing the tedious stuff, but I wanted to see if a workflow could do it in HTML (especially useful, I’d say, for those of us who still blog and are making links on the fly). I used a Hotkey, so there’s nothing to show in the Alfred window.

 

URL+text: It’s necessary to highlight text, press Command-C, and then press the Hotkey. If anyone knows how to have a workflow recognize highlighted text without the need for Command-C, I’d love to know.

 

#!/bin/bash
text=$(pbpaste)
url=$(osascript -e 'tell application "Safari" to return URL of front document')
link="<a href=\"$url\">$text</a>"
echo $link | pbcopy

 

URL+title: All that’s needed is to press the Hotkey.

 

#!/bin/bash
title=$(osascript -e 'tell application "Safari" to return name of front document')
url=$(osascript -e 'tell application "Safari" to return URL of front document')
link="<a href=\"$url\">$title</a>"
echo $link | pbcopy

 

I’ve placed the workflows in a Dropbox folder for downloading:

 

URL+text: https://www.dropbox.com/s/1v1w4qfn28rt9ye/URL%2Btext.alfredworkflow?dl=0

 

URL+title: https://www.dropbox.com/s/thd9o8kogr6cvbl/URL%2Btitle.alfredworkflow?dl=0

 

I’m no coder, and I hope what I’m sharing here is appropriate and useful.

Link to comment
48 minutes ago, Stephen_C said:

Use a Universal Action trigger limited to text?

 

Stephen

Stephen, thanks for your suggestion. Could you explain where that would go?

 

Vitor, thanks for your suggestions. I’ll look at the Automation Task to create Markdown links. I’m in way over my head here, but that’s often a good way to learn.

 

I should mention that the Safari extension URL Linker doesn’t work properly in Ventura. A working extension would make all of this a lot simpler.

Edited by Michael Leddy
Link to comment
28 minutes ago, Michael Leddy said:

Could you explain where that would go?

Right at the start of the workflow. Take a look at the link in my previous email and the link also on the help pages about universal actions[1].

 

Edit: [1] See here.

 

Stephen

Edited by Stephen_C
Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...