Selecting text and adding a hyperlink

I know I can add a url and have that text automatically turned into a link, but how do I select text and turn that text into a link to a URL I specify?

4 Likes

I would really like to see this feature implemented too.

I did finally figure out that you can paste in a URL, then select it and change the dispayed text to be something different, which gives you the same results in a roundabout way. It would still be nice to be able to select text and turn it into a link.

2 Likes

Great idea and it should have the keyboard shortcut Cmd-K which is used by TextEdit, Mail, among others.

1 Like

Me too was facing the same issue. Impressed with your answer. Thanks

@philipt18 said

you can paste in a URL, then select it and change the dispayed text to be something different

Just fyi ā€” this would be quick work with Keyboard Maestro, or any macro program.

That said, it should be built-in to OO, imho. Afaik, Apple has made this link-making available platform-wide.

Just fyi ā€” this would be quick work with Keyboard Maestro, or any macro program.

Tho ā€“ perhaps I am missing something ā€“ it doesnā€™t look as if the scripting API gives access to the text selection ?

(It appears to expose only row selection, which is not really what you need, but I suppose you could still use the clipboard and textutil to build and paste an RTF encoded link)

Perhaps quick -> quick-ishā€¦ ?

(Not hard to see the appeal of widget-free Markdown at these moments ā€¦ )

In the meanwhile, typing the url first, and then ctrl-clicking to choose Edit Link and get this:

does seem workable ā€¦

PS to speed the work up a bit, in case anyone does try that with something like Keyboard Maestro, an AppleScript incantation for building the RTF might look something like:

if (strURL is not "") and (strTitle is not "") then
	set strHTML to quoted form of ("<font face=\"helvetica\"><a href=\"" & strURL & "\">" & strTitle & "</a></font>")
	do shell script "echo " & strHTML & "  | textutil -format html -convert rtf -stdin -stdout | pbcopy -Prefer rtf"
end if

Keyboard Maestro and AppleScript are different programs. They work differently. Check out Keyboard Maestro ā€” itā€™s a superb and in many ways exemplary program (just a satisfied user).

Here is a thread I turned up with, imho, helpful information for scripting (I donā€™t use AppleScript, only due to ignorance).

Here is a quickly-written Keyboard Maestro macro. It copies the selected text (in OO), pastes it into a TextEdit RTF file, adds the previous System Clipboard as a link, copies the newly created link, and pastes it OO. I used TextEdit in order to get to the ā€œEdit ā–¹ Linkā€ command (which OO doesnā€™t give us access to). Iā€™m sure someone skilled could improve it. As is, it works.

Looks good ā€“ I guess you could also bypass TextEdit, if you wanted to.

There is a draft of a āŒ˜K at

which:

  1. Copies the text selected in OO4
  2. Prompts for a url
  3. Pastes back as a rich text hyperlink

1 Like

Thanks for this. I did check it out on the KM site as well.

I would probably have by-passed TextEdit if I knew AppleScript šŸ˜Š .

1 Like

The suggestions the work-arounds by the responders (as well as their programming insights about how OF works) is downright impressive so kudos to all of you! To a non-programmer non-tinkerer who simply wants a streamlined experience with common key commands and shortcuts consistent across Appleā€™s OS, these suggestions are work-arounds which would not be necessary if the good folks at the Omni Group could add the Cmd-K convention referenced by several responders. Hereā€™s to hoping they are monitoring these forums. Cheers everyone.

3 Likes

I so love Omni that I hesitate to say anything, but Iā€™m sincerely surprised at the round-about way necessary to create links. As many have noted, most apps I use allow Command-K. Is this not a standard, Apple way to do things? Is it standard, but thereā€™s a problem implementing it with Omniā€™s code (perhaps a carryover from NeXT code)? Iā€™m more than a bit surprised that OO5 still does it this way.

I hope this issue is ā€œfixedā€ā€¦and Iā€™m certainly open to learning why the current method is ā€œbetterā€ or ā€œmore powerfulā€ or ā€œmore elegant.ā€

1 Like

Here is an applescript snippet that will change the text of the selected row into a link based on the clipboardā€™s contents. Excessive apostrophe alert is in effect.

tell application "OmniOutliner"
	tell selected row of document 1
		set topic cell's text's style's attribute "link"'s value to the clipboard
	end tell
end tell
1 Like

Helpful that everyone submitted scripts and workarounds, but very surprising that this is not part of the text editor.

Even from AppleScript I wasnā€™t able to find a way to add a hyperlink to just the selected text, rather than the entire rowā€™s text ā€” because I couldnā€™t get the selected text from AppleScript.

I could almost certainly do it by copying the selected text and modifying the clipboard to add a link with AppleScriptObjC, or even making a service to do it, but this seems like a lot of work for something thatā€™s implemented in TextEdit and as @ibuys mentions, should probably be part of an 18 year old product!

I ended up making a slightly longer version of the script that prompts for the link destination, using the clipboard as the default:

tell application "OmniOutliner"
	local _link, _result
	set _link to ""
	try
		set _link to (the clipboard as string)
	end try
	set _result to (display dialog Ā¬
		"Link selected rowā€™s text to destination:" default answer _link Ā¬
		buttons {"Cancel", "Link"} default button 2)
	set _link to _result's text returned
	tell selected row of document 1
		set topic cell's text's style's attribute "link"'s value to _link
	end tell
end tell

And a corresponding script to remove links from the selected rows (useful when copying from OmniFocus into OmniOutliner to clear all the omnifocus:// links):

tell application "OmniOutliner"
	tell selected row of document 1
		clear topic cell's text's style's attribute "link"
	end tell
end tell

I havenā€™t found a way to accomplish this with selected text but with a more explicit reference, you can en-link a segment of a topic. (Just made that word up :)

set word's style's attribute "link"'s value of words 1 thru 2 of topic of child 1 of document 1 to the clipboard

It does mean that you have to figure out how to point to the text you wish to add the link to. That can be a pain. You should also be able to work with characters rather than words, etcā€¦. Perhaps if you could properly convert the reference to the selected text to something more explicit, then you could turn this into a service and give it a keyboard shortcut. Maybe after I sleep for a while. I do hope itā€™s possible.

As an aside, I find that a lot of apps have issues when working with the selected text. And as a further aside, I find TextEdit to be worse than any app that implements Applescript. Iā€™ve spent (or wasted) untold hours slamming my head against those rocks.