So I here is the question I have a script that parses text. if the text is contained in a text file that I read in and parse the script works fine. But when I set the text to the contents of a message from mail the script breaks.
My assumption was that I need to convert its format but I’ve tried everything I know of and can’t seem to figure out how to make it work. Does anyone know what the difference in format would be ?
Hard to say when you don’t show the script. Are you using text item delimiters to parse it? Is the mail message in plain ASCII text, Unicode text, UTF-8, or RTF?
tell application "Mail"
set theMsgs to selection
repeat with aMsg in theMsgs
set orderdate to date sent of aMsg
set theContent to content of aMsg as string
set input to paragraphs of theContent
my ParseData(input)
end repeat
end tell
on ParseData(input)
set tid to AppleScript's text item delimiters
set AppleScript's text item delimiters to {":"}
set labelList to {}
set slabelList to {} -- shipping label list, since the shipping section uses the same labels as the main section - BH
set valueList to {}
set svalueList to {} -- shipping value list - BH
set shipFlag to false -- flag that gets turned on when we're in the shipping section - BH
set iRef to a reference to input
set orderdate to current date
--try
set lineCount to 1
set aLine to ""
-- Scan for start of data
repeat while aLine does not contain "Name:"
set aLine to item lineCount of iRef
set lineCount to lineCount + 1
end repeat
repeat with j from lineCount - 1 to (count iRef)
set aLine to item j of iRef
if aLine contains "Shipped to" then set shipFlag to true -- if we're in the shipping section, grab those fields separately - BH
if aLine contains ":" then
set templist to text items of aLine
repeat with i from 1 to (count templist) -- Trim leading and trailing spaces
set anItem to item i of templist
if anItem is " " then -- Handle if just one space
set anItem to ""
else
repeat while (anItem begins with " ") and (anItem & " ")
set anItem to text 2 thru -1 of anItem
end repeat
repeat while (anItem ends with " ") and (anItem & " ")
set anItem to text 1 through -2 of anItem
end repeat
end if
if anItem = " " then set anItem to "" -- Single space left
set item i of templist to anItem
end repeat
-- add to a different list depending on whether we're in the shipping section or not - BH
if shipFlag is true then
copy ("|" & item 1 of templist & "|") to end of slabelList
copy item 2 of templist to end of svalueList
else
copy ("|" & item 1 of templist & "|") to end of labelList
copy item 2 of templist to end of valueList
end if
else -- Do nothing, skip the line
if aLine contains "Thank you for your order" then exit repeat
end if
end repeat
end ParseData
this is the error I get
"Can't make \" catherine hall \" into type boolean."
thanks for the tip I had miss typed what was suppose to be there as that part of the script was giving to me in a format where I couldn’t tell what all the characters where.
so this
repeat while (anItem begins with " ") and (anItem & " ")
set anItem to text 2 thru -1 of anItem
end repeat
repeat while (anItem ends with " ") and (anItem & " ")
set anItem to text 1 through -2 of anItem
end repeat
should have been
repeat while (anItem begins with " ") and (anItem ≠" ")
set anItem to text 2 thru -1 of anItem
end repeat
repeat while (anItem ends with " ") and (anItem ≠" ")
set anItem to text 1 through -2 of anItem
end repeat