forked from dbyler/omnifocus-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAppend Note to Newest Task.applescript
157 lines (130 loc) · 4.69 KB
/
Append Note to Newest Task.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
(*
# DESCRIPTION #
Appends a note to the most recently created task in OmniFocus.
- By default, the clipboard contents are used for the note
- If triggered from LaunchBar or Alfred, you can use different text
See https://github.com/dbyler/omnifocus-scripts for updates
# LICENSE #
Copyright © 2015-2017 Dan Byler (contact: [email protected])
Licensed under MIT License (http://www.opensource.org/licenses/mit-license.php)
(TL;DR: no warranty, do whatever you want with it.)
# CHANGE HISTORY #
2017-04-22
- Minor update to notification code
1.0.1 (2015-05-17)
- Fix for attachments being overwritten by the note
- Use Notification Center instead of an alert when not running Growl. Requires Mountain Lion or newer
1.0 (2015-05-09) Original release.
*)
-- To change settings, modify the following properties
property showSummaryNotification : true --if true, will display success notifications
-- Don't change these
property growlAppName : "Dan's Scripts"
property allNotifications : {"General", "Error"}
property enabledNotifications : {"General", "Error"}
property iconApplication : "OmniFocus.app"
on main(q)
if q is missing value then
set q to (the clipboard)
end if
tell application "OmniFocus"
tell front document
set myTask to my getLastAddedTask()
if myTask is false then
my notify("Error", "Error", "No recent items available")
return
end if
tell myTask
insert q & "
" at before first paragraph of note
end tell
if showSummaryNotification then
set alertName to "General"
set alertTitle to q
if length of alertTitle > 20 then
set alertTitle to (text 1 thru 20 of alertTitle) & "É"
end if
set alertText to "Note appended to:
" & name of myTask
my notify(alertName, alertTitle, alertText)
end if
end tell
end tell
end main
on getLastAddedTask()
tell application "OmniFocus"
tell front document
set allTasks to {}
set maxAge to 8
repeat while length of allTasks is 0 and maxAge ² 524288
set maxAge to maxAge * 2
set earliestTime to (current date) - maxAge * 60
set allTasks to (every flattened task whose (creation date is greater than earliestTime Â
and repetition is missing value))
end repeat
if length of allTasks > 0 then
set lastTask to first item of allTasks
set lastTaskDate to creation date of lastTask
repeat with i from 1 to length of allTasks
if creation date of (item i of allTasks) > lastTaskDate then
set lastTask to (item i of allTasks)
set lastTaskDate to creation date of lastTask
end if
end repeat
return lastTask
else
return false
end if
end tell
end tell
end getLastAddedTask
(* Begin notification code *)
on notify(alertName, alertTitle, alertText)
--Call this to show a normal notification
my notifyMain(alertName, alertTitle, alertText, false)
end notify
on notifyWithSticky(alertName, alertTitle, alertText)
--Show a sticky Growl notification
my notifyMain(alertName, alertTitle, alertText, true)
end notifyWithSticky
on IsGrowlRunning()
tell application "System Events" to set GrowlRunning to (count of (every process where creator type is "GRRR")) > 0
return GrowlRunning
end IsGrowlRunning
on notifyWithGrowl(growlHelperAppName, alertName, alertTitle, alertText, useSticky)
tell my application growlHelperAppName
Çevent registerÈ given Çclass applÈ:growlAppName, Çclass anotÈ:allNotifications, Çclass dnotÈ:enabledNotifications, Çclass iappÈ:iconApplication
Çevent notifygrÈ given Çclass nameÈ:alertName, Çclass titlÈ:alertTitle, Çclass applÈ:growlAppName, Çclass descÈ:alertText
end tell
end notifyWithGrowl
on NotifyWithoutGrowl(alertText, alertTitle)
display notification alertText with title alertTitle
end NotifyWithoutGrowl
on notifyMain(alertName, alertTitle, alertText, useSticky)
set GrowlRunning to my IsGrowlRunning() --check if Growl is running...
if not GrowlRunning then --if Growl isn't running...
set GrowlPath to "" --check to see if Growl is installed...
try
tell application "Finder" to tell (application file id "GRRR") to set strGrowlPath to POSIX path of (its container as alias) & name
end try
if GrowlPath is not "" then --...try to launch if so...
do shell script "open " & strGrowlPath & " > /dev/null 2>&1 &"
delay 0.5
set GrowlRunning to my IsGrowlRunning()
end if
end if
if GrowlRunning then
tell application "Finder" to tell (application file id "GRRR") to set growlHelperAppName to name
notifyWithGrowl(growlHelperAppName, alertName, alertTitle, alertText, useSticky)
else
NotifyWithoutGrowl(alertText, alertTitle)
end if
end notifyMain
(* end notification code *)
main(missing value)
on alfred_script(q)
main(q)
end alfred_script
on handle_string(q)
main(q)
end handle_string