strip string from string

I have a script that saves off parts of an email to a text file. Ocassionally the subject headings of the mails are prepended with *** SPAM *** thanks to my webhost. I would like this to be removed so it doesn’t end up in the subject of my exported file.

I am looking for what to do in my script to take my subject string and strip off this spam label.

I am very novice with AppleScript.

If the formatting is always the same, something like this should work for you…

set theInput to "*** SPAM ***Make millions selling chia-pets!!!"
set theOutput to (characters 13 through -1 of theInput) as string

j

The spam section isn’t always there, so I need to qualify it.

I want to say something like…

If Subject begins with “*** SPAM "
then set Subject to ( Subject - "
SPAM ***” )

How about…

set Subject to "*** SPAM ***Make millions selling chia-pets!!!"

set spam to "*** SPAM ***" --> The string to look for

if (characters 1 through (count characters of spam) of Subject as string) is equal to spam then
	set Subject to ((characters ((count characters of spam) + 1) through -1 of Subject) & spam) as string
end if

j

Thank you for your suggestions.

Unfortunately I’m not getting into that if clause. I’ve debugged enough to know that.

If I comment out the IF so that I can test the replacement line, I find that my text file gets an odd version of the subject.

Let’s say the subject is:
free download

With your code my text file gets:
F) r) e) e) ) O) M) F) ) E) x) p) o) r) t) e) r) ) D) o) w) n) l) o) a) d)

If I comment out the “as string” part of your code I get (those are tabs between each character):
F r e e D o w n l o a d

What I was getting before all this was fine except for the spam part:
SPAM Free Download

That looks like a text item delimiter problem. If the delimiters are set to anything other than the default {“”}, you’ll get the delimiter characters inserted between the characters of Subject when you coerce them back to string. If you quit the program running the script, then relaunch it and try again, you may find that everything’s OK. Alternatively, you could try inserting this at the top of the script:

set applescript's text item delimiters to {""}

Better still, eshew the use of ‘characters … as string’ altogether. It’s very inefficient. Use ‘text’ instead:

set Subject to "***SPAM*** Free Download"

set spam to "***SPAM***" --> The string to look for

if Subject begins with spam then
  -- Cut "***SPAM***" and the following space.
  set Subject to text ((count spam) + 2) thru -1 of Subject
end if

Hooray! That did it! Thank you.