Mail Applescript error

Discussion in 'Mac Programming' started by moose151, Oct 23, 2009.

  1. macrumors newbie

    Joined:
    Oct 23, 2009
    #1
    :confused:
    This is my first attempt at Applescript. I 'm try to write a script to move messages that are older than 30 days from one mailbox to another. Here is my script:

    tell application "Mail"
    activate
    set lwdgInbox to mailbox "INBOX" in account "LWDG - Jules"
    set archiveBox to mailbox "Archive"

    --set numMsg to count of messages in archiveBox
    --display dialog numMsg

    set dateLimit to 24 * 3600 * 30 -- 30 days
    repeat with msg in messages in lwdgInbox
    set dateRcvd to date received in msg
    if ((current date) - dateRcvd) > dateLimit then
    display dialog date string of dateRcvd
    move the msg to archiveBox
    end if
    end repeat
    end tell

    When I run it I get "Mail got an error: NSInternalScriptError" in the evnt log. is there anyone wh would knw why this is?
     
  2. macrumors 603

    Joined:
    Aug 9, 2009
  3. macrumors 65816

    mysterytramp

    Joined:
    Jul 17, 2008
    Location:
    Maryland
    #3
    "NSInternalScriptError" essentially means, I think, that Mail got the "move" message but it has no idea what to do with it. "Move" is part of the standard dictionary; every scriptable app should have it, but it's irrelevant to lots of apps. Although you want to "move" a message from one mailbox to another, Mail doesn't understand what the script is saying.

    A message's "mailbox" is actually a property you can set:

    Code:
    set mailbox of message x to mailbox "Where I Want It"
    
    Also, if you run this under Snow Leopard, your event list should also have reported -10004 errors every time you called "current date." Basically that is, I think, using a command inside a tell block that doesn't belong. "current date" is a function that you'd be better off calling once. I re-jiggered your code so you eliminate one calculation inside the repeat loop.

    Code:
    set compareToThisDate to (the current date) - (24 * 3600 * 90) -- 90 days
    
    tell application "Mail"
    	activate
    	
    	--set numMsg to count of messages in archiveBox
    	--display dialog numMsg
    	
    	repeat with msg in messages in inbox
    		set dateRcvd to date received in msg
    		if compareToThisDate > dateRcvd then
    			display dialog date string of dateRcvd
    			set mailbox of msg to mailbox "Archive"
    		end if
    	end repeat
    	
    end tell
    mt
     
  4. thread starter macrumors newbie

    Joined:
    Oct 23, 2009
    #4
    Awesome, this works. Thanks a lot!:)
     

Share This Page