2

In AppleScript, I can cast some variables (e.g., the clipboard), to the built-in URL type:

set theURL to the clipboard as URL

If my clipboard contains an RTF link to https://temochka.com/, theUrl will be assigned a raw value like this (which can be seen in the Script Editor replies window):

«data url 68747470733A2F2F74656D6F63686B612E636F6D»

This last part (68747470733A2F2F74656D6F63686B612E636F6D) is a hex-encoded string that can be decoded as "https://temochka.com". Is there a way to extract just this value? Or maybe get the full URL value out?

4
  • Normally the clipboard contains several types (see clipboard info), so you should be able to just coerce to text.
    – red_menace
    Mar 31, 2021 at 2:15
  • Testing on my system where set theURL to the clipboard as URL returns «data url 68747470733A2F2F74656D6F63686B612E636F6D» it also returns https://temochka.com when using set theURL to the clipboard as text. So is there some reason you can not just use set theURL to the clipboard as text? Mar 31, 2021 at 2:22
  • @user3439894 try with a rich link that doesn’t match the URL. E.g., open Notes.app and create a link "website" that points to Google. the clipboard as text is "website" and the clipboard as URL is the URL.
    – temochka
    Mar 31, 2021 at 2:50
  • temochka, Whether I copy the hyperlink from a RTF document in TextEdit or from Notes, it does as I've stated in my first comment to the OP. Mar 31, 2021 at 2:58

2 Answers 2

1

If set theURL to the clipboard as URL returns:

«data url 68747470733A2F2F74656D6F63686B612E636F6D»

Then if you are set on working with that class, you need to first write it to a temporary file and then read it back, as in the following example AppleScript code:

set tmpFilename to "/tmp/theURL.txt"

set theURL to the clipboard as URL

writeToFile(theURL, tmpFilename, true)

set theURL to read tmpFilename


on writeToFile(theData, theFile, overwriteExistingContent)
    try
        set theFile to theFile as string
        if theFile contains "/" then
            set theOpenedFile to open for access theFile with write permission
        else
            set theOpenedFile to open for access file theFile with write permission
        end if
        if overwriteExistingContent is true then set eof of theOpenedFile to 0
        write theData to theOpenedFile starting at eof
        close access theOpenedFile
        return true
    on error
        try
            close access file theFile
        end try
        return false
    end try
end writeToFile 

Notes:

The writeToFile(theText, theFile, overwriteExistingContent) handler is a slightly modified handler from Reading and Writing Files

The handler from the linked Apple support document was modified to handle both POSIX and HFS+ file paths.

When the clipboard returns what's show herein, it also can return https://temochka.com directly by using, e.g.:

set theURL to the clipboard as text

enter image description here

2
  • You’re a magician! I adjusted this a bit for my use case but writing to a temporary file and then reading it back actually does the trick. Thank you!
    – temochka
    Mar 31, 2021 at 4:03
  • For posterity, this is the code that I ended up writing: github.com/temochka/macos-automation/commit/…
    – temochka
    Mar 31, 2021 at 4:26
-1

That's not what I get at all:

{class:URL, scheme:secure http URL, path:"https://temochka.com/", host:{class:Internet address, DNS form:"temochka.com", port:443, dotted decimal form:"159.203.68.102"}}

3
  • I’ve seen this behavior as well, but it only happens when the link text is itself a URL. If the link text is, say, "my homepage", then the clipboard as URL returns the described data object, not the URL class.
    – temochka
    Mar 31, 2021 at 2:45
  • RE: "That's not what I get at all:" -- That's because you did not copy the link from a RTF document where it is a hyperlink. Mar 31, 2021 at 2:46
  • I just tried that, and I still got: {class:URL, scheme:secure http URL, path:"temochka.com", host:{class:Internet address, DNS form:"temochka.com", port:443, dotted decimal form:"159.203.68.102"}}
    – Alan
    Apr 3, 2021 at 2:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .