Create URL with label via script

Is there a way to take a text label and an URL and use Applescript add them to the Mac clipboard such that rich-text apps see the data as a URL and make linked anchor text when pasting in the data.

I have untitled reference URLs (generated via code) that need to have a ‘screen’ title (anchor text). I can create the URL and anchor text. But, how do I combine them so that the Mac clipboard treats the data as a URL? I tried:

set the clipboard to "<a href=\"" & theURI & "\" target=\"_blank\">" & theAnchor & "</a>"

…but when using this data from other apps I get the HTML string and not a link with the anchor text as the visible screen text.

StandardAdditions has ‘URL’ and ‘web page’ classes but I can’t see how to apply them. This compiles but fails when run:

set myURL to theURI as URL
set myLinkAnchor to theAnchor as text
set linkURL to {URL: myURL, name: myLinkAnchor} as web page

Background: Barebones’ Yojimbo allows local links direct to item in the users Yojimbo library, e.g.
x-yojimbo-item://33B1CDF7-56F5-47FE-A1A3-1ED72953B4FC, but such URIs are only available via a menu item and also lack any indications to the target items’s name/label. So, I can use AppleScript to get and Items ID and turn that into the URL and separately get the target item’s name but I can seemingly make whatever record format is needed to make the date, when posted to the clipboard, get used as a link with anchor text when pasting to other apps.

Am I overlooking something obvious or is this just not possible? Ideally, if I can make the clipboard data correctly, i’ll add an Automator service to Yojimbo can paste a link into another app (in my case, Tinderbox).

To my surprise, this actually works ” when the result’s pasted into TextEdit, anyway! :slight_smile:

set theURL to "http://macscripter.net/profile.php?id=6548"
set anchorText to "mwra"

set fRef to (open for access file ((path to temporary items as text) & "urlscratch.tmp") with write permission)
try
	-- Convert the URL text to URL data by writing it to file as string and reading it back as URL.
	set eof fRef to 0
	write theURL as string to fRef
	set theURL to (read fRef from 1 as URL)
	-- Convert the anchor text to «class urln» similarly.
	set eof fRef to 0
	write anchorText as string to fRef
	set theAnchor to (read fRef from 1 as «class urln»)
on error msg
	display dialog msg buttons {"OK"} default button 1
end try
close access fRef

-- Put a record containing the two results onto the clipboard.
set the clipboard to {URL:theURL, «class urln»:theAnchor}

Thanks. You’re right about it varying by app. The output works in TexTEdit, but not the app I have in mind. My error in assumption.

I guess what I really need to simulate is the data in a a URL dragged from Safari but I’ve no idea how to test what that is as I can’t interrogate drag-data in AppleScript - or can I. Could an ‘on drop’ event report what it gets (I’m expecting a URL and a title, possibly as a record).