Monday, December 18, 2017

#1 2017-08-06 03:46:36 am

ppayne
Member
From:: Isesaki, Japan
Registered: 2007-10-31
Posts: 58
Website

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!


Filed under: parsing, Strings

Offline

 

#2 2017-08-06 04:35:59 am

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 3203

Re: A way of truncating a large text string efficiently

If I understand well what you want this may help :

Applescript:

(*
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

Offline

 

#3 2017-08-06 05:15:27 am

Nigel Garvey
Moderator
From:: Warwickshire, England
Registered: 2002-11-20
Posts: 4458

Re: A way of truncating a large text string efficiently

ppayne wrote:

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.


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:

Applescript:

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?


NG

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)