Examples of a use of offset in AppleScript

I’m sure I have seen Nigel doing everything with offset in AppleScript. :slight_smile:
Myself I have not use it so much but here is my approach.

The last example I need to use ‘ve’ instead of ‘v’ to get ‘very difficult problem’

I guess if we do a test to check if theBegin and theEnd is count of 1 but we could also
use tags of begin and end of every paragraph. And let AppleScript to extract the characters in between.

set theString to "123,456 789"
log (my charactersFromBegin:4 toEnd:6 inString:theString)

set theString to "abc, some words"
log (my charactersFromBegin:"s" toEnd:"e" inString:theString)

set theString to "This is a victory of a very difficult problem"
log (my charactersFromBegin:"very" toEnd:"m" inString:theString)

on charactersFromBegin:theBegin toEnd:theEnd inString:theString
	set {theBegin, theEnd} to {offset of theBegin in theString, offset of theEnd in theString}
	return (characters theBegin thru theEnd of theString) as text
end charactersFromBegin:toEnd:inString:

Hi Fredrik.

Yes. You have to know in advance that the second search term doesn’t also occur before the first in order to use ‘offset’ to cut text in two places like that. The only way to be sure using ‘offset’ is to get the text from the first term to the end of the text and then use ‘offset’ again on that to find the second term.

By the way, don’t forget that ‘(characters theBegin thru theEnd of theString) as text’ creates a list of characters which is then coerced to text using the current setting of AppleScript’s text item delimiters. ‘text theBegin thru theEnd of theString’, on the other hand, will extract the substring directly without involving the TIDs:

on charactersFromBegin:theBegin toEnd:theEnd inString:theString
	set {theBegin, theEnd} to {offset of theBegin in theString, offset of theEnd in theString}
	return text theBegin thru theEnd of theString
end charactersFromBegin:toEnd:inString:

Or implementing what I described in my first paragraph:

on charactersFromBegin:theBegin toEnd:theEnd inString:theString
	set theString to text (offset of theBegin in theString) thru -1 of theString
	return text 1 thru (offset of theEnd in theString) of theString
end charactersFromBegin:toEnd:inString:

@Nigel, thanks…
The first search term could include words or more and 1 character. But its doesn’t work for the end search term. The first character of the end search term become the offset. I thought maybe its possible to get the offset of the last character of the end term.

That way I believe its possible to include more characters for the end term to make sure the count
of the input is always 1 in a big text.

Have you ever done something like that instead of using a regular expression approach ?? :slight_smile:

Hi Fredrik.

You mean include the whole of the end term in the returned text?

on charactersFromBegin:theBegin toEnd:theEnd inString:theString
	set theString to text (offset of theBegin in theString) thru -1 of theString
	return (text 1 thru ((offset of theEnd in theString) - 1) of theString) & theEnd
end charactersFromBegin:toEnd:inString:

Or perhaps more efficiently:

on charactersFromBegin:theBegin toEnd:theEnd inString:theString
	set theString to text (offset of theBegin in theString) thru -1 of theString
	return text 1 thru ((offset of theEnd in theString) - 1 + (count theEnd)) of theString
end charactersFromBegin:toEnd:inString:

Oooo. A long time ago. :wink:

@Nigel, Exatly…

Here was mine before I check yours… and they are the same

set theString to "\nLorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla pretium purus ut enim venenatis sodales. Quisque facilisis tortor eu gravida tristique. Nulla tincidunt eros et nisl auctor, non vestibulum nisi vehicula. Nulla a porttitor lacus, ut luctus sem. Nullam dolor purus, efficitur at malesuada eu, viverra et leo. Nam et posuere dui. Cras lacinia ac arcu et molestie. Integer tincidunt, purus id aliquam iaculis, orci magna rhoncus turpis, nec luctus neque augue vitae sem. Quisque tempor mauris vel tortor laoreet, nec commodo lorem iaculis. Aenean ut ligula ut enim volutpat venenatis. Nulla facilisi. Curabitur imperdiet nunc id nibh maximus, ut auctor turpis fermentum.\n\n\n"

my charactersFromBegin:"consectetur" toEnd:"dui" inString:theString
on charactersFromBegin:theBegin toEnd:theEnd inString:theString
	set theEndsCount to count character of theEnd
	set theString to text (offset of theBegin in theString) thru -1 of theString
	return text 1 thru ((offset of theEnd in theString) + theEndsCount - 1) of theString
end charactersFromBegin:toEnd:inString: