0

I'm trying to substitute an email address for a symbolic character in a block of text.

The text block:

Dear %%FirstName%%-
... 
This message was sent to %%EmailAddress%%.

The script:

...
set theBody to my replace(theBody, "%%FirstName%%", "First")
set theBody to my replace(theBody, "%%EmailAddress%%", "first.last@company.com")
...

Replace sub-procedure (AppleScript: Essential Sub-Routines):

on replace(this_text, search_string, replacement_string)

    set AppleScript's text item delimiters to the search_string
    set the item_list to every text item of this_text
    set AppleScript's text item delimiters to the replacement_string
    set this_text to the item_list as string
    set AppleScript's text item delimiters to ""
    return this_text

end replace

The result:

Dear First-
... 
This message was sent to .

If I change the script to:

...
set theBody to my replace(theBody, "%%FirstName%%", "First")
set theBody to my replace(theBody, "%%EmailAddress%%", "first.lastATcompany.com")

The result:

Dear First-
... 
This message was sent to first.lastATcompany.com.

What is wrong with the replace sub-procedure?

** edit **

I'm collecting a distinct list email addresses from an Outlook Contact:

...
set theAddresses to email addresses of theContact
set addressList to {}
repeat with theAddress in the theAddresses
  if addressList does not contain (address of theAddress) then
    set addressList to addressList & (address of theAddress)
  end if
end repeat
...

Then using the list:

...
repeat with theAddress in addressList
  ...
  make new recipient at newMessage with properties {email address:{name:displayName of theContact, address:theAddress}}
  ...
end repeat
...
1

I tried running the first example:

set theBody to "Dear %%FirstName%%-
... 
This message was sent to %%EmailAddress%%."

set theBody to my replace(theBody, "%%FirstName%%", "First")
set theBody to my replace(theBody, "%%EmailAddress%%", "first.last@company.com")

on replace(this_text, search_string, replacement_string)
    set AppleScript's text item delimiters to the search_string
    set the item_list to every text item of this_text
    set AppleScript's text item delimiters to the replacement_string
    set this_text to the item_list as string
    set AppleScript's text item delimiters to ""
    return this_text
end replace

The result was:

Dear First-
... 
This message was sent to first.last@company.com.

So it seemed to work just fine. The replace handler shouldn't have any problems with strings that contain special characters, so there might be something else you're missing.

Also restoring text item delimiters is not necessary as far as I know, and you don't need AppleScript's outside tell blocks, so the replace handler can be simplified to something like this:

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
1
  • Lauri, How did you hide your name to only show points, badges, etc?
    – adayzdone
    Sep 5 '13 at 19:39

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .