Help with Applescript Rules

Discussion in 'Mac Programming' started by dchiusa, Jan 17, 2014.

  1. dchiusa macrumors newbie

    Joined:
    Jan 17, 2014
    Location:
    Nashville
    #1
    This is my first time making an Applescript. The script redirects mail based on several rules setup inside Apple Mail. My problem is that when the message is redirected to the new recipient, they receive it just fine but it has duplicated the Body of the email once for every Rule setup inside Apple mail. I can’t figure out why it is doing that so I thought perhaps if I manually enter the Rules inside the Applescript and bypass using Apple Mail for the Rules, it would solved the problem. I can’t figure out how to write the rules in Applescript. Here are my rules setup in Apple Mail:

    If All of the following conditions are met:

    Account - Dchiusa
    From - Contains: livechatinc.com
    Message Content - Contains: donald
    Message Content - Contains: duration

    Below is my Applescript. If someone could help with this, thank you very much. I’ve been using Applescript for 2 days and trying to learn this.

    Code:
    using terms from application "Mail"
    	on perform mail action with messages theMessages
    		repeat with thisMessage in theMessages
    			tell application "Mail"
    				
    				set theText to content of thisMessage
    				set newMessage to redirect thisMessage with opening window
    				tell newMessage
    					set subject of newMessage to "Live Chat Transcript@Live Chat"
    					make new to recipient at beginning of to recipients with properties {address:”emailaddress”}
    				end tell
    				set the sender of newMessage to “emailaddress”
    				
    				
    				
    				send newMessage
    			end tell
    		end repeat
    	end perform mail action with messages
    end using terms from
    
     
  2. kryten2 macrumors 6502a

    Joined:
    Mar 17, 2012
    Location:
    Belgium
    #2
    For creating your rules for Mail in Applescript you could do something like this :

    Code:
    set theAccountType to «constant eruttacc»
    
    tell application "Mail"
    	set newRule to make new rule at end of rules with properties {name:"TestRule5", all conditions must be met:true, redirect message:"blabla@some.org", enabled:true}
    	tell newRule
    		-- Change expression:"Some Account" into a real account. Look inside your ~/Library/Mail/V2/ folder.
    		-- Example : "~/Library/Mail/V2/IMAP-xxxxxx@xxx.xxx@xxxx.xxx.xxx"
    		make new rule condition at end of rule conditions with properties {rule type:theAccountType, expression:"Some Account"}
    		make new rule condition at end of rule conditions with properties {rule type:from header, qualifier:does contain value, expression:"livechatinc.com"}
    		make new rule condition at end of rule conditions with properties {rule type:message content, qualifier:does contain value, expression:"donald"}
    		make new rule condition at end of rule conditions with properties {rule type:message content, qualifier:does contain value, expression:"duration"}
    		-- set run script to file "disk:path:to:script.scpt"
    		-- Example see below
    		set run script to file "nameofyourstartuphddisk:Users:yourusername:Library:Application Scripts:com.apple.mail:testscript.scpt"
    	end tell
    end tell
     
    Note : After running the script I saw properties set in the script not sticking in the UI of the Mail application yet getting properties of the rule showed everything was set. In order to keep my sanity I decided to stop trying. This exercise in pain was done in Mail Version 7.2 (1859).
     
  3. dchiusa thread starter macrumors newbie

    Joined:
    Jan 17, 2014
    Location:
    Nashville
    #3
    Thanks kryten2 for your help. I still can't figure out why the content of the message is duplicated several times. When the new message is received, the body of the message is duplicated. Sometimes 2 times, sometimes 3 or 4 times. Perhaps something is off in my first part of the code but I can't figure it out. Anyone have an idea. All I want to is forward mail to another address but I need to change the subject to Live Chat before it is forwarded. Help please
     
  4. kryten2, Jan 18, 2014
    Last edited: Jan 18, 2014

    kryten2 macrumors 6502a

    Joined:
    Mar 17, 2012
    Location:
    Belgium
    #4
    You're not the only one struggling with this. Info : AppleScript to Forward or Redirect an email and while-redirecting-email-with-applescript-the-content-of-the-email-is-duplicating. After some experimentation I did found something that seems to work when I apply rules while normally it should not. Give this a try :

    Code:
    using terms from application "Mail"
    	on perform mail action with messages theMessages for rule theRule
    		tell application "Mail"
    			delay 1
    			repeat with eachMessage in theMessages
    				--set theText to content of eachMessage
    				set newMessage to redirect eachMessage with opening window
    				tell newMessage
    					set subject of newMessage to "Live Chat Transcript@Live Chat"
    					make new to recipient at beginning of to recipients with properties {address:"bla@some.org"}
    				end tell
    				set the sender of newMessage to "bla@some.org"
    				set the content of newMessage to ""
    				--set content of newMessage to theText
    				--send newMessage
    			end repeat
    		end tell
    	end perform mail action with messages
    end using terms from
    
    Note : In my testing I always ended up with the original content of eachMessage in the redirect mail opening window. Which is crazy because I'm setting the content of newMessage to an empty string.
     
  5. dchiusa thread starter macrumors newbie

    Joined:
    Jan 17, 2014
    Location:
    Nashville
    #5
    Thanks for the help, that worked great. I'm wondering if some of the other mail programs such as Postbox will have that issue. They support some limited applescpript now I think and will be adding more in the future. Would be interesting to see if it happens with anther mail program. Again, thanks a lot.
    Don




     

Share This Page