Question

I have the challenge in AppleScript to manipulate a string as follows:

  • Base string is an email recipient display name, say: First Last (first.last@hotmail.com)
  • I'd like to "trim" the display name to remove the actual email address in the brackets
  • The desired result should be First Last - so the space in front of the first bracket needs to be removed.

What is the best and most efficient way to do this in AppleScript?

Was it helpful?

Solution

set theSample to "First Last (first.last@hotmail.com)"

return trimEmailAddress(theSample)
-->Result: "First Last"

on trimEmailAddress(sourceAddress)
    set AppleScript's text item delimiters to {" ("}
    set addressParts to (every text item in sourceAddress) as list
    set AppleScript's text item delimiters to ""
    set nameOnly to item 1 of addressParts

    return nameOnly
end trimEmailAddress

OTHER TIPS

I'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 (first.last@hotmail.com)"
set pos to offset of " (" in x
{text 1 thru (pos - 1) of x, text (pos + 2) thru -2 of x}

As far as I know, you don't have to restore text item delimiters.

set x to "First Last (first.last@hotmail.com)"
set text item delimiters to {" (", ")"}
set {fullname, email} 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 {input}
    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

Reversing text:

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)
-- {"a", "b", "c", ""}

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.

You may want to use a simpler solution like this:

set theSample to "First Last (first.last@hotmail.com)"

on trimEmailAddress(sourceAddress)
    set cutPosition to (offset of " (" in sourceAddress) - 1
    return text 1 thru cutPosition of sourceAddress
end trimEmailAddress

return trimEmailAddress(theSample)
-->  "First Last"
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top