convert link in HTML to link in RTF

Is any idea how to convert clipboard contents “myURLAsSimpleText” into RTF format to be pasted as clickable link in any app (ie Mail)?
I found no info concerning to my problem in forum.

I can create HTML link as

set myURLAsSimpleText to the clipboard
set myHTMLClipboard to '<a href="' & myURLAsSimpleText & '">' & myURLAsSimpleText & '</a>'

But HOW to convert this string into RTF form to be pasted as RTF link?

I don’t know how to do this with applescript, but you can create clickable links in mail.app and many other programs too such as TextEdit. In Mail you go to the edit menu and there’s a menu item called link that you can use. In TextEdit again you go to the edit menu and there’s a menu item called ‘substitutions’. Under that menu you can select ‘Smart Links’ which will do what you want.

Basically I don’t think there’s really a way to use applescript to encode text in one format that will work for all applications. For example in Mail when you use that menu item, it changes the format of the email from rtf to html, and then uses the link format that you mentioned to create the link. So in that case it actually changed the email type to get links to work. Of course when you send a regular email in rtf format links are clickable by the receiver of the email… unless they choose to receive all emails as text only.

In TextEdit I don’t know how they do it, but you can copy/paste those enabled links other places.

Mail isn’t very good at dealing with html. You can either:

  1. Create an html file, open it in Safari, tell Safari to “Mail Contents of This Page”, but then you’ll have difficulty getting Applescript to set a recipient. If you figure that part out, please post the solution!

  2. Paste the text of the actual link and let mail convert it:
    html://www.somesite.com
    It’s not as nice as an embedded link, but it works. :frowning:

Well, I choose emulating keystrokes as workaround.

tell application "Mail" to activate
tell application "System Events"
	tell process "Mail"
		keystroke "T" using {command down, shift down}
		click menu item "Add." of menu "Link" of menu item "Link" of menu "Edit" of menu bar 1
		tell window 1
			delay 2
			keystroke "V" using {command down}
			key code 36
		end tell
	end tell
end tell

But in this case script stuck on window Link/Add…
It seems that I should focus script on this window before pressing Paste key.

How can I point on this window?
“window 1” is not worked.

This was an interesting question and one that I had to see if I could find an answer to. I located something close to what you were trying to do from http://dltj.org/article/copy-last-sent-message-as-rtf-link/ but needed to modify it a bit.

Re-creating the .sh file that Brett Terpstra had written was easy enough: contents of rtflink.sh

After saving the rtflink.sh file to my desktop and then changing the code a bit from the Jester’s (Peter E. Murray) page, I ended with something like this for the Applescript file:

set _script to POSIX path of (path to desktop as Unicode text)
set AppleScript's text item delimiters to " "
set _dirty to text items of _script
set AppleScript's text item delimiters to "\\ "
set _script to (_dirty as text) & "rtflink.sh"
tell application "Safari"
	set theUrl to URL of front document
end tell
set _anchorText to "New URL"
do shell script "/bin/bash -c \"" & _script & " \\\"" & _anchorText & "\\\" \\\"" & theUrl & "\\\"\""

Afterwards, when I run the script, presto… I have a clickable RTF link that I can paste into any app that recognizes RTF.

Also from PeewwJD comes this statement: I have a bookmarklet on my computer at work that I can click. When I click it, it creates a mail message to my home e-mail account with a link to the current page in the browser window and the title of the webpage as the e-mail subject.

It works in any borwser that supports bookmarklets (including safari, firefox/mozilla and others). I have it installed on my bookmarks toolbar for easy/fast access.

And from jago lebow comes this one:
If you enter the URL mail-link: in Safari’s address bar, it will tell your default mail client to create a new message with a link to the current page – just the same as selecting File: Mail Link to This Page. You could put these links on your website for Safari users, or make bookmarks from them…

Chuck

That’s really cool soboks. I ran it how you said and it works.

