The Omni Group Forums

The Omni Group Forums (http://forums.omnigroup.com/index.php)
-   OmniFocus Extras (http://forums.omnigroup.com/forumdisplay.php?f=44)
-   -   Find Omnifocus Task (http://forums.omnigroup.com/showthread.php?t=20311)

lexden 2011-03-02 04:22 AM

Find Omnifocus Task
 
Has anyone got a script that will search for a string in a task title and return that task's id? I'd be most grateful for a script, a link to somewhere with an example or a script extract.

John

RobTrew 2011-03-02 06:25 AM

[QUOTE=lexden;94314]search for a string in a task title and return that task's id[/QUOTE]


[CODE]property pTopic : "Get ID(s) of tasks matching string"

set str to text returned of (display dialog "Enter string:" default answer "" with title pTopic)

tell application id "com.omnigroup.OmniFocus"
tell default document
set lstID to id of flattened tasks where name contains str
end tell
end tell

set text item delimiters to return
display dialog "Tasks containing: " & str & "

" & lstID as string buttons {"OK"} with title pTopic
set text item delimiters to space
[/CODE]

lexden 2011-03-02 01:51 PM

Many thanks
 
Thanks for that, just what I needed. You're a star!

lexden 2011-03-02 02:06 PM

I'm going to push my luck now but how do I update due date for the found task given that I have the id?

John

RobTrew 2011-03-02 02:10 PM

[QUOTE=lexden;94334]Thanks for that, just what I needed[/QUOTE]

Good.

You could use [URL="http://bit.ly/OF-Find2"]Where in OF[/URL] to experiment with slightly more complex searches.

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

RobTrew 2011-03-02 02:23 PM

[CODE]property pTitle : "Set due date of task matching string"

set str to text returned of (display dialog "Enter string:" default answer "" with title pTitle)

tell application id "com.omnigroup.OmniFocus"
tell default document
set refTasks to a reference to (flattened tasks where name contains str)
if (count of refTasks) < 1 then return
set oTask to first item of refTasks

set dteToday to (current date)
set dteToday to dteToday - (time of dteToday)

set due date of oTask to dteToday + 3 * days
end tell

set dteDue to due date of oTask
end tell

display dialog (date string of dteDue & tab & time string of dteDue) buttons {"OK"} with title pTitle[/CODE]

RobTrew 2011-03-03 05:19 AM

[QUOTE=lexden;94335]how do I update due date for the found task given that I have the id ?[/QUOTE]

If you want to get the task by its id, the idiom is of the form:

[CODE]task id [I]strID[/I] of default document[/CODE]

Which you might broadly use like this:

[CODE]property pVer : "0.1"
property pTitle : "Choose and process " & pVer

property pstrDelim : tab
property pdteToday : (current date) - (time of (current date))
property plngDays : 3

set str to text returned of (display dialog "Enter string:" default answer "" with title pTitle)
tell application id "com.omnigroup.omnifocus"
tell default document
-- GET TWO PARALLEL PROPERTY LISTS FROM A REFERENCE
set {lstID, lstName} to {id, name} of (flattened tasks where name contains str)

-- PREFIX EACH NAME WITH A FIXED-WIDTH DIGIT STRING
-- (SO THAT WE CAN IDENTIFY THE USER CHOICES(s) BY NUMBER
set lng to length of lstID
if lng = 0 then return
set lngDigits to length of (lng as string)
repeat with i from 1 to lng
set item i of lstName to my PadNum(i, lngDigits) & pstrDelim & item i of lstName
end repeat

-- GET THE USER'S CHOICE(S)
set varChoice to choose from list lstName with title pTitle with prompt "Please make your selection(s)" with multiple selections allowed
if varChoice = false then return

set my text item delimiters to pstrDelim
repeat with oChoice in varChoice
-- GET THE CHOSEN ID FROM THE ID LIST,
-- USING THE NUMBER OF THE CHOICE
set strID to item ((first text item of oChoice) as integer) of lstID

-- USE THE ID TO GET A REFERENCE TO THE OBJECT
set oTask to task id strID

-- DO SOMETHING WITH THE OBJECT
my ProcessTask(oTask)
end repeat
set my text item delimiters to space
end tell
end tell


-- APPLY SOME PROCESSING TO THE TASK
-- E.G.
on ProcessTask(oTask)
tell application id "com.omnigroup.omnifocus"
set due date of oTask to pdteToday + plngDays * days
log due date of oTask
end tell
end ProcessTask

-- GET A DIGIT STRING OF MINIMUM WIDTH (LEFT-PADDING WITH ZEROS WHERE NEEDED)
on PadNum(lngNum, lngDigits)
set strNum to lngNum as string
set lngGap to (lngDigits - (length of strNum))
repeat while lngGap > 0
set strNum to "0" & strNum
set lngGap to lngGap - 1
end repeat
strNum
end PadNum[/CODE]

lexden 2011-03-03 12:01 PM

Again, many thanks. My script is now working perfectly.


All times are GMT -8. The time now is 12:57 AM.

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