Entourage Scripting Starter Snippets Request

Hey all you Entourage scripting gurus!

I’ve enjoyed tremendous success with scripting InDesign and Illustrator (thanks to some help at the get-go from smarty-pants folk like yourself). Where as I understand all the concepts for creating scripts and how to utilize variables, etc… I still find it very difficult to begin scripting a new program without some good example scripts. Syntax is so important!

My ultimate goal is to be able to receive an email from someone with the subject “RunScript” and have it trigger a script that will process the body text. After processing, it will then respond with an email to that recipient saying the body text was processed correctly. To do this I am really looking for some snippets that will let me organize information into variables (like the sender, body text, subject, etc…). I will also want to only do this for emails with that specified subject.

Now I currently have Entourage set up with a rule on the (exchange) setting that will run a script if a new email has “RunScript” in the subject. The trick is to then process all emails with that subject in the order they are received. Any thoughts?

I have included an attempt at what I’m trying to do here:

set emailSender to ""
set emailDate to ""
set emailRecipient to ""
set emailSubject to ""
set emailContent to ""

tell application "Microsoft Entourage"
	activate
	if window 1 = main window then
		set theMsgs to (get incoming messages)
	end if
	
	--HARVEST INFO FROM EMAIL
	tell theMsg
		set emailSender to the address of the sender
		set emailDate to the time sent
		tell address of recipient 1 to set emailRecipient to its display name & space & "<" & its address & ">"
		set emailSubject to the subject
		try
			set emailContent to the content
		end try
	end tell
	set citation to "From: " & emailSender & return & "Date: " & emailDate & return & "To: " & emailRecipient & return & "Subject: " & emailSubject & return & return & emailContent as text
	
	
	--SEND MESSAGE
	set newMsg to make new outgoing message with properties {to recipients:emailSender, subject:emailSubject, content:emailContent}
	tell newMsg
		send
	end tell
	
end tell

I guess you are almost there.
Set a variable x to a particular “Runscript”-msg’s contents and then


try
set resuld to run script x -- resuld will determine your confirmation action(s)
on error resuld
return resuld
end try

Make sure that the Runscript returns a value under ANY condition, otherwise feedback will get lost and will not help you any further.
Use a repeat loop that runs thourgh all messages whose unread status = true, and after successful execution, change their status into “read” & something that is easily discerned or put them into an “executed” folder. This should be easy - as part of the confirmation routine.
At the same time, I’d at least advise an ELEMENTARY form of security by using either a password, an execution timeframe (until next 6 hours) or the like.

Eelco, thanks for the help. Unfortunately, I need more than just that. I somehow need to be able to target just the first email received with the subject “RunScript” and then process the others in line. I also don’t think that my coding is correct because it doesn’t function.

Any more suggestions and tips are very welcome!

Thanks!

Why not cite your script and explain what works (/not)…?

Eelco,

I can’t seem to get a list of messages in Entourage’s inbox. I would also like to be able to filter them by read/unread. I’d like to process them in the order received.

Any thoughts?

I’ve made some small edits to the script and added more comments.

-Evan

global debug

set debug to true

set emailSender to ""
set emailDate to ""
set emailRecipient to ""
set emailSubject to ""
set emailContent to ""

tell application "Microsoft Entourage"
	activate
	if window 1 = main window then
		set listMsgs to (every message)
		--doesn't produce any results. should be only "unread" messages, anyway.
	end if
	
	repeat with thisMsg in listMsgs
		--HARVEST INFO FROM EMAIL
		if subject of thisMsg is "RunScript" then
			if debug is true then display dialog "subject match"
			tell thisMsg
				set emailSender to the address of the sender
				set emailDate to the time sent
				tell address of recipient 1 to set emailRecipient to its display name & space & "<" & its address & ">"
				set emailSubject to the subject
				try
					set emailContent to the content
				end try
			end tell
			set citation to "From: " & emailSender & return & "Date: " & emailDate & return & "To: " & emailRecipient & return & "Subject: " & emailSubject & return & return & emailContent as text
		end if
	end repeat
	
	if debug is true then display dialog ("emailSender" & emailSender as string)
	
	--SEND MESSAGE
	set newMsg to make new outgoing message with properties {to recipients:emailSender, subject:emailSubject, content:emailContent}
	tell newMsg
		send
	end tell
	
end tell

eureka!

i figured out how to access the inbox folder… then the problem was that since i was using a mac, the "inbox folder’ only referred to the one on my computer. however, i use an exchange account (as i would imagine most businesses do, for security). so i had to add that extra bit in to refer to THAT inbox.


tell application "Microsoft Entourage"

	set theMessages to every incoming message of folder "inbox" of (item 1 of every Exchange account) whose ((subject is "RunScript") and (read status is untouched))
	
	repeat with i from 1 to count theMessages
		display dialog (i & "/" & (count theMessages) & " : " & (get content of item i of theMessages) as string)
	end repeat
	
end tell

For me this works (in my local situation with some basic scripting, no Exchange).
I have to convert the (overly) smart quotes that Entourage introduced in the script into regular quotes that Applescript understands.
Some more of those conversions may prove necessary.


tell application "Microsoft Entourage"
	set theMessages to every message of inbox folder whose (read status is untouched) and ((subject is "RunScript"))
	repeat with h in theMessages
		if h's subject is "RunScript" then tell h to set theScript to its content
	end repeat
end tell

set theScript to my substitute(theScript, """, "\"")
set theScript to my substitute(theScript, """, "\"")

try
	run script theScript
on error x
	display dialog x
end try

on substitute(theText, toReplace, newText)
	set AppleScript's text item delimiters to the toReplace
	set the allTheText to every text item of theText
	set AppleScript's text item delimiters to the newText
	set theText to the allTheText as string
	set AppleScript's text item delimiters to ""
	return theText
end substitute