Advertisement
dbyler

Untitled

Sep 8th, 2011
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. (*
  3.     # DESCRIPTION #
  4.    
  5.     This script "snoozes" the currently selected actions or projects by setting the start date to
  6.     given number of days in the future.
  7.    
  8.    
  9.     # LICENSE #
  10.    
  11.     Copyright © 2010 Dan Byler (contact: dbyler@gmail.com)
  12.     Licensed under MIT License (http://www.opensource.org/licenses/mit-license.php)
  13.     (TL;DR: no warranty, do whatever you want with it.)
  14.    
  15.    
  16.     # CHANGE HISTORY #
  17.    
  18.     0.4 (2011-08-30)
  19.     -   Rewrote notification code to gracefully handle situations where Growl is not installed
  20.     -   Changed "The item/The items" to "It/They"
  21.    
  22.     0.3 (2011-07-07)
  23.     -   New option to set start time (default: 8am)
  24.     -   Reorganized; incorporated Rob Trew's method to get items from OmniFocus
  25.     -   No longer fails when a Grouping divider is selected
  26.     -   Fixes potential issue when launching from OmniFocus toolbar
  27.    
  28.     0.2c (2010-06-22)
  29.     -   Actual fix for autosave
  30.    
  31.     0.2b (2010-06-21)
  32.     -   Encapsulated autosave in "try" statements in case this fails
  33.    
  34.     0.2 (2010-06-15)
  35.     -   Fixed Growl code
  36.     -   Added performance optimization (thanks, Curt Clifton)
  37.     -   Changed from LGPL to MIT license (MIT is less restrictive)
  38.        
  39.     0.1: Original release. (Thanks to Curt Clifton, Nanovivid, and Macfaninpdx for various pieces of code)
  40.  
  41.    
  42.     # INSTALLATION #
  43.    
  44.     -   Copy to ~/Library/Scripts/Applications/Omnifocus
  45.     -   If desired, add to the OmniFocus toolbar using View > Customize Toolbar... within OmniFocus
  46.  
  47.    
  48.     # KNOWN ISSUES #
  49.     -   When the script is invoked from the OmniFocus toolbar and canceled, OmniFocus displays an alert.
  50.         This does not occur when invoked from another launcher (script menu, FastScripts LaunchBar, etc).
  51.        
  52. *)
  53.  
  54. -- To change settings, modify the following properties
  55. property showSummaryNotification : true --if true, will display success notifications
  56. property defaultOffset : 1 --number of days to snooze by default
  57. property defaultStartTime : 8 --default time to use (in hours, 24-hr clock)
  58.  
  59. -- Don't change these
  60. property alertItemNum : ""
  61. property alertItemPronoun : "It"
  62. property alertDayNum : ""
  63. property growlAppName : "Dan's Scripts"
  64. property allNotifications : {"General", "Error"}
  65. property enabledNotifications : {"General", "Error"}
  66. property iconApplication : "OmniFocus.app"
  67.  
  68. on main()
  69.     tell application "OmniFocus"
  70.         tell content of front document window of front document
  71.             --Get selection
  72.             set validSelectedItemsList to value of (selected trees where class of its value is not item and class of its value is not folder)
  73.             set totalItems to count of validSelectedItemsList
  74.             if totalItems is 0 then
  75.                 set alertName to "Error"
  76.                 set alertTitle to "Script failure"
  77.                 set alertText to "No valid task(s) selected"
  78.                 my notify(alertName, alertTitle, alertText)
  79.                 return
  80.             end if
  81.            
  82.             --User options
  83.             tell application "System Events" to open location "x-launchbar:hide"
  84.             display dialog "Snooze for how many days (from today)?" default answer defaultOffset buttons {"Cancel", "OK"} default button 2
  85.             set daysOffset to (the text returned of the result) as integer
  86.            
  87.             --Perform action
  88.             set todayStart to (current date) - (get time of (current date)) + (defaultStartTime * 3600)
  89.             set successTot to 0
  90.             set autosave to false
  91.             repeat with thisItem in validSelectedItemsList
  92.                 set succeeded to my snooze(thisItem, todayStart, daysOffset)
  93.                 if succeeded then set successTot to successTot + 1
  94.             end repeat
  95.             set autosave to true
  96.         end tell
  97.     end tell
  98.    
  99.     --Display summary notification
  100.     if showSummaryNotification then
  101.         set alertName to "General"
  102.         set alertTitle to "Script complete"
  103.         if daysOffset is not 1 then set alertDayNum to "s"
  104.         if successTot > 1 then
  105.             set alertItemPronoun to "They"
  106.             set alertItemNum to "s"
  107.         end if
  108.         set alertText to successTot & " item" & alertItemNum & " snoozed. " & alertItemPronoun & " will become available in " & daysOffset & " day" & alertDayNum & "." as string
  109.         my notify(alertName, alertTitle, alertText)
  110.     end if
  111. end main
  112.  
  113. on snooze(selectedItem, todayStart, daysOffset)
  114.     set success to false
  115.     tell application "OmniFocus"
  116.         try
  117.             set start date of selectedItem to my offsetDateByDays(todayStart, daysOffset)
  118.             set success to true
  119.         end try
  120.     end tell
  121.     return success
  122. end snooze
  123.  
  124. on offsetDateByDays(myDate, daysOffset)
  125.     return myDate + (86400 * daysOffset)
  126. end offsetDateByDays
  127.  
  128. (* Begin notification code *)
  129. on notify(alertName, alertTitle, alertText)
  130.     --Call this to show a normal notification
  131.     my notifyMain(alertName, alertTitle, alertText, false)
  132. end notify
  133.  
  134. on notifyWithSticky(alertName, alertTitle, alertText)
  135.     --Show a sticky Growl notification
  136.     my notifyMain(alertName, alertTitle, alertText, true)
  137. end notifyWithSticky
  138.  
  139. on IsGrowlRunning()
  140.     tell application "System Events" to set GrowlRunning to (count of (every process where creator type is "GRRR")) > 0
  141.     return GrowlRunning
  142. end IsGrowlRunning
  143.  
  144. on dictToString(dict) --needed to encapsulate dictionaries in osascript
  145.     set dictString to "{"
  146.     repeat with i in dict
  147.         if (length of dictString > 1) then set dictString to dictString & ", "
  148.         set dictString to dictString & "\"" & i & "\""
  149.     end repeat
  150.     set dictString to dictString & "}"
  151.     return dictString
  152. end dictToString
  153.  
  154. on NotifyWithGrowl(alertName, alertTitle, alertText, useSticky)
  155.     if useSticky then
  156.         set osascript to "property growlAppName : \"" & growlAppName & "\"
  157. property allNotifications : " & dictToString(allNotifications) & "
  158. property enabledNotifications : " & dictToString(enabledNotifications) & "
  159. property iconApplication : \"" & iconApplication & "\"
  160.  
  161. tell application \"GrowlHelperApp\"
  162.     register as application growlAppName all notifications allNotifications default notifications enabledNotifications icon of application iconApplication
  163.     notify with name \"" & alertName & "\" title \"" & alertTitle & "\" application name growlAppName description \"" & alertText & "\" with sticky
  164. end tell
  165. "
  166.     else
  167.         set osascript to "property growlAppName : \"" & growlAppName & "\"
  168. property allNotifications : " & dictToString(allNotifications) & "
  169. property enabledNotifications : " & dictToString(enabledNotifications) & "
  170. property iconApplication : \"" & iconApplication & "\"
  171.  
  172. tell application \"GrowlHelperApp\"
  173.     register as application growlAppName all notifications allNotifications default notifications enabledNotifications icon of application iconApplication
  174.     notify with name \"" & alertName & "\" title \"" & alertTitle & "\" application name growlAppName description \"" & alertText & "\"
  175. end tell
  176. "
  177.     end if
  178.     set shellScript to "osascript -e " & quoted form of osascript & " &> /dev/null &"
  179.     ignoring application responses
  180.         do shell script shellScript
  181.     end ignoring
  182. end NotifyWithGrowl
  183.  
  184. on NotifyWithoutGrowl(alertText)
  185.     tell application "OmniFocus" to display dialog alertText with icon 1 buttons {"OK"} default button "OK"
  186. end NotifyWithoutGrowl
  187.  
  188. on notifyMain(alertName, alertTitle, alertText, useSticky)
  189.     set GrowlRunning to my IsGrowlRunning() --check if Growl is running...
  190.     if not GrowlRunning then --if Growl isn't running...
  191.         set GrowlPath to "" --check to see if Growl is installed...
  192.         try
  193.             tell application "Finder" to tell (application file id "GRRR") to set strGrowlPath to POSIX path of (its container as alias) & name
  194.         end try
  195.         if GrowlPath is not "" then --...try to launch if so...
  196.             do shell script "open " & strGrowlPath & " > /dev/null 2>&1 &"
  197.             delay 0.5
  198.             set GrowlRunning to my IsGrowlRunning()
  199.         end if
  200.     end if
  201.     if GrowlRunning then
  202.         NotifyWithGrowl(alertName, alertTitle, alertText, useSticky)
  203.     else
  204.         NotifyWithoutGrowl(alertText)
  205.     end if
  206. end notifyMain
  207. (* end notification code *)
  208.  
  209. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement