Extracting Labels and respective links from Mail message

I have tried to get labels and their links from mail messages,
the problem is that most mail message source body differs.
So my question is now, whether this undertaking is useful or not.

launch application "Mail"
#delay 3
tell application "Mail"
	#activate
	set sel to selection
	if sel is {} then
		set {description_txt, title_txt} to {"please select a mail message first", "ERROR!"}
		display notification description_txt with title title_txt
		return
	end if
	set selMail to item 1 of sel
	set {getSource, getSubj} to {source, subject} of selMail
end tell
set ReviewQ to my findRep(getSource, "'", "'\\''")
set getLinks to paragraphs of (do shell script "echo '" & ReviewQ & "' | grep 'http' ")
set catchLinks to {}
repeat with a in getLinks
	set a to a as text
	#if word 3 of a is "href" then
	set myLink to text (offset of "http" in a) thru -1 of a
	if myLink is not in catchLinks then copy myLink to end of catchLinks
	#end if
end repeat

on findRep(T, fin, repl)
	set tid to text item delimiters
	set text item delimiters to fin
	set T to text items of T
	set text item delimiters to repl
	set T to T as string
	set text item delimiters to tid
	return T
end findRep

I like use the Shane Stanley’s ASObjC solution. The script adapted for my tasks is here:


use AppleScript version "2.4"
use framework "Foundation"
use scripting additions

set aBody to ""
tell application "Mail"
	repeat with aMessage in (get selection)
		set aBody to aBody & linefeed & (get content of aMessage)
	end repeat
end tell
set emailAddresses to paragraphs of (its findLinksIn:{aBody, "'mailto'"})
set httpsAddresses to paragraphs of (its findLinksIn:{aBody, "'https'"})
set httpAddresses to paragraphs of (its findLinksIn:{aBody, "'http'"})

on findLinksIn:{theString, uRLscheme}
	set theString to current application's NSString's stringWithString:theString
	-- Locate all the "links" in the text.
	set theNSDataDetector to current application's NSDataDetector's dataDetectorWithTypes:(current application's NSTextCheckingTypeLink) |error|:(missing value)
	set theURLsNSArray to theNSDataDetector's matchesInString:theString options:0 range:{location:0, |length|:theString's |length|()}
	-- Extract email links
	set emailPredicate to current application's NSPredicate's predicateWithFormat:("self.URL.scheme == " & uRLscheme)
	set emailURLs to theURLsNSArray's filteredArrayUsingPredicate:emailPredicate
	-- Get just the addresses
	set emailsArray to emailURLs's valueForKeyPath:"URL.resourceSpecifier"
	--eliminate duplicates
	set emailsArray to (current application's NSSet's setWithArray:emailsArray)'s allObjects()
	-- Or: set emailsArray to (current application's NSOrderedSet's orderedSetWithArray:emailsArray)'s array()
	-- Join the remainder as a single, return-delimited text
	set emailAddresses to emailsArray's componentsJoinedByString:(return)
	-- Return as AppleScript text
	return emailAddresses as text
end findLinksIn: