The Omni Group
These forums are now read-only. Please visit our new forums to participate in discussion. A new account will be required to post in the new forums. For more info on the switch, see this post. Thank you!

Go Back   The Omni Group Forums > Developer > AppleScripting Omni Apps
FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
Apple Mail Msg to OO3 - Add indented row Thread Tools Search this Thread Display Modes
Hi,
I am experimenting with an Applescript that will copy an email message with header information into an Outline. The Outline must have at least two columns : one named "Subject" the second "Date". The date column should be formatted as a ling date. The script will run and both copy the email to the clipboard and into the outline.
Code:
tell application "Mail"
	-- get all the messages in the list with same subject
	set tMessageList to selection
	
	--set theMessageID to the message id of item 1 of tMessage
	--set thesubject to the subject of item 1 of tMessage
	--set theheaders to the all headers of item 1 of tMessage
	--set thehead to the headers of item 1 of the selection
	set tEmail to ""
	-- loop thorough all the messages in the list or perhaps not
	--repeat with x from 1 to (count tMessageList)
	-- comment out the repeat loop and set x to 1 to just copy one message
	set x to 1
	--From:
	set tSender to the sender of (item x) of tMessageList
	--Subject:
	set tSubject to the subject of (item x) of tMessageList
	--Date:
	set tDateSent to the date sent of (item x) of tMessageList
	--To:
	set tRecipients to the recipients of (item x) of tMessageList
	set tAddresseeList to ""
	repeat with n from 1 to (count tRecipients)
		set tAddressee to (item n) of tRecipients
		set tAddresseeList to tAddresseeList & address of tAddressee & return
	end repeat --list of recipients
	-- Message Body
	set tContent to the content of (item x) of tMessageList
	-- end the loop through message list
	-- build the data
	set tEmail to tEmail & "XXFrom: " & tSender & return
	set tEmail to tEmail & "Subject: " & tSubject & return
	set tEmail to tEmail & "Date: " & tDateSent & return
	set tEmail to tEmail & "To: " & tAddresseeList & return
	set tEmail to tEmail & tContent & return
	
end tell -- Apple Mail

set original to front document of application "OmniOutliner Professional"
tell front document of application "OmniOutliner Professional"
	-- Trap for script being run in a brand new blank documnt
	-- Later perhaps
	-- check that script is applicable to the OO file  should contain 4 columns
	if title of every column does not contain {"Subject", "Date"} then
		
		display dialog "You need two columns named 'Subject,Date,' to use this script " buttons "Cancel" default button 1
	end if
	
	-- A brand new document may not have any rows so trap for this condition
	-- create a blank row if the document has no rows (i.e brand new document) - unlikely
	if not (exists (last row of original)) then
		tell original
			make new row at end of rows
		end tell
	end if
	set tRow to the last row of original
	set value of cell "Subject" of tRow to tSubject
	set value of cell "Date" of tRow to tDateSent
	--set LastSection to last child of original -- 
	
	--add a new row as error is thrown when there are no rows in section at level
	--set MessageData to make new child at end of tRow of LastSection --with properties {topic:dateString}
	-- add new row for data
	--make new child of tRow --whose level = 2
	set DataRow to make new child at end of rows of original
	--set tRow to the last row of original
	set value of cell "Subject" of DataRow to tEmail
end tell -- OO3
The problem is at the end : I wish to add the body of the email (tEmail) to a row that is a child of the row above. I have tried all sorts of syntax but failed each time. I know it must be simple but .......

Thanks in anticipation
Simon
 
I solved my own question but feel free to offer better syntax. The code below requires a OO3 file with two named columns: "Subject" and "Date" and the date column should be defined as a date column. As many other columns may be added as required.

