Things · AppleScripts

Delegate Task(s)

    An AppleScript to quickly delegate the selected to dos to a team mate in Things.

Screenshot of Delegate Task(s)

Save the script to '~/Library/Scripts/Applications/Things/'. Use a third-party application like FastScripts to easily assign a keyboard shortcut to the script e.g. 'Cmd-Shift+D'.

Select a to do in Things and run the script. When the list shows up, you can easily select a teammate by starting to type his name.


show source AppleScript Editor open download
--   Creation date:    Freitag, 12. November 2010, 19:19:01
--   Created by:        ljr_nbg (http://applescript.bratis-lover.net)
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
--c-                                                                                                     SETTINGS
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --

property myName : "Delegate Task(s)"
property myVersion : "1.0"


--c-- strings
property noTasksStr : "No task(s) selected!"
property rmvDelegateStr : "--> Remove delegate"

--c-- items to remember
property lastDelegate : {}


-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
--c-                                                                                               DESCRIPTION
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --

(*
An AppleScript to quickly delegate the selected to dos to a team mate in Things.
Save the script to '~/Library/Scripts/Applications/Things'. Use a third-party
application like FastScripts to easily assign a keyboard shortcuts to the script
e.g. 'Cmd-Shift+D'.

Select a to do in Things and run the script. When the list shows up, you can
easily select a teammate by starting to type his name.

WWW: http://applescript.bratis-lover.net/applescripts/things/delegate-task-s/
*)

-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
--c-                                                                                                            MAIN
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --

try

        tell application "Things"

                --c-- get selected tasks
                set toDoLst to selected to dos
                set len to count toDoLst
                if len = 0 then
                        display dialog noTasksStr with title myName buttons ¬
                                {"OK"} default button 1 with icon 2
                        return
                end if

                --c-- get teammates
                set teamMates to people's name
                set teamMates to _list's sortList(teamMates)
                set beginning of teamMates to rmvDelegateStr

                --c-- prompt for delegate
                set pluralS to ""
                if len > 1 then set pluralS to "s"
                set thisDelegate to (choose from list (teamMates) with prompt ¬
                        "Please choose a teammate.\n" & len & " task" & pluralS & ¬
                        " selected" with title myName cancel button name ¬
                        "Exit" OK button name "Apply" default items lastDelegate)
                if thisDelegate = false then return
                set lastDelegate to thisDelegate
                set thisDelegate to thisDelegate as text
                if thisDelegate is rmvDelegateStr then
                        set thisDelegate to missing value
                else
                        set thisDelegate to person named thisDelegate
                end if

                --c-- delegate task(s)
                repeat with i from 1 to len
                        if thisDelegate is missing value then
                                delete delegate of (toDoLst's item i)
                        else
                                set (toDoLst's item i)'s delegate to thisDelegate
                        end if
                end repeat

        end tell



on error eMsg number eNum
        my errorHandler("Hmmm. Something went wrong...", eMsg, eNum)
end try



-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
--c-                                                                                                   HANDLERS
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --

on errorHandler(customMsg, eMsg, eNum)
        display dialog customMsg & "\r\rError: " & eNum default answer eMsg ¬
                buttons {"Cancel"} default button 1 cancel button 1 with icon 0 ¬
                with title myName giving up after 15
        error number -128
end errorHandler

script _list

        --c--   sortList(lst)
        --d--   Sort a list (Respects current considering/ignoring settings). Will raise error
        --d--   if list contains both text and numbers, as text and numerical values cannot
        --d--   be reliably compared against one another.
        --a--   lst : list -- list of simple, directly comparable values (integers/reals/string/date)
        --r--   list
        --x--   sortList({3, 1, 2}) --> {1, 2, 3}
        --u--   HAS (http://applemods.sourceforge.net/mods/Data/List.php)
        on sortList(theList)
                -- a stack-based, non-recursive quicksort
                local theList, s, l, a, b, c, j, r, v, i, tmp
                try
                        if theList's class is not list then error "not a list." number -1704
                        if (count theList each number) > 0 and ¬
                                ((count theList each string) > 0) then
                                error "can't sort a list containing " & ¬
                                        "both number and text values." number -1704
                        end if
                        script k -- list access speed kludge
                                property lst : theList's items
                        end script
                        if k's lst's length < 2 then return k's lst
                        set s to {a:1, b:count k's lst, c:missing value} -- unsorted slices stack
                        repeat until s is missing value
                                set l to s's a
                                set r to s's b
                                set s to get s's c
                                set i to l
                                set j to r
                                set v to k's lst's item ((l + r) div 2)
                                repeat while (j > i)
                                        repeat while (k's lst's item i < v)
                                                set i to i + 1
                                        end repeat
                                        repeat while (k's lst's item j > v)
                                                set j to j - 1
                                        end repeat
                                        if (ij) then
                                                set tmp to k's lst's item i
                                                set k's lst's item i to k's lst's item j
                                                set k's lst's item j to tmp
                                                set i to i + 1
                                                set j to j - 1
                                        end if
                                end repeat
                                if (l < j) then set s to {a:l, b:j, c:s}
                                if (r > i) then set s to {a:i, b:r, c:s}
                        end repeat
                        return k's lst
                on error eMsg number eNum
                        error "Can't sortList: " & eMsg number eNum
                end try
        end sortList

end script


-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
--c-                                                                            TERMS OF USE & CREDITS
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --

(*
This script was written by ljr_nbg (http://applescript.bratis-lover.net).

It uses handlers from the appleMods projekt:
(c) 2003 HAS (http://applemods.sourceforge.net)

It is released under the same terms as appleMods:

Permission is hereby granted, free of charge, to any person obtaining a copy of this
software and associated documentation files (the "Software"), to deal in the Software
without restriction, including without limitation the rights to use, copy, modify, merge,
publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies
or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NON INFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*)