So I’m trying to make it just an applescript so I don’t need the shell script file… then I can do other stuff. What’s weird is I’m having trouble. This first applescript works (uses keystroke commands) but the second one”the one I want to work”doesn’t (uses do shell script). Does anyone have a clue why? It’s the same thing, just 2 different techniques yet one works and one doesn’t???

set theLink to "http://www.hamsoftengineering.com"
set linkText to "click here"

set cmd to "\"{\\rtf1\\ansi\\ansicpg1252\\cocoartf949\\cocoasubrtf270{\\fonttbl\\f0\\fswiss\\fcharset0 Helvetica;}{\\colortbl;\\red255\\green255\\blue255;}\\margl1440\\margr1440\\vieww9000\\viewh8400\\viewkind0\\pard\\tx720\\tx1440\\tx2160\\tx2880\\tx3600\\tx4320\\tx5040\\tx5760\\tx6480\\tx7200\\tx7920\\tx8640\\ql\\qnatural\\pardirnatural{\\field{\\*\\fldinst{HYPERLINK \"" & theLink & "\"}}{\\fldrslt\\f0\\fs24 \\cf0 \"" & linkText & "\"}}}\""

tell application "Terminal" to activate
tell application "System Events"
	keystroke "echo " & cmd & " | pbcopy -Prefer rtf"
end tell
set theLink to "http://www.hamsoftengineering.com"
set linkText to "click here"

set cmd to "\"{\\rtf1\\ansi\\ansicpg1252\\cocoartf949\\cocoasubrtf270{\\fonttbl\\f0\\fswiss\\fcharset0 Helvetica;}{\\colortbl;\\red255\\green255\\blue255;}\\margl1440\\margr1440\\vieww9000\\viewh8400\\viewkind0\\pard\\tx720\\tx1440\\tx2160\\tx2880\\tx3600\\tx4320\\tx5040\\tx5760\\tx6480\\tx7200\\tx7920\\tx8640\\ql\\qnatural\\pardirnatural{\\field{\\*\\fldinst{HYPERLINK \"" & theLink & "\"}}{\\fldrslt\\f0\\fs24 \\cf0 \"" & linkText & "\"}}}\""

do shell script "echo " & cmd & " | pbcopy -Prefer rtf"

regulus6633, try this one:

set theLink to "http://www.hamsoftengineering.com"
set linkText to "click here"

set cmd to "\"{\\rtf1\\ansi\\ansicpg1252\\cocoartf949\\cocoasubrtf270{\\fonttbl\\f0\\fswiss\\fcharset0 Helvetica;}{\\colortbl;\\red255\\green255\\blue255;}\\margl1440\\margr1440\\vieww9000\\viewh8400\\viewkind0\\pard\\tx720\\tx1440\\tx2160\\tx2880\\tx3600\\tx4320\\tx5040\\tx5760\\tx6480\\tx7200\\tx7920\\tx8640\\ql\\qnatural\\pardirnatural{\\field{\\*\\fldinst{HYPERLINK \"" & theLink & "\"}}{\\fldrslt\\f0\\fs24 \\cf0 \"" & linkText & "\"}}}\""

do shell script "/bin/bash -c 'echo " & cmd & " | pbcopy -Prefer rtf'"

chuck

Perfect! I never would have figured out that -c option. Thanks. :slight_smile:

Here’s a script I wrote to use this:

-- this will create an rtf link on the clipboard so you can paste it anywhere that accepts rtf
-- I noticed TextEdit plain text documents work too

-- first a dialog box will ask you for the link
-- second a dialog box will ask you for the words to represent the link
-- third the result is placed on the clipboard for pasting

-- there is an option to add 'http://' to any link that doesn't have '://' in it... set the variable 'addHTTP' to true or false
-- there is an option to replace spaces in the link with %20... set the variable 'replaceSpaces' to true or false

-- Note: use the enter key to activate the default button 'OK' in the first dialog box from the keyboard. The return key doesn't activate it as a result of the larger text entry field in the dialog box. You can make the return key work by removing '& return' in the first display dialog command... but the text entry box will only be one line if you do that. I think since links are generally long that the larger box is useful. You can make the box even larger by adding more '& return' to the command if you wish too.
-- Note: the second dialog box works normally because it's only one line (i.e. without '& return')