I plan to run the script from a button on the OO3 button bar. The script requires that Apple Mail is open and an email selected. When run the header and text of the email is copied across to the OO3 document. At present the script does not check if an email is selected (to do).
Code:
tell application "Mail"
	-- get all the messages in the list with same subject
	set tMessageList to selection
	set tEmail to ""
	set x to 1 -- Originally I grabbed all the emails in the selection
	
	--From:
	set tSender to the sender of (item x) of tMessageList
	
	--Subject:
	set tSubject to the subject of (item x) of tMessageList
	
	--Date:
	set tDateSent to the date sent of (item x) of tMessageList
	
	--To:
	set tRecipients to the recipients of (item x) of tMessageList
	set tAddresseeList to ""
	repeat with n from 1 to (count tRecipients)
		set tAddressee to (item n) of tRecipients
		set tAddresseeList to tAddresseeList & address of tAddressee & return
	end repeat --list of recipients
	
	-- Message Body
	set tContent to the content of (item x) of tMessageList
	-- end the loop through message list
	-- build the data 
	set tEmail to tEmail & "From: " & tSender & return
	set tEmail to tEmail & "Subject: " & tSubject & return
	set tEmail to tEmail & "Date: " & tDateSent & return
	set tEmail to tEmail & "To: " & tAddresseeList & return
	set tEmail to tEmail & tContent & return
	
end tell -- Apple Mail

set tDocument to front document of application "OmniOutliner Professional"
tell front document of application "OmniOutliner Professional" --why does tell tDocument fail ?
	
	-- check that script is applicable to the OO file  should contain 2 columns
	if title of every column does not contain {"Subject", "Date"} then
		
		display dialog "You need two columns named 'Subject,Date,' to use this script " buttons "Cancel" default button 1
	end if
	-- Does cancel stop the script ?  Yes
	
	--If OO doc does not have a last section add one called E-Mails
	try
		set LastSection to last child of tDocument -- we only have one section at present named E-Mails
	on error
		tell tDocument --original
			make new row at end of rows with properties {topic:"E-Mails"} -- this will become the email section
		end tell
		set LastSection to last child of tDocument
	end try
	-- add a new child row for the header of the email
	set LastRow to make new child at end of rows of LastSection with properties {topic:tSubject}
	set value of cell "Date" of LastRow to tDateSent -- probably syntax to add this in the topic list ?
	
	-- Add a new child row to store the email body
	set EmailBody to make new child at end of rows of LastRow with properties {topic:tEmail}
	
end tell
It is a simple matter to modify the script to write to more columns e.g. To: and From: if required.

Simon
 
Hi Simon,

Looks like you found a good solution.

FWIW the idiom which seems the most intuitive to me is that of making a new row at the end of the children of a given row.

Code:
tell application id "OOut"
	tell front document
		set oRow to first selected row
		
		-- new row at end of children
		make new row at end of children of oRow
		
		set expanded of oRow to true
	end tell
end tell
 
Rob,
Thanks for your clear piece of code. What is your view on the use of the line
Code:
set LastRow to make new child at end of rows of LastSection with properties {topic:tSubject}
while its powerful I suspect that in 6 plus months time I will wonder what it is doing i.e. defines and sets a variable to a new row it adds to the document plus setting a value into that row - phew?

Simon
 
Well, you can always break it up or bracket it to the point where it begins to feel more readable.

Vertical is cognitively easier than horizontal, on the whole, but there are plenty of available points on the spectrum :-)

Code:
set tSubject to "Some subject"

tell application id "OOut"
	tell front document
		tell last child
			tell (make new row at end of children) to set topic to tSubject
			set expanded to true
		end tell
	end tell
end tell
--

Last edited by RobTrew; 2013-03-19 at 01:47 PM..
 
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Postbox as alternative to Apple Mail Mixalis OmniFocus 1 for Mac 12 2011-01-06 02:26 PM
Inconsistent Clipping with Apple Mail nickgrossman OmniFocus 1 for Mac 1 2010-09-27 10:15 AM
Apple Mail to Omnifocus, with attachment chipjoyce Applying OmniFocus 1 2010-07-14 01:45 PM
clipping from Apple Mail dsolomon OmniFocus 1 for Mac 12 2007-11-25 08:04 AM
Apple Mail integration johnrover OmniFocus 1 for Mac 6 2007-06-10 10:13 AM


All times are GMT -8. The time now is 09:22 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2017, vBulletin Solutions, Inc.