The Omni Group Forums

The Omni Group Forums (http://forums.omnigroup.com/index.php)
-   AppleScripting Omni Apps (http://forums.omnigroup.com/forumdisplay.php?f=46)
-   -   Apple Mail Msg to OO3 - Add indented row (http://forums.omnigroup.com/showthread.php?t=28748)

Simon Knight 2013-03-18 06:45 AM

Apple Mail Msg to OO3 - Add indented row
 
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[/CODE]

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

Simon Knight 2013-03-18 08:04 AM

Solved....
 
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[/CODE]

It is a simple matter to modify the script to write to more columns e.g. To: and From: if required.

Simon

RobTrew 2013-03-19 03:49 AM

Hi Simon,

Looks like you found a good solution.

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

[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[/CODE]

Simon Knight 2013-03-19 10:39 AM

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}[/CODE] 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

RobTrew 2013-03-19 12:04 PM

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[/CODE]

[COLOR="White"]--[/COLOR]


All times are GMT -8. The time now is 03:24 PM.

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