Getting selected text in Safari

I did some research how to get selected text in Safari, but that’s long time ago.
As far as I know similar scripts didn’t ever work, because Safari has poor scriptability and do Javascript commands aren’t my thing.

Maybe somebody knows a better solution than mine.The script below should work, but it doesn’t. :rolleyes:

set javaSel to "getSelectionHTML = function () {\n      var userSelection;\n      if (window.getSelection) {\n        // W3C Ranges\n        userSelection = window.getSelection ();\n        // Get the range:\n        if (userSelection.getRangeAt)\n          var range = userSelection.getRangeAt (0);\n        else {\n          var range = document.createRange ();\n          range.setStart (userSelection.anchorNode, userSelection.anchorOffset);\n          range.setEnd (userSelection.focusNode, userSelection.focusOffset);\n        }\n        // And the HTML:\n        var clonedSelection = range.cloneContents ();\n        var div = document.createElement ('div');\n        div.appendChild (clonedSelection);\n        return div.innerHTML;\n      } else if (document.selection) {\n        // Explorer selection, return the HTML\n        userSelection = document.selection.createRange ();\n        return userSelection.htmlText;\n      } else {\n        return '';\n      }\n    };"

tell window 1 of application "Safari"
	tell current tab
 		set res to do JavaScript javaSel
	end tell
 end tell

return res

Very nice, but I guess this command doesn’t include links, or am I wrong? :wink:

To get URLs too, your script should copy the selection to the clipboard. Then you can grab URLs from the clipboard using regex expression.

So, select in the Safari some text, including links too. Run this:

use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use scripting additions

tell application "Safari"
	(do JavaScript "document.execCommand('copy')" in document 1) -- copy selection
	set theText to (do JavaScript "getSelection().toString()" in document 1)
end tell

# Get a reference to the Clipboard object.
set pb to current application's NSPasteboard's generalPasteboard()

# Read HTML from the Clipboard object.
set theHtmlText to (pb's stringForType:"public.html") as text

# Find HREF URLs.
set foundUrlList to its regexFindWithCapture:"(?i)(?:href\\h*=\\h*[\"'])(http[^\"']+)" fromString:theHtmlText resultTemplate:"$1"

return {|URLs|:foundUrlList, |Text|:theText}

on regexFindWithCapture:thePattern fromString:theString resultTemplate:templateStr
	set theString to current application's NSString's stringWithString:theString
	set theRegEx to current application's NSRegularExpression's regularExpressionWithPattern:thePattern options:0 |error|:(missing value)
	set theFinds to theRegEx's matchesInString:theString options:0 range:{0, theString's |length|()}
	set theResult to current application's NSMutableArray's array()
	repeat with aFind in theFinds
		set foundString to (theRegEx's replacementStringForResult:aFind inString:theString |offset|:0 template:templateStr)
		(theResult's addObject:foundString)
	end repeat
	return theResult as list
end regexFindWithCapture:fromString:resultTemplate: