The Omni Group Forums

The Omni Group Forums (http://forums.omnigroup.com/index.php)
-   OmniFocus Extras (http://forums.omnigroup.com/forumdisplay.php?f=44)
-   -   Applescript to capture list of actions to clipboard (http://forums.omnigroup.com/showthread.php?t=19326)

JohnJ80 2010-12-09 11:35 AM

Applescript to capture list of actions to clipboard
 
I wrote this to be able to make a list of action items that I can then paste into an email, a document, etc... The way I use this is to use OF to take meeting action items then I can select them all in OF, run this script, and the information is transferred to the clipboard. I can then paste them into an email, into a text file, into a word processor etc....

Use it if it's useful. No claims made on accuracy, effectiveness or the safety of your data. It's just written so no claim on being bug free:


tell front document of application "OmniFocus"
-- get the window the user is using
set |w| to first document window whose index is 1
-- find out where we are and if there's anything we can do

set theItems to selected trees of content of |w|

-- Detect common failure modes and explain

if ((count of theItems) is 0) then
display alert ¬
"Select an action to " & toolName message "You have not selected an action"
return
end if

(*) if ((count of theItems) is greater than 1) then
display alert ¬
"Select just one action to " & toolName message "You have selected more than one item, please select just one"
return
end if *)

set _body to "" -- null out text
repeat with cnt from 1 to (count of theItems)

set selectedItem to value of item cnt of theItems

if ((class of selectedItem) is inbox task) then
display alert ¬
"Sorry, action not supported" message "Actions in inbox do not reveal their projects in version " & toolVersion & " of " & toolName
return
end if


if ((class of selectedItem) is not task) then
display alert ¬
"Select an action to " & toolName message "You have selected something that isn't an action"
return
end if

try
set _dueDate to (get due date of selectedItem as date)
on error
set _dueDate to "None"

(* set _dueDate to text returned of (display dialog "Please enter data and time xx/xx/xxxx xx:xx AM/PM." default answer (current date) as string)
set _dueDate to _dueDate as date *)
end try


try
set _note to (get note of selectedItem)
on error
set _note to ""
end try


try
set _project to the name of (get containing project of selectedItem)
on error
set _project to "None"
end try

try
set _context to the name of (get context of selectedItem)
on error
set _contect to "None"
end try


try
set _subject to the name of selectedItem
on error
set _subect to "No Subject"
end try

set _body to _body & "Action Item: " & _subject & space & return
set _body to _body & "Due by: " & _dueDate & return
set _body to _body & "Project: " & _project & return
set _body to _body & "Who: " & _context & return & return



end repeat
end tell

set the clipboard to _body

RobTrew 2010-12-09 12:44 PM

Thanks, nice idea.

You may need to define your toolName variable, and decide what will happen if no Context is assigned.

An abbreviation might look something like:
[CODE]tell application id "com.omnigroup.omnifocus"
tell content of front document window of front document
set lstActions to value of (selected trees where class of value is task)
set str to ""
repeat with oAction in lstActions
tell oAction
set {strName, dteDue, oProject, oContext} to {name, due date, containing project, its context}
set str to str & "Action Item: " & strName & return
if dteDue is not missing value then set str to (str & "Due by: " & dteDue as string) & return
if oProject is not missing value then set str to str & "Project: " & name of oProject & return
if oContext is not missing value then set str to str & "Who: " & name of oContext & return
set str to str & return
end tell
end repeat
end tell
end tell
if length of str > 0 then set the clipboard to str[/CODE]

(You have probably noticed that there is also an [URL="http://forums.omnigroup.com/forumdisplay.php?f=44"]OmniFocus Extras[/URL] forum, on which most scripts tend to be posted).

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

Brian 2010-12-09 05:26 PM

Thanks for posting this, Johnj80!

JohnJ80 2010-12-09 08:41 PM

[QUOTE=RobTrew;90212]Thanks, nice idea.

You may need to define your toolName variable, and decide what will happen if no Context is assigned.

An abbreviation might look something like:
[CODE]tell application id "com.omnigroup.omnifocus"
tell content of front document window of front document
set lstActions to value of (selected trees where class of value is task)
set str to ""
repeat with oAction in lstActions
tell oAction
set {strName, dteDue, oProject, oContext} to {name, due date, containing project, its context}
set str to str & "Action Item: " & strName & return
if dteDue is not missing value then set str to (str & "Due by: " & dteDue as string) & return
if oProject is not missing value then set str to str & "Project: " & name of oProject & return
if oContext is not missing value then set str to str & "Who: " & name of oContext & return
set str to str & return
end tell
end repeat
end tell
end tell
if length of str > 0 then set the clipboard to str[/CODE]

(You have probably noticed that there is also an [URL="http://forums.omnigroup.com/forumdisplay.php?f=44"]OmniFocus Extras[/URL] forum, on which most scripts tend to be posted).

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

Whatever. Works for me. It'll just fail with an applescript error and I'll just add the context which it should have anyhow.

J.

RobTrew 2010-12-09 09:20 PM

[QUOTE=JohnJ80;90229]It'll just fail with an applescript error and I'll just add the context which it should have anyhow.[/QUOTE]

Yes - a pity to discard information. In case anyone finds it useful, here is a variant which will prompt the user to supply the missing context (by optionally choosing it from a list).

[CODE]property pTitle : "Selected actions to RTF clipboard"
property pVer : "0.6"

-- Uses table format to align labels and allow wrapping of note.

property pblnIncludeNotes : true

property pTaskLabel : "Action Item:"
property pDueLabel : "Due by:"
property pProjLabel : "Project:"
property pContextLabel : "Who:" -- or "Context: " edit as required
property pNoteLabel : "Note:"

-- edit font, point size, color, margins etc as required:
property pstrCSS : "
.TaskLabel {color:#617794;}
.DueLabel {color:#617794;}
.ProjectLabel {color:#617794;}
.ContextLabel {color:#617794;}
.NoteLabel {color:#617794; }

.Task {color:#3c6ba9; font-weight: bold;}
.Due {color:#3c6ba9;}
.Project {color:#3c6ba9;}
.Context {color:#ff0000;}
.Note {color: #666666}

td {font: 14.0px 'Lucida Grande';}
"

tell application id "com.omnigroup.omnifocus"
set oDoc to front document
tell content of front document window of oDoc
set lstActions to value of (selected trees where class of value is task or class of value is inbox task)
set lngActions to count of lstActions
if lngActions < 1 then
tell application id "com.apple.systemevents"
activate
display dialog "Select one or more (non-inbox) actions and run this script" buttons {"OK"} default button "OK" with title pTitle
end tell
return
end if
set str to ""
repeat with oAction in lstActions
set {strName, dteDue, oProject, oContext, strNote} to {name, due date, containing project, its context, note} of oAction
set str to str & "<table border=\"0\" cellpadding=\"2\">"
set str to str & "<tr><td WIDTH=100 align=\"right\" style=\"vertical-align:top\"><span class=\"TaskLabel\">" & ¬
pTaskLabel & "</span></td><td WIDTH=500><span class=\"Task\">" & strName & "</span></td></tr>"
if dteDue is not missing value then set str to ¬
(str & "<tr><td align=\"right\" style=\"vertical-align:top\"><span class=\"DueLabel\">" & pDueLabel & ¬
"</span></td><td><span class=\"Due\">" & dteDue as string) & "</span></td></tr>"
if oProject is not missing value then set str to ¬
str & "<tr><td align=\"right\" style=\"vertical-align:top\"><span class=\"ProjectLabel\">" & ¬
pProjLabel & "</span></td><td><span class=\"Project\">" & name of oProject & "</span></td></tr>"
if oContext is not missing value then
set str to ¬
str & "<tr><td align=\"right\" style=\"vertical-align:top\"><span class=\"ContextLabel\">" & pContextLabel & ¬
"</span></td><td><span class=\"Context\">" & name of oContext & "</span></td></tr>"
else
set lstContexts to name of (flattened contexts of oDoc)
tell application id "com.apple.systemevents" -- (needed for interactivity when launched by osascript, Keyboard Maestro etc)
activate
set varChoice to choose from list lstContexts with prompt ¬
"Assign a context to:" & return & return & strName ¬
default items {first item of lstContexts} ¬
cancel button name "No context needed" with title pTitle
end tell
if varChoice is not false then
set strContext to first item of varChoice
set context of oAction to (first flattened context of oDoc where name = strContext)
set str to str & "<tr><td align=\"right\" style=\"vertical-align:top\"><span class=\"ContextLabel\">" & pContextLabel & ¬
"</span></td><td><span class=\"Context\">" & strContext & "</span></td></tr>"
end if
end if
if pblnIncludeNotes then if length of strNote > 0 then ¬
set str to str & "<tr><td align=\"right\" style=\"vertical-align:top\"><span class=\"NoteLabel\">" & ¬
pNoteLabel & "</span></td><td><span class=\"Note\">" & strNote
set str to str & "</td></tr></table>"
set str to str & "<p><p>"

end repeat
end tell
end tell

set strPara to "<p>" & str & "</p>"

set strHTML to "
<html><head><style type=\"text/css\">
" & pstrCSS & "
</style></head><body>" & strPara & "</body></html>"

if length of str > 0 then
do shell script "echo " & quoted form of strHTML & " | textutil -format html -convert rtf -stdin -stdout | pbcopy -Prefer rtf"
set str to do shell script "echo " & quoted form of strHTML & " | textutil -format html -convert txt -stdin -stdout"
my Growl(lngActions)
end if


-- Replace a sub string
on replaceString(theText, oldString, newString)
set AppleScript's text item delimiters to oldString
set tempList to every text item of theText
set AppleScript's text item delimiters to newString
set theText to the tempList as string
set AppleScript's text item delimiters to ""
return theText
end replaceString

on Growl(lngActions)
-- If Growl is running, report the references sent

if lngActions > 0 then
if lngActions = 1 then
set strRefInflection to " action"
else
set strRefInflection to " actions"
end if
set strReport to "Formatted versions of " & (lngActions as string) & strRefInflection & " in RTF clipboard."
tell application "System Events"
set blnGrowlRunning to (count of (every process whose name is "GrowlHelperApp")) > 0
if blnGrowlRunning then
-- set strReport to text 1 thru ((length of strReport) - 2) of strReport

tell application "GrowlHelperApp"
register as application "RobTrew scripts" all notifications {"Actions handled"} default notifications {"Actions handled"} icon of application "OmniFocus"
notify with name "Actions handled" title "Actions processed" application name "RobTrew scripts" description strReport
end tell
else
activate
display dialog strReport buttons {"OK"} default button "OK" with title pTitle
end if
end tell
end if
end Growl
[/CODE]

JohnJ80 2010-12-10 06:39 AM

Looks good. I'll give it a try. Thanks for the mods.

J.

JohnJ80 2010-12-10 06:45 AM

I like it! Nicely done.

You wouldn't know how to set the style for the text in mail.app through applescript, would you?

J

RobTrew 2010-12-10 07:17 AM

[QUOTE=JohnJ80;90247]set the style for the text in mail.app through applescript[/QUOTE]

What in particular would you like to do ?

JohnJ80 2010-12-10 07:19 AM

I believe that Mail has a build in style called "Normal Email". I'd like to be able to put the text in that style into the email if possible.

j.

RobTrew 2010-12-10 07:50 AM

I may well be missing something - haven't tried to drive Mail formatting before - but a quick glance at the Mail applescript library doesn't immediately reveal a way of setting styles through formatting.

RobTrew 2010-12-10 08:38 AM

This, however, is a version which places formatted RTF rather than plain text in the clipboard.

You can edit the CSS lines near the beginning to alter the details of point size, font, and color.

[CODE]property pTitle : "Selected actions to RTF clipboard"
property pVer : "0.6"

-- Uses table format to align labels and allow wrapping of note.

property pblnIncludeNotes : true

property pTaskLabel : "Action Item:"
property pDueLabel : "Due by:"
property pProjLabel : "Project:"
property pContextLabel : "Who:" -- or "Context: " edit as required
property pNoteLabel : "Note:"

-- edit font, point size, color, margins etc as required:
property pstrCSS : "
.TaskLabel {color:#617794;}
.DueLabel {color:#617794;}
.ProjectLabel {color:#617794;}
.ContextLabel {color:#617794;}
.NoteLabel {color:#617794; }

.Task {color:#3c6ba9; font-weight: bold;}
.Due {color:#3c6ba9;}
.Project {color:#3c6ba9;}
.Context {color:#ff0000;}
.Note {color: #666666}

td {font: 14.0px 'Lucida Grande';}
"

tell application id "com.omnigroup.omnifocus"
set oDoc to front document
tell content of front document window of oDoc
set lstActions to value of (selected trees where class of value is task or class of value is inbox task)
set lngActions to count of lstActions
if lngActions < 1 then
tell application id "com.apple.systemevents"
activate
display dialog "Select one or more (non-inbox) actions and run this script" buttons {"OK"} default button "OK" with title pTitle
end tell
return
end if
set str to ""
repeat with oAction in lstActions
set {strName, dteDue, oProject, oContext, strNote} to {name, due date, containing project, its context, note} of oAction
set str to str & "<table border=\"0\" cellpadding=\"2\">"
set str to str & "<tr><td WIDTH=100 align=\"right\" style=\"vertical-align:top\"><span class=\"TaskLabel\">" & ¬
pTaskLabel & "</span></td><td WIDTH=500><span class=\"Task\">" & strName & "</span></td></tr>"
if dteDue is not missing value then set str to ¬
(str & "<tr><td align=\"right\" style=\"vertical-align:top\"><span class=\"DueLabel\">" & pDueLabel & ¬
"</span></td><td><span class=\"Due\">" & dteDue as string) & "</span></td></tr>"
if oProject is not missing value then set str to ¬
str & "<tr><td align=\"right\" style=\"vertical-align:top\"><span class=\"ProjectLabel\">" & ¬
pProjLabel & "</span></td><td><span class=\"Project\">" & name of oProject & "</span></td></tr>"
if oContext is not missing value then
set str to ¬
str & "<tr><td align=\"right\" style=\"vertical-align:top\"><span class=\"ContextLabel\">" & pContextLabel & ¬
"</span></td><td><span class=\"Context\">" & name of oContext & "</span></td></tr>"
else
set lstContexts to name of (flattened contexts of oDoc)
tell application id "com.apple.systemevents" -- (needed for interactivity when launched by osascript, Keyboard Maestro etc)
activate
set varChoice to choose from list lstContexts with prompt ¬
"Assign a context to:" & return & return & strName ¬
default items {first item of lstContexts} ¬
cancel button name "No context needed" with title pTitle
end tell
if varChoice is not false then
set strContext to first item of varChoice
set context of oAction to (first flattened context of oDoc where name = strContext)
set str to str & "<tr><td align=\"right\" style=\"vertical-align:top\"><span class=\"ContextLabel\">" & pContextLabel & ¬
"</span></td><td><span class=\"Context\">" & strContext & "</span></td></tr>"
end if
end if
if pblnIncludeNotes then if length of strNote > 0 then ¬
set str to str & "<tr><td align=\"right\" style=\"vertical-align:top\"><span class=\"NoteLabel\">" & ¬
pNoteLabel & "</span></td><td><span class=\"Note\">" & strNote
set str to str & "</td></tr></table>"
set str to str & "<p><p>"

end repeat
end tell
end tell

set strPara to "<p>" & str & "</p>"

set strHTML to "
<html><head><style type=\"text/css\">
" & pstrCSS & "
</style></head><body>" & strPara & "</body></html>"

if length of str > 0 then
do shell script "echo " & quoted form of strHTML & " | textutil -format html -convert rtf -stdin -stdout | pbcopy -Prefer rtf"
set str to do shell script "echo " & quoted form of strHTML & " | textutil -format html -convert txt -stdin -stdout"
my Growl(lngActions)
end if


-- Replace a sub string
on replaceString(theText, oldString, newString)
set AppleScript's text item delimiters to oldString
set tempList to every text item of theText
set AppleScript's text item delimiters to newString
set theText to the tempList as string
set AppleScript's text item delimiters to ""
return theText
end replaceString

on Growl(lngActions)
-- If Growl is running, report the references sent

if lngActions > 0 then
if lngActions = 1 then
set strRefInflection to " action"
else
set strRefInflection to " actions"
end if
set strReport to "Formatted versions of " & (lngActions as string) & strRefInflection & " in RTF clipboard."
tell application "System Events"
set blnGrowlRunning to (count of (every process whose name is "GrowlHelperApp")) > 0
if blnGrowlRunning then
-- set strReport to text 1 thru ((length of strReport) - 2) of strReport

tell application "GrowlHelperApp"
register as application "RobTrew scripts" all notifications {"Actions handled"} default notifications {"Actions handled"} icon of application "OmniFocus"
notify with name "Actions handled" title "Actions processed" application name "RobTrew scripts" description strReport
end tell
else
activate
display dialog strReport buttons {"OK"} default button "OK" with title pTitle
end if
end tell
end if
end Growl
[/CODE]

JohnJ80 2010-12-10 09:47 AM

I think it's an Apple bug.

If i use the font selection stuff, it comes out with a small font in Times regardless of the font name or size specified. If I go back to the previous version with no effort at formatting, it comes out with a different font (small version of lucida grande).

but thanks for figuring that out.

J.

RobTrew 2010-12-10 10:36 AM

[QUOTE=JohnJ80;90255]I think it's an Apple bug.[/QUOTE]

Turns out I had pasted a comment into the CSS string at the last moment.

Amended above, and should work now.

JohnJ80 2010-12-10 02:03 PM

[QUOTE=RobTrew;90258]Turns out I had pasted a comment into the CSS string at the last moment.

Amended above, and should work now.[/QUOTE]

Yep. Works great - thanks!

J.

policarpo 2010-12-11 08:27 PM

How can you get this to work so it includes the Notes assigned to a Task?

RobTrew 2010-12-12 01:45 AM

[QUOTE=policarpo;90321]How can you get this to work so it includes the Notes assigned to a Task?[/QUOTE]

I have amended the code in [URL="http://forums.omnigroup.com/showpost.php?p=90254&postcount=11"]post 11[/URL] above to include line 3:
[CODE]property pblnIncludeNotes : false[/CODE]

if you edit 'false' to 'true' any notes attached to the selected items will also be placed in the clipboard.

(I have also updated the code to allow more flexible formatting - you can edit the CSS to get the font, color, size and margins you want for each label and data component).

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

JohnJ80 2010-12-12 06:28 PM

[QUOTE=RobTrew;90323]I have amended the code in [URL="http://forums.omnigroup.com/showpost.php?p=90254&postcount=11"]post 11[/URL] above to include line 3:
[CODE]property pblnIncludeNotes : false[/CODE]

if you edit 'false' to 'true' any notes attached to the selected items will also be placed in the clipboard.

(I have also updated the code to allow more flexible formatting - you can edit the CSS to get the font, color, size and margins you want for each label and data component).

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

Very nice, Rob. thanks for doing this. EXACTLY what I was looking for. Great feature added through applescript. Really, this would be very nice if added to OF in a future revision.

J.

marco.onderstal 2011-03-25 03:01 AM

Thank very much
 
This was exact the thing I was looking for

Regards

RobTrew 2011-03-25 05:59 AM

[URL="http://forums.omnigroup.com/showpost.php?p=90254&postcount=11"]Ver 0.6[/URL] in post 11 above contains some improvements in dialog behaviour - (focus, default button) mainly noticeable when using the script from LaunchBar, Keyboard Maestro etc.

Also adds use of [URL="http://growl.info/"]Growl[/URL], if installed, to report success.

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

JohnJ80 2011-03-25 06:57 AM

[QUOTE=RobTrew;95159][URL="http://forums.omnigroup.com/showpost.php?p=90254&postcount=11"]Ver 0.6[/URL] in post 11 above contains some improvements in dialog behaviour - (focus, default button) mainly noticeable when using the script from LaunchBar, Keyboard Maestro etc.

Also adds use of [URL="http://growl.info/"]Growl[/URL], if installed, to report success.

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

Any way to preserve the formatting in the notes? I.e. returns, links, etc...

J.

RobTrew 2011-03-25 10:19 AM

[QUOTE=JohnJ80;95160]Any way to preserve the formatting in the notes? I.e. returns, links, etc...[/QUOTE]

All things are possible, but I don't think this one is NPV-positive.


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

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