Skip to content

Commit

Permalink
Update Focus in new Tab with helper script + application bundle.
Browse files Browse the repository at this point in the history
Fixes an issue when running with certain top-level category separators selected
Minor update to notification code
  • Loading branch information
dbyler committed Apr 23, 2017
1 parent 1b756c0 commit b77a6bb
Show file tree
Hide file tree
Showing 15 changed files with 651 additions and 102 deletions.
136 changes: 132 additions & 4 deletions Append Note to Newest Task.applescript
Expand Up @@ -10,20 +10,148 @@
# LICENSE #
Copyright © 2015 Dan Byler (contact: dbyler@gmail.com)
Copyright © 2015-2017 Dan Byler (contact: dbyler@gmail.com)
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 propertiesproperty showSummaryNotification : true --if true, will display success notifications-- Don't change theseproperty 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 & "
*)


-- 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

" 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 tellend mainon 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 tellend getLastAddedTask(* Begin notification code *)on notify(alertName, alertTitle, alertText) --Call this to show a normal notification my notifyMain(alertName, alertTitle, alertText, false)end notifyon notifyWithSticky(alertName, alertTitle, alertText) --Show a sticky Growl notification my notifyMain(alertName, alertTitle, alertText, true)end notifyWithStickyon IsGrowlRunning() tell application "System Events" to set GrowlRunning to (count of (every process where creator type is "GRRR")) > 0 return GrowlRunningend IsGrowlRunningon 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 tellend notifyWithGrowlon NotifyWithoutGrowl(alertText) display notification alertTextend NotifyWithoutGrowlon 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) end ifend notifyMain(* end notification code *)main(missing value)on alfred_script(q) main(q)end alfred_scripton handle_string(q) main(q)end handle_string
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
134 changes: 127 additions & 7 deletions Append Note to Selected Task.applescript
Expand Up @@ -3,28 +3,148 @@
Appends a note to the selected OmniFocus task(s).
- By default, the clipboard contents are used for the note
- If triggered from LaunchBar or Alfred, you can use different text
- If triggered from LaunchBar or Alfred, you can specify the text
See https://github.com/dbyler/omnifocus-scripts for updates
# LICENSE #
Copyright © 2015 Dan Byler (contact: dbyler@gmail.com)
Copyright © 2015-2017 Dan Byler (contact: dbyler@gmail.com)
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-23
- Fixes an issue when running with certain top-level category separators selected
- Minor update to notification code
1.0.1 (2015-05-17)
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.
2015-05-09
- Original release
*)-- To change settings, modify the following propertiesproperty showSummaryNotification : true --if true, will display success notifications-- Don't change theseproperty 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 content of first document window of front document --Get selection set validSelectedItemsList to value of (selected trees where class of its value is not item and class of its value is not folder) set totalItems to count of validSelectedItemsList if totalItems is 0 then set alertName to "Error" set alertTitle to "Error" set alertText to "No valid task(s) selected" my notify(alertName, alertTitle, alertText) return end if if totalItems > 1 then display dialog "Multiple items selected. Continue?" buttons {"Cancel", "OK"} default button 2 end if repeat with thisItem in validSelectedItemsList tell thisItem insert q & "
*)

" at before first paragraph of note end tell end repeat 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 if totalItems > 1 then set alertText to "Note appended to " & totalItems & " selected tasks" else set alertText to "Note appended to:
" & name of first item in validSelectedItemsList end if my notify(alertName, alertTitle, alertText) end if end tell end tellend main(* Begin notification code *)on notify(alertName, alertTitle, alertText) --Call this to show a normal notification my notifyMain(alertName, alertTitle, alertText, false)end notifyon notifyWithSticky(alertName, alertTitle, alertText) --Show a sticky Growl notification my notifyMain(alertName, alertTitle, alertText, true)end notifyWithStickyon IsGrowlRunning() tell application "System Events" to set GrowlRunning to (count of (every process where creator type is "GRRR")) > 0 return GrowlRunningend IsGrowlRunningon 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 tellend notifyWithGrowlon NotifyWithoutGrowl(alertText) display notification alertTextend NotifyWithoutGrowlon 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) end ifend notifyMain(* end notification code *)main(missing value)on alfred_script(q) main(q)end alfred_scripton handle_string(q) main(q)end handle_string
-- 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 content of first document window of front document
--Get selection
set validSelectedItemsList to value of (selected trees where class of its value is not item and class of its value is not folder and class of its value is not context and class of its value is not perspective)
set totalItems to count of validSelectedItemsList
if totalItems is 0 then
set alertName to "Error"
set alertTitle to "Error"
set alertText to "No valid task(s) selected"
my notify(alertName, alertTitle, alertText)
return
end if

if totalItems > 1 then
display dialog "Multiple items selected. Continue?" buttons {"Cancel", "OK"} default button 2
end if

repeat with thisItem in validSelectedItemsList
tell thisItem
insert q & "
" at before first paragraph of note
end tell
end repeat

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
if totalItems > 1 then
set alertText to "Note appended to " & totalItems & " selected tasks"
else
set alertText to "Note appended to:
" & name of first item in validSelectedItemsList
end if
my notify(alertName, alertTitle, alertText)
end if

end tell
end tell
end main


(* 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

0 comments on commit b77a6bb

Please sign in to comment.