replace each email and website url in a text (variable)

Hello, I am looking for a way to change all the email addresses and all the website urls in a text to the same email address and website url preceding the text ‘<!s>’. Possibly using a grep command or anything. Is there anyone here who can help me? Would be great, I’ve been trying myself for a few days, but cannot get to any result.

Hi, maarten999.

Exists many URL schemes to apply. I will use only 3 of URL schemes. (click to see all).
To edit email addresses, http URLs and https URLs, contained by text:


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

set aText to "Hello, guys.
My email address is \"kniazidis.rompert@gmail.com.\"
I am looking for a way to change all the email addresses
 and all the website urls in a text to the same email address
 and website url preceding the text <\\!s>.
Current webpage's URL address is \"https://macscripter.net/viewtopic.php?pid=199515#p199515\".
 Possibly using a grep command or anything. Is there anyone here who can help me?
 Would be great, I've been trying myself for a few days, but cannot get to any result."

-- Locate all the "links" in the text.
set theNSDataDetector to current application's NSDataDetector's dataDetectorWithTypes:(current application's NSTextCheckingTypeLink) |error|:(missing value)
set theString to current application's NSString's stringWithString:aText
set theURLsNSArray to theNSDataDetector's matchesInString:theString options:0 range:{location:0, |length|:theString's |length|()}

set emailAddresses to {its findAddressesIn:{aText, theURLsNSArray, "'mailto'"}}
set httpsAddresses to {its findAddressesIn:{aText, theURLsNSArray, "'https:'"}}
set httpAddresses to {its findAddressesIn:{aText, theURLsNSArray, "'http:'"}}

repeat with emailAddress in emailAddresses
	set newEmailAddress to "<\\!s>" & emailAddress
	set aText to my replaceText(emailAddress, newEmailAddress, aText)
end repeat

repeat with i from 1 to count httpsAddresses
	set httpsAddress to "https:" & (item 1 of httpsAddresses)
	set newhttpsAddress to "<\\!s>" & httpsAddress
	set aText to my replaceText(httpsAddress, newhttpsAddress, aText)
end repeat

repeat with i from 1 to count httpAddresses
	set httpAddress to "http:" & (item 1 of httpAddresses)
	set newhttpAddress to "<\\!s>" & httpAddress
	set aText to my replaceText(httpAddress, newhttpAddress, aText)
end repeat

return aText

on findAddressesIn:{theString, theURLsNSArray, urlScheme}
	-- Filter the links
	set aPredicate to current application's NSPredicate's predicateWithFormat:("self.URL.scheme == " & urlScheme)
	set theURLs to theURLsNSArray's filteredArrayUsingPredicate:aPredicate
	-- Get just the addresses
	set anArray to theURLs's valueForKeyPath:"URL.resourceSpecifier"
	--eliminate duplicates
	set anArray to (current application's NSSet's setWithArray:anArray)'s allObjects()
	-- Join the remainder as a single, return-delimited text
	set theAddresses to anArray's componentsJoinedByString:(return)
	-- Return as AppleScript text
	return theAddresses as text
end findAddressesIn:

on replaceText(find, replace, subject)
	set prevTIDs to text item delimiters of AppleScript
	set text item delimiters of AppleScript to find
	set subject to text items of subject
	set text item delimiters of AppleScript to replace
	set subject to "" & subject
	set text item delimiters of AppleScript to prevTIDs
	return subject
end replaceText

NOTE. To extract and filter the URLs, I use handler, written by Shane Stanley. Thank you for sharing the experience.

Hi KniazidisR, thank you very much for your help. Really helped out a lot. Thanks.