Looking at the code I see some improvements that can be made.
Offset uses an external AppleEvents which is relatively time consuming so an custom handler using text item delimiters is actually faster.
on customOffset(str, subStr)
if subStr is not in str then return 0
tell AppleScript
set oldTIDs to text item delimiters
set text item delimiters to subStr
set distance to count text item 1 of str
set text item delimiters to oldTIDs
end tell
return distance + 1
end customOffset
But I also noticed that your doing the same loop for different tasks like counting, matching and cleaning empty lines. Can’t this be done in one single go to make things more efficient and eventually faster?
But if you prefer to make it work this way you can use AppleScript Toolbox (AST) to manage lists easier. It has the AST copy list command where you can instruct which items you want to keep or omit by index. So you keep track of all the items that needs to be removed and remove the items in a single command:
set theList to {"1", "2", "3", "4", "5", "6", "7", "8", "9"}
AST copy list theList omitting item {6, 3, 5}
-- Result: {"1", "2", "4", "7", "8", "9"}
The same is for removing empty lines which can be done in a single command using AST:
set theLines to {"John: 1", "Bill: 2", "", "Mark: 4", "David: 5", "", "Peter: 7", "Kyle: 8", "George: 9"}
AST copy list theLines matching regex ".+"
-- Result: {"John: 1", "Bill: 2", "Mark: 4", "David: 5", "Peter: 7", "Kyle: 8", "George: 9"}
Summarizing all values can be done on file contents instead of line by line using AST as well:
set theText to "John: 1
Bill: 2
Mark: 4
David: 5
Peter: 7
Kyle: 8
George: 9"
set theValues to AST find regex ": ([0-9]+)$" in string theText regex group 2 with newline sensitivity
set theTotal to run script join(theValues, "+")
-- Result: 36
on join(lst, del)
tell AppleScript
set oldTIDs to text item delimiters
set text item delimiters to del
set str to lst as string
set text item delimiters to oldTIDs
end tell
return str
end join
I’ll hope this gets you started