Tuesday, September 23, 2014

#1 2008-07-24 03:46:37 am

Mikebor
Member
Registered: 2008-07-24
Posts: 53

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

Applescript:

set myURLAsSimpleText to the clipboard

Applescript:

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

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


Filed under: clipboard, RTF, TXT

Offline

 

#2 2008-07-24 07:41:02 am

regulus6633
Member
From: Taulov, Denmark
Registered: 2006-11-01
Posts: 1695
Website

Re: convert link in HTML to link in RTF

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.

Offline

 

#3 2008-07-24 07:43:10 am

cwtnospam
Member
Registered: 2008-06-08
Posts: 274

Re: convert link in HTML to link in RTF

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. sad

Offline

 

#4 2008-07-24 08:08:33 am

Mikebor
Member
Registered: 2008-07-24
Posts: 53

Re: convert link in HTML to link in RTF

Well, I choose emulating keystrokes as workaround.

Applescript:

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.


Filed under: mail, System

Offline

 

#5 2008-07-24 12:09:11 pm

soboks
Member
From: New Hampshire
Registered: 2006-09-09
Posts: 9

Re: convert link in HTML to link in RTF

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- … -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

#!/bin/bash
# Places a rich text link on the clipboard
# usage: rtflink.sh "Title of link" "URL to link to"
#
# This will paste *nothing* into applications that don't recognize rich text

echo "{\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 \"$2\"}}{\fldrslt
\f0\fs24 \cf0 $1}}}" | pbcopy -Prefer rtf

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:

Applescript:

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.

<edit>
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.

javascript:location.href='mailto:youraddress@domain.com?SUBJECT='+document.title+%20'&BODY='+escape(location.href)

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...
<end edit>

Chuck

Last edited by soboks (2008-07-24 10:09:14 pm)


deliver deletter desooner debetter

Filed under: safari

Offline

 

#6 2008-07-24 02:35:20 pm

regulus6633
Member
From: Taulov, Denmark
Registered: 2006-11-01
Posts: 1695
Website

Re: convert link in HTML to link in RTF

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???

Applescript:

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

Applescript:

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"


Filed under: terminal, System

Offline

 

#7 2008-07-24 04:03:10 pm

soboks
Member
From: New Hampshire
Registered: 2006-09-09
Posts: 9

Re: convert link in HTML to link in RTF

regulus6633, try this one:

Applescript:

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

Last edited by soboks (2008-07-24 09:54:21 pm)


deliver deletter desooner debetter

Offline

 

#8 2008-07-24 04:38:53 pm

regulus6633
Member
From: Taulov, Denmark
Registered: 2006-11-01
Posts: 1695
Website

Re: convert link in HTML to link in RTF

soboks wrote:

regulus6633, try this one:

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

Here's a script I wrote to use this:

Applescript:

-- 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

Last edited by regulus6633 (2008-07-24 06:02:42 pm)

Offline

 

#9 2008-07-24 04:49:46 pm

soboks
Member
From: New Hampshire
Registered: 2006-09-09
Posts: 9

Re: convert link in HTML to link in RTF

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


deliver deletter desooner debetter

Offline

 

#10 2008-07-24 04:52:09 pm

regulus6633
Member
From: Taulov, Denmark
Registered: 2006-11-01
Posts: 1695
Website

Re: convert link in HTML to link in RTF

soboks wrote:

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

Offline

 

#11 2008-07-24 05:30:58 pm

Adam Bell
Administrator
From: Nova Scotia, Canada
Registered: 2005-10-04
Posts: 4632

Re: convert link in HTML to link in RTF

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.smile


iMac & MBP both running OS X 10.9.1

Offline

 

#12 2008-07-24 06:09:36 pm

regulus6633
Member
From: Taulov, Denmark
Registered: 2006-11-01
Posts: 1695
Website

Re: convert link in HTML to link in RTF

Adam Bell wrote:

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.smile

You know what I'm thinking Adam ...and the database grows! wink

Offline

 

#13 2011-01-27 04:39:45 am

yiam-jin-qui
Member
Registered: 2010-10-10
Posts: 34

Re: convert link in HTML to link in RTF

Using textutil can simplify this a bit:

Applescript:

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


天地不仁, 仁者人也

Houthakker / RobTrew

Offline

 

Board footer

Powered by FluxBB

[ Generated in 0.036 seconds, 8 queries executed ]

RSS (new topics) RSS (active topics)