Thursday, September 20, 2018

#1 2012-07-02 10:38:16 am

Registered: 2005-04-07
Posts: 28

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

Filed under: URL clipboard



#2 2012-07-02 03:29:23 pm

Nigel Garvey
From:: Warwickshire, England
Registered: 2002-11-20
Posts: 4653

Re: Create URL with label via script

To my surprise, this actually works — when the result's pasted into TextEdit, anyway!  smile


set theURL to "[url][/url]"
set anchorText to "mwra"

set fRef to (open for access file ((path to temporary items as text) & "urlscratch.tmp") with write permission)
   -- 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}




#3 2012-07-02 04:23:10 pm

Registered: 2005-04-07
Posts: 28

Re: Create URL with label via script

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



Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)