use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
use framework "AppKit" -- for NSPasteboard
tell application "Safari"
if not (exists document 1) then error "No document is open."
set theURL to URL of document 1
try
get theURL
on error
error "The front document has no URL."
end try
set thisTitle to name of document 1
end tell
-- make link NSURL
set linkURL to current application's NSURL's URLWithString:theURL
-- make dictionary of text attributes: the NSURL, blue color, single underline
set theNSDictionary to current application's NSDictionary's dictionaryWithObjects:{linkURL, current application's NSNumber's numberWithUnsignedInteger:(current application's NSSingleUnderlineStyle), (current application's NSColor's blueColor())} forKeys:{current application's NSLinkAttributeName, current application's NSUnderlineStyleAttributeName, current application's NSForegroundColorAttributeName}
-- make attributed string containing the link
set theNSAttributedString to current application's NSAttributedString's alloc()'s initWithString:thisTitle attributes:theNSDictionary
-- get the clipboard
set theNSPasteboard to current application's NSPasteboard's generalPasteboard()
-- clear it
theNSPasteboard's clearContents()
-- put the link on it
theNSPasteboard's writeObjects:{theNSAttributedString}