What is the best and most efficient way to do this in AppleScript?
8,644 19 19 gold badges 86 86 silver badges 152 152 bronze badges asked Jul 15, 2009 at 7:27 Sven Fechner Sven FechnerI'd also use offsets. text 1 thru 2 of "xyz" is equivalent to items 1 thru 2 of "xyz" as string .
set x to "First Last ([email protected])" set pos to offset of " (" in x
set x to "First Last ([email protected])" set text item delimiters to set to text items 1 thru 2 of x
If others were searching about string manipulation in general, here are methods for replacing and splitting text and joining lists:
on replace(input, x, y) set text item delimiters to x set ti to text items of input set text item delimiters to y ti as text end replace on split(input, x) if input does not contain x then return set text item delimiters to x text items of input end split on join(input, x) set text item delimiters to x input as text end join
String comparisons ignore case by default:
"A" is "a" -- true "ab" starts with "A" -- true considering case "A" is "a" -- false "ab" starts with "A" -- false end considering
reverse of items of "esrever" as text
You can use do shell script to change the case of text:
do shell script "printf %s " & quoted form of "aä" & " | LC_CTYPE=UTF-8 tr [:lower:] [:upper:]" without altering line endings
echo interprets escape sequences by default in OS X's /bin/sh. You could also use shopt -u xpg_echo; echo -n instead of printf %s . LC_CTYPE=UTF-8 makes character classes include some non-ASCII characters. If without altering line endings is left out, linefeeds are replaced with carriage returns and a newline at the end of the output is removed.
paragraphs of splits strings around \n, \r, and \r\n. It doesn't strip delimiters.
paragraphs of ("a" & linefeed & "b" & return & "c" & linefeed) --
The plain text version of the clipboard uses CR line endings. This converts line endings to LF:
set text item delimiters to linefeed (paragraphs of (get the clipboard as text)) as text
Unicode text has been equivalent with text and string since 10.5:
There is no longer a distinction between Unicode and non-Unicode text. There is exactly one text class, named “text”: that is, class of "foo" returns text.