A way of truncating a large text string efficiently

Hello, longtime Macscripter friends. I have a scripting question, and I would like some help if anyone can suggest something. Basically, I do a lot of scripting by grabbing the source code of a large website, then mucking around inside the source code to find the URLs, etc. that I need. Currently I have a problem: truncating text using only AppleScript seems to go ridiculously slow. The scripts step in question is basically this:

set myText to (characters offset1 thru thelength of thesourcecode) as string

The problem is, the code I’m parsing is large enough that this step takes a whopping 2-4 minutes to complete. The website source code I’m working through is only 500 kb, not that huge, but the step takes a super long time for some reason.

So what I’m hoping is, someone can tell me a super-efficient way of truncating a text variable. This can be done either using AppleScript, or some other method such as pearl, though I have no experience in how to do this, and would like help. I want to call it all from one AppleScript, or possibly from a keyboard Maestro action before the main Applescript.

Thanks in advance for any help you can offer!

If I understand well what you want this may help :

(*
Handler borrowed from Shane STANLEY's Everyday AppleScriptObjC
*)

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

on findURLsIn:theString
	-- make a data detector
	set theNSDataDetector to current application's NSDataDetector's dataDetectorWithTypes:(current application's NSTextCheckingTypeLink) |error|:(missing value)
	-- find the matches
	set theURLsNSArray to theNSDataDetector's matchesInString:theString options:0 range:{location:0, |length|:length of theString}
	-- extract the strings
	return ((theURLsNSArray's valueForKeyPath:"URL.absoluteString")'s componentsJoinedByString:return) as text
end findURLsIn:

tell application "Mail"
	set theMessage to item 1 of (get the selection)
	set theDatas to source of theMessage
end tell

set theURLs to my findURLsIn:theDatas

Yvan KOENIG running Sierra 10.12.6 in French (VALLAURIS, France) dimanche 6 aout 2017 11:35:50

Don’t use that code. It creates a list of all the individual characters to be used in the substring and then coerces the list to text, which is inefficient and the result depends on the current value of AppleScript’s text item delimiters. This extracts the substring directly:

set myText to text offset1 thru thelength of thesourcecode

But even so, your code shouldn’t take two to four minutes! Is it being repeated many thousands of times? Is thesourcecode AppleScript text or text in an application?