-
Notifications
You must be signed in to change notification settings - Fork 0
/
Birthdays.applescript
124 lines (93 loc) · 3.15 KB
/
Birthdays.applescript
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
(*
==========
Name : Add Birthdays to OmniFocus
Description : Add a task to OmniFocus Inbox for contacts with close birthdays
Author : Robert van Bregt (https://robertvanbregt.nl/)
Known bugs and features:
- Task name and notes in Dutch
- Fixed defer time of 00:00 and due time of 20:00
- Tested with OF 3.6.4 / MacOS 10.15.4
2020-05-21
- Initial script.
==========
*)
-- days to look ahead
property AHEAD : 7
set cdt to (current date)
set cyr to year of (current date)
set s to date string of (current date)
-- display notification s
tell application "Contacts"
-- get all people with a birth date
set thePeople to every person whose birth date is not missing value
if (count of thePeople) = 0 then
display dialog "No people with birth date."
return
end if
repeat with thePerson in thePeople
set bdt to birth date of thePerson
set byr to year of bdt
-- get this year's birthday from birth date
set bdy to bdt
set year of bdy to cyr
if (bdy is greater than cdt) and (bdy is less than (cdt + AHEAD * days)) then
log "Matching birth day: " & short date string of bdy
if (byr = 1604) then -- unknown year
set age to "zoveelste"
else
set age to (cyr - byr) & "e"
end if
set task_name to "Feliciteren " & (name of thePerson) & " met " & age & " verjaardag"
-- DRY is impossible for phone and email
-- putting the redundant code in a function raises an error
set theList to (phone of thePerson)
set allItems to ""
repeat with theItem in theList
set theLabel to (label of theItem)
set theLabel to my removeGarbageFromLabel(theLabel)
set theValue to (value of theItem)
set allItems to allItems & theLabel & ": " & theValue & "
"
end repeat
set task_phone to allItems
set theList to (email of thePerson)
set allItems to ""
repeat with theItem in theList
set theLabel to (label of theItem)
set theLabel to my removeGarbageFromLabel(theLabel)
set theValue to (value of theItem)
set allItems to allItems & theLabel & ": " & theValue & "
"
end repeat
set task_email to allItems
set task_defer to bdy - 12 * hours
set task_due to bdy + 8 * hours
set task_note to "---
Van harte gefeliciteerd met je " & age & " verjaardag. Geniet van je dag.
---
" & task_phone & "
" & task_email & "
---"
tell application "OmniFocus"
tell default document
make new inbox task with properties Â
Â
{name:task_name, note:task_note, defer date:task_defer, due date:task_due} Â
end tell
end tell
end if
end repeat
end tell
on findAndReplaceInText(theText, theSearchString, theReplacementString)
set AppleScript's text item delimiters to theSearchString
set theTextItems to every text item of theText
set AppleScript's text item delimiters to theReplacementString
set theText to theTextItems as string
set AppleScript's text item delimiters to ""
return theText
end findAndReplaceInText
on removeGarbageFromLabel(theLabel)
set theLabel to my findAndReplaceInText(theLabel, "_$!<", "")
set theLabel to my findAndReplaceInText(theLabel, ">!$_", "")
return theLabel
end removeGarbageFromLabel