Problem with Applescript to get email received date

Discussion in 'Mac Programming' started by blodwyn, Jun 9, 2014.

  1. blodwyn macrumors 65816

    Joined:
    Jul 28, 2004
    Location:
    Portland, Oregon
    #1
    Hi, I'm kinda new to AppleScript and I'm trying to write a script to count the number of email messages in a particular folder, that have been received today (for use in a Geek Tool window). I'm getting an error message (as follows) when I run the script:

    Mail got an error: Can’t make date string of date received of message id 30782 of mailbox "INBOX/Junk Mail" of account "MyEmailAccount" into type specifier.​

    The whole problem seems to be with the following line:
    set messageDate to the date string of the (date received) of thisMsg

    For some reason I can't figure, the "date string" and "date received" don't seem to want to play together. Any pointers or suggestions? Thanks in advance..

    Here's the script so far...

    tell application "System Events" to set MailIsRunning to (name of processes) contains "Mail"
    if MailIsRunning then
    tell application "Mail"
    set junkCount to "0"
    set thisDate to the date string of the (current date)
    repeat with thisMsg in (get every message of mailbox "INBOX/Junk Mail" of account "MyEmailAccount")
    set messageDate to the date string of the (date received) of thisMsg
    if messageDate is equal to thisDate then junkCount = junkCount + 1
    end repeat
    end tell
    return (junkCount as string)
    end if​
     
  2. zeppenwolf macrumors regular

    zeppenwolf

    Joined:
    Nov 17, 2009
    #2
    I got it to work with the following:

    Code:
    set junkCount to "0"
    tell application "System Events" to set MailIsRunning to (name of processes) contains "Mail"
    if MailIsRunning then
    	tell application "Mail"
    		set thisDate to the date string of the (current date)
    		display dialog "thisDate is " & thisDate
    		repeat with thisMsg in (get every message of mailbox "INBOX" of account "zeppenwolf's mail server REDACTED")
    			--		repeat with thisMsg in (get every message of mailbox "INBOX/Junk Mail" of account "MyEmailAccount")
    			set theDateReceived to (date received) of thisMsg
    			-- return theDateReceived
    			set messageDate to the date string of theDateReceived
    			-- display dialog "messageDate is " & messageDate
    			if messageDate is equal to thisDate then
    				--display dialog "junkCount first is " & junkCount
    				set junkCount to junkCount + 1
    				--display dialog "junkCount next is " & junkCount
    				--else
    				--	display dialog "not eq somehow?!??"
    			end if
    		end repeat
    	end tell
    	return junkCount
    end if
    You'll notice I left a bunch of crud in your script, on purpose, to show you how I went about attacking the problems I/you had.

    The first thing is that you reported a problem in the line

    Code:
    set messageDate to the date string of the (date received) of thisMsg
    Ok, so without any clue *why* the line is a problem, I broke it down into the smallest pieces I could:

    Code:
    			set theDateReceived to (date received) of thisMsg
    			-- return theDateReceived
    			set messageDate to the date string of theDateReceived
    			-- display dialog "messageDate is " & messageDate
    
    And printed out some assertions that each line was being reasonable. Surprise, surprise, when I broke the compound line into two smaller ones, that part works. That's just the way AS is, sometimes.

    Also, you had the line "junkCount = junkCount + 1". For some reason I don't completely understand, when junkCount was zero, after that line executed it was *still* zero. That line is legal AS, apparently, but it's not clear what the purpose is since it does nothing. Anyhow, I changed it to the "set" style you see above.

    Also, I changed "INBOX/Junk mail" to just "INBOX", cuz I didn't understand what you were getting at there...
     
  3. blodwyn thread starter macrumors 65816

    Joined:
    Jul 28, 2004
    Location:
    Portland, Oregon
    #3
    Hi Zeppenwolf

    Thanks a whole bunch for your help, and the helpful troubleshooting comments.

    I've been playing with shell scripts as well and obviously had some kind of mental lapse, hence messing up the "set junkCount to ..." statement - thanks for that as well.

    The "INBOX/Junk Mail" reference is needed so I can access the emails in the Junk Mail mailbox on my IMAP providers server (not sure why, but it works). I edited it back into your script, and it now all works as intended.

    Many thanks again for your assistance
    B.
     

Share This Page