Monthly Archives: September 2017

Parse e-mail for UPS tracking numbers

This AppleScript will parse the selected e-mail in Mail.app for UPS tracking numbers and will open the tracking page of the UPS website in your default browser.

This only looks at a single e-mail, but could be made to repeat through multiple selections.

tell application "Mail"
  set _messages to selection as list
  set input to content of item 1 of _messages
  try
    open location my parse_nums(input)
  on error
    display dialog "No UPS tracking info found."
  end try
end tell

on parse_nums(input)
  set input_new to ""
  set output to ""
  set input to every word of input
  repeat with i in input
    try
      if first character of i is "1" and second character of i is "Z" then --Every UPS # I've seen starts with '1Z'
        set input_new to input_new & i & "%0D%0A" as string -- 
URL encoded carriage return line feed needed for multiple entries.
      end if
    end try
  end repeat
-- Now to remove the last set of new line characters.
  repeat with c in characters 1 through ((count of characters of input_new) - 6) of input_new
    set output to output & c
  end repeat
  set the_URL to "https://wwwapps.ups.com/WebTracking/track?track.x=Track&trackNums=" & output
  return the_URL
end parse_nums