Things · AppleScripts

Add Teammate

    The purpose of this script is to add a new teammate to Cultured Code's Things. The main advantage of this method is that the person does not need to exist in Address Book.

Screenshot of Add Teammate

You can use any name or string (see below for restriction) as input. A temporary person is created in Address Book, so Address Book will start when this script is run. The person is automatically deleted from Address Book afterwards. The script always creates a temporary person so an accidental deletion of an existing person is not possible.

Note: The name of the new teammate must consist of two individual, space separated strings in order for Things to find a match in address book.

Save the script to '~/Library/Scripts/Applications/Things/' and run it via the AppleScript Menu or use a third-party application like FastScripts to easily assign a keyboard shortcut to the script.


show source AppleScript Editor open download
--   Creation date:    Montag, 22. Februar 2010, 20:31:43
--   Created by:        ljr_nbg (http://applescript.bratis-lover.net)
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
--c-                                                                                                     SETTINGS
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --

property myName : "Things · Add Teammate"
property myVersion : "1.0"

--c-- strings
set strPrompt to "Name of new person:\n\ne.g.\n\tFirstName LastName\ne.g." & ¬
        "\n\t" & "@ WaitingFor\n\n(input must contain two space separated strings)"
set butLst to {"Cancel", "Create Person"}
set invalidInp to "Invalid input!"
set customErrMsg to "A problem occured while creating new person."


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

(*
The purpose of this script is to add a new teammate to Cultured Code's Things 
(http://culturedcode.com/things). The main advantage of this method is that the
person does not need to exist in Address Book.

You can use any name or string (see below for restriction) as input. A temporary 
person is created in Address Book, so Address Book will start when this script is 
run. The person is automatically deleted from Address Book afterwards. The 
script always creates a temporary person so an accidental deletion of an existing 
person is not possible.

e.g.
        Donald Duck
e.g.
        @ WaitingFor

NOTE: The name of the new teammate must consist of two individual, space 
separated strings in order for Things to find a match in address book.

WWW: http://applescript.bratis-lover.net/applescripts/things/add-teammate/
*)

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

global _string
set AbWasRunning to true --flag: address book running?
try
        --c-- prompt for name of dummy person
        set tmpPerson to ""
        repeat
                tell application "Things"
                        set tmpPerson to ¬
                                text returned of (display dialog strPrompt with title myName ¬
                                default answer tmpPerson with icon 1 buttons butLst ¬
                                cancel button 1 default button 2)
                end tell
                set tmpPerson to _string's trimBoth(tmpPerson)
                if tmpPerson ≠ "" and tmpPerson contains " " and ¬
                        tmpPersoninvalidInp then exit repeat
                set tmpPerson to invalidInp
        end repeat
        
        --c-- launch address book if necessary
        if not (application "Address Book" is running) then
                tell application "Address Book" to launch
                set AbWasRunning to false
        end if
        
        --c-- make first name, last name for address book
        set firstName to (_string's explode(" ", tmpPerson))'s item 1
        set cnt to count firstName
        set lastName to tmpPerson's text (cnt + 1) thru -1
        set lastName to _string's trimBoth(lastName)
        
        --c-- create dummy person in address book
        tell application "Address Book"
                set tmpPerson to make new person with properties ¬
                        {last name:lastName, first name:firstName}
                save
        end tell
        
        --c-- create new person in things
        tell application "Things"
                add teammate named firstName & " " & lastName
        end tell
        
        --c-- delete dummy person from address book
        tell application "Address Book"
                delete tmpPerson
                save
                if not AbWasRunning then quit
        end tell
        
        
on error eMsg number eNum
        if eNum ≠ -128 then
                try
                        --c-- delete dummy person on error
                        tell application "Address Book"
                                delete tmpPerson
                                save
                                if not AbWasRunning then quit
                        end tell
                end try
                my errorHandler(customErrMsg, eMsg, eNum)
        end if
end try



-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
--c-                                                                                                  FUNCTIONS
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --

on errorHandler(customMsg, eMsg, eNum)
        tell application "Things"
                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
        end tell
end errorHandler


script _string
        
        property _whiteSpace_ : {linefeed, return, space, tab}
        
        on trimStart(str)
                -- HAS (http://applemods.sourceforge.net/mods/Data/String.php)
                local str, whiteSpace
                try
                        set str to str as string
                        set whiteSpace to my _whiteSpace_
                        try
                                repeat while str's first character is in whiteSpace
                                        set str to str's text 2 thru -1
                                end repeat
                                return str
                        on error number -1728
                                return ""
                        end try
                on error eMsg number eNum
                        error "Can't trimStart: " & eMsg number eNum
                end try
        end trimStart
        
        on trimEnd(str)
                -- HAS (http://applemods.sourceforge.net/mods/Data/String.php)
                local str, whiteSpace
                try
                        set str to str as string
                        set whiteSpace to my _whiteSpace_
                        try
                                repeat while str's last character is in whiteSpace
                                        set str to str's text 1 thru -2
                                end repeat
                                return str
                        on error number -1728
                                return ""
                        end try
                on error eMsg number eNum
                        error "Can't trimEnd: " & eMsg number eNum
                end try
        end trimEnd
        
        on trimBoth(str)
                -- HAS (http://applemods.sourceforge.net/mods/Data/String.php)
                local str
                try
                        return my trimStart(my trimEnd(str))
                on error eMsg number eNum
                        error "Can't trimBoth: " & eMsg number eNum
                end try
        end trimBoth
        
        on explode(delimiter, input)
                -- ljr_nbg [modified from Applescript.net 
                -- (http://bbs.applescript.net/viewtopic.php?id=18377)]
                local delimiter, input, ASTID
                set ASTID to AppleScript's text item delimiters
                try
                        set AppleScript's text item delimiters to delimiter
                        set input to text items of input
                        set AppleScript's text item delimiters to ASTID
                        return input --> list
                on error eMsg number eNum
                        set AppleScript's text item delimiters to ASTID
                        error "Can't explode: " & eMsg number eNum
                end try
        end explode
        
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.
*)