iMessage - how to "add link"?

Here’s the AppleScript I have so far:

property buddyList : {"0709123456", ""}

repeat with aBuddy in buddyList
	tell application "Messages"
		send "This is the text" to buddy aBuddy of service ""
	end tell
end repeat

Now I want to have links in the text I’m sending, just like one can make through the “Add link” (Cmd-K) in Messages - how can I do this with AppleScript?


actually at least text starting with http://. is treated automatically as link

even www. or just makes a link
but that’s not what I’m looking for obviously
i need to be able to make links out of any text


Please try to describe more narrower/detailed what you are looking for. :slight_smile:

OK, I’ll try again.
In Messages you can write some text, let’s say just for example “this is a link”.
Then you can make this text a link by choosing “Add link” from the Edit Menu, let’s say for example “”.
So, the text “this is a link” is made a link which takes you to
This I want to with AppleScript!


What happens if you past in this?

doesn’t work unfortunately…


I don’t have all the day for this, but hopefully the problem was that you are having rtf text, the code below isn’t much clean, but if the messages is in rtf-text, then I have high hopes that it works. And you will probably, and should, both clean up the code, and modify it to your taste. :slight_smile:

-- totally stolen from
-- I have a shell file - rather compact, but I can't figure out where that leading zero comes from.
-- I'll come back and revise this one as well.
local _msglnk
set _linkText to text returned of (display dialog "Enter link title" default answer "")

set _msglnk to text returned of (display dialog "Enter the url" default answer "")

set startEcho to "echo "
set echoDelimiter to "'"

set html_1 to "<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01//EN\" \"\">
<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">
<meta http-equiv=\"Content-Style-Type\" content=\"text/css\">
set html_2 to "</title>
<meta name=\"Generator\" content=\"Cocoa HTML Writer\">
<meta name=\"CocoaVersion\" content=\"1038.11\">
<style type=\"text/css\">
p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica}
<p class=\"p1\"><a href=\""
set html_3 to "\">"
set html_4 to "</a></p>
set echoCommand to startEcho & echoDelimiter & html_1 & _linkText & html_2 & _msglnk & html_3 & _linkText & html_4 & echoDelimiter
set textutilCommand to " | textutil -convert rtf -inputencoding UTF-8 -format html -stdin -stdout"
set pbcopyCommand to " | pbcopy -Prefer rtf"

set entireCommand to echoCommand & textutilCommand & pbcopyCommand

do shell script entireCommand
display dialog "Try paste it in"


You can’t have any quotes (single or double ticks in the link titles, or it will break, you should also avoid æøå and such, since those should really be encoded. Well. you’ll find those missing parts around here, should you wish to enforce it to this standard.

(I just realized, how to create a table in an rtf document the easy way. ):slight_smile:

I don’t get it, I’m sorry.

I didn’t have any rtf text - i guess you can’t have inside an AppleScript?

Even though iMessages are in rtf, how are you going to get it in an AppleScript?


First of all the script accepts a text for a title, then the url. then we create an html document on the fly, containing the text making it into an title as before. But now, we continue, and generate an rtf document out of that, which we put onto the clipboard. (This works for me, when I then paste the contents into an rtf-document in TextEdit. So I hope it works for you too.

OK, your script works. I paste the result in Messages and it works.

So you’re suggesting to modify my original script (above) to include pasting the iMessage text in Messages?

I try to stay away from UI scripting but it seems I have to use it here…


The answer to that, is that the process must be more elaborate, as you’ve alread pointed out, AppleScript doesn’t handle rtf very well. but you could try to send the clipboard as record.

Something like below. (Maybe you are in luck.)

set aLink to the clipboard as record
repeat with aBuddy in buddyList
   tell application "Messages"
       send aLink to buddy aBuddy of service ""
   end tell
end repeat

But I guess you’ll end up with scripting something including creating a new message, and then pasting the contents of the clipboard into that message. Then you will of course have to look around for similiar scripts, and study the dictionary of Messages in AppleScript editor as well.

OK fine - and HEY thanks a lot! and many räksmörgåsar to you again :wink:


I am glad it worked for you! :slight_smile: