Applescript help: creating a date from text in Outlook email

Discussion in 'Mac Programming' started by Rossdillon, Feb 21, 2014.

  1. Rossdillon macrumors newbie

    Joined:
    Feb 20, 2014
    Location:
    Las Vegas
    #1
    My wife receives emails with substitute teaching assignments. My goal is to automate the conversion of those emails into calendar events by parsing out the relevant information from the emails.

    I have an Applescript that I shamelessly borrowed from the web (knowing nothing about Applescript until a few days ago) and tweaked until it worked for everything...except the dates. To simplify this post I've stripped out most of the code except for one section that works and a section that doesn't;hopefully somebody will be able to tell me how to get a date to work.

    The email content contains lines that looks like the following:
    Code:
    Location: MOORE, WILLIAM ES
    Start Date: 2014-03-11
    End Date: 2014-03-11
    Using the code below I successfully converted the "Location" line into the location field of the event. However, when I tried parsing out the date info, creating a string, and then turning that into a date, I get errors at "date (myStartMonth & "/" & myStartDay & "/" & myStartYear)". The error reads Microsoft Outlook got an error: Can’t get date "03/11/2014". However, if actually replace the line with date ("03/11/2014") it works fine. It's the combining of strings that is failing.
    The odd thing is a similar construct worked fine a standalone code...it appears to fail when inside the "Tell application" line.

    Any help on how to fix this and successfully convert the start date text into an Applescript date would be gratefully appreciated.

    Thanks
    Rossdillon
    Code:
    tell application "Microsoft Outlook"
    	repeat with theMessage in selectedMessages
    		
    		set theContent to content of theMessage
    		set theCategory to category of theMessage
    		
    		-- Find the location from the email
    		set mylocation to {}
    		repeat with aParagraph in (paragraphs of theContent)
    			set aParagraph to contents of aParagraph 
    			if aParagraph begins with "Location:" then
    				set the end of mylocation to text (word 2 of aParagraph) thru -1 of aParagraph
    				exit repeat
    			end if
    		end repeat
    		--end repeat
    		set theLocation to mylocation
    
    		-- Find the "Start date" from the email
    		repeat with aParagraph in (paragraphs of theContent)
    			set aParagraph to contents of aParagraph
    			if aParagraph begins with "Start Date:" then
    				set myStartMonth to (characters 18 thru 19 of aParagraph) as string
    				set myStartDay to (characters 21 thru 22 of aParagraph) as string
    				set myStartYear to (characters 13 thru 16 of aParagraph) as string
    				set myStartDate to date (myStartMonth & "/" & myStartDay & "/" & myStartYear)
    				exit repeat
    			end if
    		end repeat
    
    		set newEvent to make new calendar event with properties {all day flag:true, subject:theName, category:theCategory, content:theContent, location:theLocation, start time:myStartDate, end time:myEndDate}
    		
    		open newEvent
    	end repeat
    end tell
     
  2. Rossdillon thread starter macrumors newbie

    Joined:
    Feb 20, 2014
    Location:
    Las Vegas
    #2
    I was right...

    Once I split the Tell Application into two parts, the get data part and the create new event part, and did all the date manipulating outside the Tell function, it worked. Bizarre but OK...
     

Share This Page