property addHTTP : true -- adds http:// if :// is not found in the link
property replaceSpaces : true -- replace spaces with %20 in links?

property defaultLinkAnswer : ""
property defaultTextAnswer : "click here"

property linkDialogText : "This will create an rtf link on the clipboard. You can paste it anywhere that accepts rtf text." & return & return & "Enter the hypertext link:"
property wordDialogText : "Enter the words that will represent the link:"
property btn1 : "Clipboard Contents"
property btn2 : "Cancel"
property btn3 : "OK"

-- get the link
repeat
	tell me to activate
	tell me to display dialog linkDialogText default answer defaultLinkAnswer & return buttons {btn1, btn2, btn3} default button 3 with icon note
	set {theLink, theButton} to {text returned, button returned} of result
	if theButton is btn1 then
		set defaultLinkAnswer to the clipboard
	else
		set defaultLinkAnswer to theLink
		exit repeat
	end if
end repeat
set theLink to removeTrailingReturns(theLink)
if theLink is "" then return -- we don't continue if there is no link to use

-- add http:// if needed
if addHTTP is true then
	if theLink does not contain "://" then set theLink to "http://" & theLink
end if

-- replace spaces with %20
if replaceSpaces is true then set theLink to findReplace(theLink, space, "%20")

-- get the words for the link
repeat
	tell me to activate
	tell me to display dialog wordDialogText default answer defaultTextAnswer buttons {btn1, btn2, btn3} default button 3 with icon note
	set {linkText, theButton} to {text returned, button returned} of result
	if theButton is btn1 then
		set defaultTextAnswer to the clipboard
	else if linkText is not "" then
		set defaultTextAnswer to linkText
		exit repeat
	end if
end repeat

-- create the link on the clipboard
set cmd to "\"{\\rtf1\\ansi\\ansicpg1252\\cocoartf949\\cocoasubrtf270{\\fonttbl\\f0\\fswiss\\fcharset0 Helvetica;}{\\colortbl;\\red255\\green255\\blue255;}\\margl1440\\margr1440\\vieww9000\\viewh8400\\viewkind0\\pard\\tx720\\tx1440\\tx2160\\tx2880\\tx3600\\tx4320\\tx5040\\tx5760\\tx6480\\tx7200\\tx7920\\tx8640\\ql\\qnatural\\pardirnatural{\\field{\\*\\fldinst{HYPERLINK \"" & theLink & "\"}}{\\fldrslt\\f0\\fs24 \\cf0 \"" & linkText & "\"}}}\""
do shell script "/bin/bash -c 'echo " & cmd & " | pbcopy -Prefer rtf'"



(*============ SUBROUTINES ==============*)
on findReplace(theString, search_string, replacement_string)
	if theString contains search_string then
		set AppleScript's text item delimiters to search_string
		set text_item_list to text items of theString
		set AppleScript's text item delimiters to (replacement_string as Unicode text)
		set theString to every item in text_item_list as Unicode text
		set AppleScript's text item delimiters to ""
	end if
	return theString
end findReplace

on removeTrailingReturns(someText)
	repeat while someText ends with return
		set someText to text 1 thru -2 of someText -- lop off last character.
	end repeat
	return someText
end removeTrailingReturns

Now that’s a work of art. Works great and the best part is it’s something that I can utilize.
Chuck

Thanks. We made a nice team! This is something I’ll use too! :wink:

I can’t express to you how handy that script (with some mods, of course) is going to be to me. Thank you both – fantastic.:slight_smile:

You know what I’m thinking Adam …and the database grows! :wink:

Using textutil can simplify this a bit:

property pTitle : "Make RTF link in clipboard"

set strURL to text returned of (display dialog "Enter a URL:" default answer "http://" with title pTitle)
set strTitle to text returned of (display dialog "Enter a title for the link:" default answer strURL with title pTitle)

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