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 Today's Posts

 
OFSelectAny(), select made easy Thread Tools Search this Thread Display Modes
Using the select command in applescript for OmniFocus 1.6 is not easy. For details, see:
http://forums.omnigroup.com/showthread.php?t=11610

OFSelectAny() makes selecting things easy, just pass it a task, project, list of items, IDs of items, etc.
See the next post for testing code which shows how to use OFSelectAny()

Code:
(* 
OFSelectAny(), code to select items in OmniFocus.
By David Amis, version 4/8/2009
	
Using OFSelectAny()
	Used to select just about anything in the front window.
	Currenty only tested in planning mode.
	Pass any of the following to OFSelectAny():	
		Sidebar items:
			sidebar
			inbox
			library
			quick entry tree (not tested)
			section (not tested)
			folder
			project
			
		Content items:
			task
			inbox task
			group
			available task (not tested)
			remaining task (not tested)
		
		by ID, i.e. OFelectAny("pbV93_WpDMx"):
			project ID (text)
			task ID (text)
			
		List of any of the above
		Nothing: OFSelectAny({})

		Note: Selects parent item in sidebar for a given content item.
*)
on OFSelectAny(selectItems)
	-- checks to see if we are processing a list, iterates if necessary
	local selectItem, extendSelection
	
	if class of selectItems is list then
		if (count of selectItems) is 0 then -- select nothing in the sidebar
			tell application "OmniFocus"
				tell default document
					tell front document window
						tell sidebar -- if Inbox, Library, Folder, Project, or SAL
							select {}
						end tell
					end tell
				end tell
			end tell
		end if
		set extendSelection to false
		repeat with selectItem in selectItems
			OFSelectOne(selectItem, extendSelection)
			set extendSelection to true
		end repeat
	else
		OFSelectOne(selectItems, false)
	end if
end OFSelectAny


on OFSelectOne(selectItem, extendSelection)
	local itemClass, textConvertFailed, containingProject
	
	tell application "OmniFocus"
		tell default document
			set itemClass to class of selectItem
			if itemClass is rich text then
				set textConvertFailed to true
				try
					set selectItem to task id selectItem
					set textConvertFailed to false
				end try
				if textConvertFailed then
					try
						set selectItem to project id selectItem
						set textConvertFailed to false
					end try
				end if
				if textConvertFailed then
					error "OFSelectOne:Couldn't find item ID:" & selectItem
				end if
				set itemClass to class of selectItem
			end if
			
			if itemClass is task then
				set containingProject to containing project of selectItem
				tell front document window
					tell sidebar
						select {containingProject} extending extendSelection
					end tell
					tell content
						select {selectItem} extending extendSelection
						set selected to true -- bug in OmniFocus 1.6: doesn't work
					end tell
				end tell
			else if itemClass is inbox task then
				tell front document window
					tell sidebar
						select inbox extending extendSelection
					end tell
					tell content
						select {selectItem} extending extendSelection
						set selected to true -- bug in OmniFocus 1.6: doesn't work
					end tell
				end tell
			else --Not needed: if (itemClass is project) or (itemClass is tree) or (itemClass is sidebar tree) then
				tell front document window
					tell sidebar
						select {selectItem} extending extendSelection
					end tell
				end tell
			end if
		end tell
	end tell
end OFSelectOne
 
Testing code for OFSelectAny()
version: April 8 2009 by David Amis
These tests were written for my OmniFocus document, you will need to modify the code to fit your document for some of the tests.

Code:
OFSelectTestUI()

on OFSelectTestUI() --Let the user choose a test from a list.
	local tests, testType
	
	set tests to ¬
		{"Task", "Task2", "Task by id", "Inbox task", "Group", "Project", "Project2", "Project by id", "Folder", "Inbox", "Library", "Sidebar", "list", "nothing in sidebar"}
	set testType to choose from list tests ¬
		with title "OFSelectAny tester" with prompt "Choose a type to select:" OK button name "Select" without empty selection allowed
	if class of testType is list then
		OFSelectTest(item 1 of testType)
	end if
end OFSelectTestUI


