Applescript to file message based on recipient

Discussion in 'Mac Programming' started by Froggvox, Oct 6, 2014.

  1. Froggvox macrumors newbie

    Joined:
    Oct 6, 2014
    #1
    Hi there everyone,

    I am very very new to the Applescript world, and have found the existing applescript in OUTLOOK 2011 that files message by SENDER very helpful!

    What i want to do now is to recreate the script but have it file the message based on the recipient (to use to clean up my sent emails into the folder name of the recipient).

    I have played around a lot, and searched online for hours - yet cannot find anything...

    Has anyone created a script for this, and if so please help me out :)

    Thank you!
     
  2. kryten2 macrumors 6502a

    Joined:
    Mar 17, 2012
    Location:
    Belgium
    #2
    What script are you referring to? The only thing I see are some Automator example workflows. Did you find it on the web? Please post a link to it or the location where this script is located on you mac.
     
  3. Froggvox, Oct 6, 2014
    Last edited by a moderator: Oct 7, 2014

    Froggvox thread starter macrumors newbie

    Joined:
    Oct 6, 2014
    #3
    Hi Kryten,

    I am referring to the ones that are pre-loaded into Office 2011 (located in your Library/Application Support/Microsoft/Office/Outlook Script Menu Items folder).

    What I'm wanting is to reconfigure items in the File by Sender script, to make it file by recipient.

    The File by Sender script is:

    Code:
    (*
    	File Message by Sender
    	--
    	Given one or more messages, file the message into a subfolder based on the sender of the message
    *)
    on run {}
    	
    	tell application "Microsoft Outlook"
    		-- Get the messages selected in Outlook
    		set currentMessages to current messages
    		
    		-- Check to make sure items are selected, if not then quit
    		if ((count of currentMessages) < 1) then return
    		
    		-- Iterate through selected messages
    		repeat with aMessage in currentMessages
    			-- Get the account for the message
    			set messageAccount to account of aMessage
    			
    			-- Get the sender for the message
    			set messageSender to sender of aMessage
    			
    			-- Ignore non-exchange messages
    			if ((class of messageAccount is exchange account) and (messageSender is not missing value)) then
    				-- Determine name of  folder to use for filing the message
    				set filingFolderName to missing value
    				try
    					set filingFolderName to name of messageSender
    				end try
    				if (filingFolderName is missing value) then
    					try
    						set filingFolderName to address of messageSender
    					end try
    				end if
    				
    				if (filingFolderName is not missing value) then
    					-- Get the filing folder
    					set filingFolder to missing value
    					set matchingFolders to (mail folders of messageAccount whose name is filingFolderName)
    					if ((count of matchingFolders) > 0) then
    						set filingFolder to (item 1 of matchingFolders)
    					else
    						set filingFolder to make new mail folder at messageAccount with properties {name:filingFolderName}
    					end if
    					
    					-- File the message
    					if filingFolder is not missing value then
    						move aMessage to filingFolder
    					end if
    				end if
    			end if
    		end repeat
    	end tell
    end run
    I just don't know what codes to use... :)

    Thank you
     
  4. kryten2 macrumors 6502a

    Joined:
    Mar 17, 2012
    Location:
    Belgium
    #4
    Here's an example for an IMAP account. Messages will be filed by recipient inside the Sent Messages mail folder on the server. It will duplicate the message for now see the comment in the script. For testing use this on ONE selected mail message and see what it does.

    Code:
    on run {}
    	tell application "Microsoft Outlook"
    		-- Get the messages selected in Outlook
    		set currentMessages to current messages
    		
    		-- Check to make sure items are selected, if not then quit
    		if ((count of currentMessages) < 1) then return
    		
    		-- Iterate through selected messages
    		repeat with aMessage in currentMessages
    			-- Get the account for the message
    			set messageAccount to account of aMessage
    			--log messageAccount
    			-- Get the sender for the message
    			-- set messageSender to sender of aMessage
    			--log messageSender
    			-- Get the recipients for the message. A message can have multiple recipients.
    			-- Try to get the first to recipient
    			try
    				set messageRecipient to email address of to recipient 1 of aMessage
    			on error
    				try
    					set messageRecipient to email address of cc recipient 1 of aMessage
    				on error
    					set messageRecipient to email address of bcc recipient 1 of aMessage
    				end try
    			end try
    			-- I use an IMAP account as example
    			if ((class of messageAccount is imap account) and (messageRecipient is not missing value)) then
    				
    				-- Determine name of  folder to use for filing the message
    				set filingFolderName to missing value
    				try
    					set filingFolderName to name of messageRecipient
    					--log filingFolderName
    				end try
    				if (filingFolderName is missing value) then
    					try
    						set filingFolderName to address of messageRecipient
    						--log filingFolderName
    					end try
    				end if
    				if (filingFolderName is not missing value) then
    					-- Get the filing folder
    					set filingFolder to missing value
    					--set folderNames to name of mail folders of messageAccount
    					--log folderNames
    					-- Looking for mail folder INSIDE the Sent Messages mail folder!
    					set matchingFolders to (mail folders of mail folder "Sent Messages" of messageAccount whose name is filingFolderName)
    					if ((count of matchingFolders) > 0) then
    						set filingFolder to (item 1 of matchingFolders)
    					else
    						set filingFolder to make new mail folder at mail folder "Sent Messages" of messageAccount with properties {name:filingFolderName}
    					end if
    					
    					-- File the message
    					if filingFolder is not missing value then
    						--move aMessage to filingFolder
    						-- Duplicate the message for testing!
    						duplicate aMessage to filingFolder
    					end if
    				end if
    			end if
    		end repeat
    	end tell
    end run
    
    Note : I usually don't use Outlook so I have only done a quick test. I also don't know what's in your contacts in Outlook so name of folder to use for filing the message will either be a name or an email address.
     
  5. Froggvox thread starter macrumors newbie

    Joined:
    Oct 6, 2014
    #5
    Thank you so much Kryten2!!!!

    This works exactly as I need! Perfect!

    Cheers

    Trent
     

Share This Page