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 > OmniFocus > OmniFocus Extras
FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
setting project + context via script (modifying a DEVONthink script) Thread Tools Search this Thread Display Modes
Using DEVONthink Pro Office I am creating lots of tasks in OmniFocus via a script, that comes with DEVONthink Pro Office. The script is called “Add as To Do to OmniFocus”. This script creates the tasks in OmniFocus' inbox.

I am looking for help on modifying this script, so that a dedicated project (“project-A”) and context (“context-A”) (both already existing in OmniFocus) are already set via the script.

I don't need to choose from a list of projects/contexts, I would save different versions of the script each version meeting the project and context I want the task filed to. These scripts will be started via LaunchBar (not from DEVONthink Pro Office's script menue).

The complete code of the script I want to modify is:
Code:
-- Script to add a selected record to OmniFocus as a to do
-- Written by Eric Böhnisch-Volkmann, Version 1.0.2, Aug 10, 2010
-- © 2010 DEVONtechnologies, LLC


-- Set properties
property pDaysIntoFuture : -1 -- Created to do will have a due date n days in the future
property pPrefix : "Reminder" -- Prefix for the created to do item
property pDelays : {{displayname:"No due date", value:-1}, {displayname:"Tomorrow", value:1 * days}, {displayname:"In two days", value:2 * days}, {displayname:"In three days", value:3 * days}, {displayname:"In one week", value:1 * weeks}, {displayname:"In two weeks", value:2 * weeks}, {displayname:"In one month", value:4 * weeks}, {displayname:"In two months", value:8 * weeks}, {displayname:"In three months", value:90 * days}, {displayname:"In six months", value:180 * days}, {displayname:"In one year", value:365 * days}}
property pDefaultDelay : "In one week"

-- Import helper library
tell application "Finder" to set pathToAdditions to ((path to application id "com.devon-technologies.thinkpro2" as string) & "Contents:Resources:Template Script Additions.scpt") as alias
set helperLibrary to load script pathToAdditions

try
   -- Get the selection
   tell application id "com.devon-technologies.thinkpro2" to set thisSelection to the selection
   
   -- Error handling
   if thisSelection is {} then error localized string "Please select a document or group, then try again."
   if (length of thisSelection) > 1 then error localized string "Please select only one document or group, then try again."
   
   -- Get and format the data we need
   set pLocalizedPrefix to localized string pPrefix
   tell application id "com.devon-technologies.thinkpro2"
      set thisItem to first item of thisSelection
      set theSummary to (pLocalizedPrefix & ": " & name of thisItem) as string
      set theURL to (reference URL of thisItem) as string
   end tell
   
   -- Let the user choose when to receive the reminder
   -- Convert array into localized arrays
   set pLocalizedDelays to {}
   set pLocalizedDelayNames to {}
   repeat with theDelay in pDelays
      set pLocalizedDelays to pLocalizedDelays & {{displayname:localized string (displayname of theDelay), value:(value of theDelay)}}
      set pLocalizedDelayNames to pLocalizedDelayNames & {localized string (displayname of theDelay)}
   end repeat
   set theChoice to choose from list pLocalizedDelayNames with title (localized string "Set reminder") with prompt (localized string "Please choose when you want to get reminded of the item") & " \"" & theSummary & "\"" & (localized string "%choice prompt end%") & ":" default items {pDefaultDelay}
   if theChoice is false then return false -- If the user pressed Cancel, exit
   set theDelayValue to pDaysIntoFuture -- Assume default
   try
      -- Find the number of days associated with the user's choice
      repeat with theDelay in pLocalizedDelays
         if ((displayname of theDelay) as string) is equal to (theChoice as string) then set theDelayValue to (value of theDelay)
      end repeat
   end try
   
   -- Calculate due date
   if theDelayValue ≥ 0 then set theDueDate to (date (date string of (current date))) + theDelayValue
   
   -- Add new to do to OmniFocus
   try
      tell application "OmniFocus"
         if theDelayValue ≥ 0 then
            tell default document to set newTask to make new inbox task with properties {name:theSummary, due date:theDueDate, note:theURL}
         else
            tell default document to set newTask to make new inbox task with properties {name:theSummary, note:theURL}
         end if
      end tell
   on error errmsg
      display alert (localized string "OmniFocus is not available.")
   end try
   
on error errmsg
   
   display alert (localized string "Error when adding item to OmniFocus") message errmsg
   
end try
AppleScript is beyond my horizion. I have tried to find a solution by picking parts from other scripts I found on the web (MacSparky, a script that moves task from Things toOmniFocus etc.) and from different threads on this board, but my experiments were rather like playing lottery, needless to mention, I did not win.
Now I'm hoping that someone kind will help me here.

Kind regards,
Bernd

PS: I don't know if it's relevant: The contexts I want to integrate in different versions of this script are no sub-contexts, although that may change some time later.
 
To get references to existing projects and contexts, you need to define a couple of functions which can be called like this:

Code:
tell application id "OFOC"
	set oDoc to default document
	set oProj to GetProject(oDoc, "project-A")
	set oContext to GetContext(oDoc, "context-A")
end tell

on GetContext(oDoc, strName)
	tell application id "OFOC"
		tell oDoc
			set lstContexts to flattened contexts where name = strName
			if length of lstContexts < 1 then return missing value
			return item 1 of lstContexts
		end tell
	end tell
end GetContext

on GetProject(oDoc, strName)
	tell application id "OFOC"
		tell oDoc
			set lstProjects to flattened projects where name = strName
			if length of lstProjects < 1 then return missing value
			return item 1 of lstProjects
		end tell
	end tell
end GetProject
Then using DevonThink's supplied code, you should be able to make an edit like the following:

Code:
-- Script to add a selected record to OmniFocus as a to do
-- Written by Eric Böhnisch-Volkmann, Version 1.0.2, Aug 10, 2010
-- © 2010 DEVONtechnologies, LLC


-- Set properties
property pDaysIntoFuture : -1 -- Created to do will have a due date n days in the future
property pPrefix : "Reminder" -- Prefix for the created to do item
property pDelays : {{displayname:"No due date", value:-1}, {displayname:"Tomorrow", value:1 * days}, {displayname:"In two days", value:2 * days}, {displayname:"In three days", value:3 * days}, {displayname:"In one week", value:1 * weeks}, {displayname:"In two weeks", value:2 * weeks}, {displayname:"In one month", value:4 * weeks}, {displayname:"In two months", value:8 * weeks}, {displayname:"In three months", value:90 * days}, {displayname:"In six months", value:180 * days}, {displayname:"In one year", value:365 * days}}
property pDefaultDelay : "In one week"

on GetContext(oDoc, strName)
	tell application id "OFOC"
		tell oDoc
			set lstContexts to flattened contexts where name = strName
			if length of lstContexts < 1 then return missing value
			return item 1 of lstContexts
		end tell
	end tell
end GetContext

on GetProject(oDoc, strName)
	tell application id "OFOC"
		tell oDoc
			set lstProjects to flattened projects where name = strName
			if length of lstProjects < 1 then return missing value
			return item 1 of lstProjects
		end tell
	end tell
end GetProject

on run
	-- Import helper library
	tell application "Finder" to set pathToAdditions to ((path to application id "com.devon-technologies.thinkpro2" as string) & "Contents:Resources:Template Script Additions.scpt") as alias
	set helperLibrary to load script pathToAdditions
	
	try
		-- Get the selection
		tell application id "com.devon-technologies.thinkpro2" to set thisSelection to the selection
		
		-- Error handling
		if thisSelection is {} then error localized string "Please select a document or group, then try again."
		if (length of thisSelection) > 1 then error localized string "Please select only one document or group, then try again."
		
		-- Get and format the data we need
		set pLocalizedPrefix to localized string pPrefix
		tell application id "com.devon-technologies.thinkpro2"
			set thisItem to first item of thisSelection
			set theSummary to (pLocalizedPrefix & ": " & name of thisItem) as string
			set theURL to (reference URL of thisItem) as string
		end tell
		
		-- Let the user choose when to receive the reminder
		-- Convert array into localized arrays
		set pLocalizedDelays to {}
		set pLocalizedDelayNames to {}
		repeat with theDelay in pDelays
			set pLocalizedDelays to pLocalizedDelays & {{displayname:localized string (displayname of theDelay), value:(value of theDelay)}}
			set pLocalizedDelayNames to pLocalizedDelayNames & {localized string (displayname of theDelay)}
		end repeat
		set theChoice to choose from list pLocalizedDelayNames with title (localized string "Set reminder") with prompt (localized string "Please choose when you want to get reminded of the item") & " \"" & theSummary & "\"" & (localized string "%choice prompt end%") & ":" default items {pDefaultDelay}
		if theChoice is false then return false -- If the user pressed Cancel, exit
		set theDelayValue to pDaysIntoFuture -- Assume default
		try
			-- Find the number of days associated with the user's choice
			repeat with theDelay in pLocalizedDelays
				if ((displayname of theDelay) as string) is equal to (theChoice as string) then set theDelayValue to (value of theDelay)
			end repeat
		end try
		
		-- Calculate due date
		if theDelayValue ≥ 0 then set theDueDate to (date (date string of (current date))) + theDelayValue
		
		-- Add new to do to named project
		try
			tell application "OmniFocus"
				set oDoc to default document
				
				set oProj to my GetProject(oDoc, "project-A")
				set oContext to my GetContext(oDoc, "context-A")
				set recProjContext to {project:oProj, context:oContext} as record
				
				tell oProj
					if theDelayValue ≥ 0 then
						set newTask to make new task at end of tasks with properties {name:theSummary, due date:theDueDate, note:theURL}
					else
						set newTask to make new task at end of tasks with properties {name:theSummary, note:theURL}
					end if
				end tell
				set context of newTask to oContext
				
			end tell
		on error errmsg
			display alert (localized string "OmniFocus is not available.")
		end try
		
	on error errmsg
		
		display alert (localized string "Error when adding item to OmniFocus") message errmsg
		
	end try
end run
 
Quote:
Originally Posted by RobTrew View Post
Then using DevonThink's supplied code, you should be able to make an edit like the following:...
Rob, thank you so much for sharing your knowledge and time!

Your script works brilliant and it even finds sub-contexts as I found out. This means real huge ease for me as I don't use OmniFocus' mail clipping. Instead I import all my email to DEVONthink Pro Office and create the tasks from there - and this is just one example. Thanks to you, I'll have to work through OmniFocus' inbox much less from now on. Super!

Best wishes from Germany,
Bernd

Last edited by bernd; 2012-09-08 at 12:29 PM..
 
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Searching for script to convert project-based perspective to context one vitvit2002 OmniFocus Extras 0 2013-02-03 11:58 PM
Mass de-flag in context/perspective script. roadtrip OmniFocus Extras 5 2012-01-26 12:57 PM
Setting nested context from script donhoffman OmniFocus Extras 5 2011-09-16 01:48 PM
Job $$ : Apple Script to backup each context separately smartmobilesoftware AppleScripting Omni Apps 2 2010-04-04 05:50 AM
Context Search and Replace script davidamis OmniFocus Extras 2 2008-01-31 12:26 PM


All times are GMT -8. The time now is 11:34 AM.


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