on OFSelectTest(testType) -- testing code for OFSelectAny()
	local aProject, aTask, someTasks, aFolder, taskID, projectID, aList
	
	-- document should contain at least one folder or project in the Library
	-- following test code is dependant on your OF document	
	
	if testType is "Task" then -- assumes there is a task in a root project, i.e. a misc. SAL
		tell application "OmniFocus"
			tell default document
				set aProject to first project whose status is active
				set aTask to task 1 of aProject whose completed is false
			end tell
		end tell
		OFSelectAny(aTask)
	end if
	if testType is "Task2" then
		tell application "OmniFocus"
			tell default document
				set aFolder to first folder
				set aProject to first project in aFolder whose status is active
				set aTask to task 2 of aProject whose completed is false
			end tell
		end tell
		OFSelectAny(aTask)
	end if
	if testType is "Task by id" then -- assumes there is a task in a root project, i.e. a misc. SAL
		tell application "OmniFocus"
			tell default document
				set aProject to first project whose status is active
				set aTask to task 1 of aProject whose completed is false
				set taskID to id of aTask
			end tell
		end tell
		OFSelectAny(taskID)
	end if
	if testType is "Inbox task" then
		tell application "OmniFocus"
			tell default document
				set someTasks to inbox tasks whose completed is false
				set aTask to item 1 of someTasks
			end tell
		end tell
		OFSelectAny(aTask)
	end if
	if testType is "Group" then
		tell application "OmniFocus"
			tell default document
				set aFolder to second folder
				set aProject to first project in aFolder whose status is active
				set aTask to task 6 of aProject whose completed is false -- really big assumption here
			end tell
		end tell
		OFSelectAny(aTask)
	end if
	if testType is "Project" then
		tell application "OmniFocus"
			tell default document
				set aFolder to first folder
				set aProject to first project in aFolder whose status is active
			end tell
		end tell
		OFSelectAny(aProject)
	end if
	if testType is "Project2" then
		tell application "OmniFocus"
			tell default document
				set aFolder to second folder
				set aProject to second project in aFolder whose status is active
			end tell
		end tell
		OFSelectAny(aProject)
	end if
	if testType is "Project by id" then
		tell application "OmniFocus"
			tell default document
				set aFolder to first folder
				set aProject to first project in aFolder whose status is active
				set projectID to id of aProject
			end tell
		end tell
		OFSelectAny(projectID)
	end if
	if testType is "Folder" then
		tell application "OmniFocus"
			tell default document
				set aFolder to second folder
			end tell
		end tell
		OFSelectAny(aFolder)
	end if
	if testType is "Inbox" then
		tell application "OmniFocus"
			tell default document
				tell sidebar of document window 1
					set aProject to inbox
				end tell
			end tell
		end tell
		OFSelectAny(aProject)
	end if
	if testType is "Library" then
		tell application "OmniFocus"
			tell default document
				tell sidebar of document window 1
					set aProject to library
				end tell
			end tell
		end tell
		OFSelectAny(aProject)
	end if
	if testType is "Sidebar" then
		tell application "OmniFocus"
			tell default document
				tell document window 1
					set aProject to sidebar
				end tell
			end tell
		end tell
		OFSelectAny(aProject)
	end if
	if testType is "list" then
		tell application "OmniFocus"
			tell default document
				set aFolder to second folder
				set aProject to second project in aFolder whose status is active
				set someTasks to tasks of aProject whose completed is false
				set aList to {aProject}
				set aList to aList & someTasks
			end tell
		end tell
		OFSelectAny(aList)
	end if
	if testType is "nothing in sidebar" then
		OFSelectAny({})
	end if
end OFSelectTest
 
 




Similar Threads
Thread Thread Starter Forum Replies Last Post
Tags are possible in OF, and quite easy gandalf44 OmniFocus 1 for Mac 5 2013-04-14 06:46 AM
Who is omniplan made for??? PhiDelt OmniPlan General 3 2012-11-18 09:54 AM
Hopefully an Easy Question Amovida OmniOutliner 3 for Mac 3 2011-03-22 08:13 PM
This script should be *so* easy … Zoolok OmniFocus Extras 2 2009-10-10 07:35 AM


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


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