PDA

View Full Version : what is the fastest way to search tasks based on custom data in AppleScript


cope360
2009-07-10, 11:54 AM
For some of my tasks I have a custom data field "tracid" set. In AppleScript I need to find the task where the tracid is set to a particular value.

Currently I am just iterating over the list of tasks until I find the one I want. Is there any faster way to do this?

-- Finds a Task
-- _id : tracid of task
on findTask(_id)
tell application "OmniPlan"
tell first document

repeat with t in tasks
set customData to get custom data of t
if customData is not {} then
if (tracid of customData = _id) then
return t
end if
end if
end repeat

return false

end tell
end tell
end findTask

Lizard
2009-07-16, 10:15 AM
-- Finds a Task
-- _id : tracid of task
on findTask(_id)
tell application "OmniPlan"
tell first document
set t to first task whose value of custom data entry "tracid" is _id
if exists t then
return t
else
return false
end if
end tell
end tell
end findTask

cope360
2009-07-16, 02:57 PM
Thanks for the help Lizard.

I tried your code and it causes an error when the task is not found instead of returning false.

I think this is the ticket:

-- Finds a Task
-- _id : tracid of task
on findTask(_id)
tell application "OmniPlan"
tell first document
try
set t to first task whose value of custom data entry "tracid" is _id
on error errmesg number errn
if errn is -1719 then
return false
else
error errmesg number errn
end if
end try
return t
end tell
end tell
end findTask