In Progress AppleScript

Something changed in OmniFocus 2 back in April or so and this AppleScript that I have been using for years no longer

  1. duplicates actions,
  2. marks an item with [In Progress]
  3. puts the new duplicated item in the future (defer date) based on the days in the pop up box.
--Prepend text to select actions
--http://forums.omnigroup.com/showthread.php?p=101754#post101754


tell application "System Events" to key code 49 --Spacebar which causes OmniFocus to duplicate task and mark as complete

property prependText : "[In Pogress] "

tell application "OmniFocus"
  tell front document
    tell document window 1
      set oTrees to selected trees of content
      set IngTrees to count of oTrees
      if (IngTrees > 0) then
        set strPrepend to prependText
        
        repeat with iTree from 1 to IngTrees
          set oTask to value of (item iTree of oTrees)
          set name of oTask to strPrepend & name of oTask
        end repeat
      else
        display alert "No content selected!"
      end if
    end tell
  end tell
end tell


--See post 8 and 12 http://forums.omnigroup.com/showthread.php?t=22158&page=2


--*** This script uses four scripts combined
-- Some code from Dan Byler's "Snooze" script
(* 	# LICENSE #
  
  Copyright © 2010 Dan Byler (contact: dbyler@gmail.com)
  Licensed under MIT License (http://www.opensource.org/licenses/mit-license.php)
*)

--http://forums.omnigroup.com/showthread.php?p=101869#post101869 post 7 by scb	
--***Copy a list of selected tasks , or an individual task, to non repeating and remove flags if any. The original task is marked as completed.



property showAlert : false --if true, will display success/failure alerts
property useGrowl : true --if true, will use Growl for success/failure alerts
property defaultSnooze : 1 --number of days to snooze by default
property alertItemNum : ""
property alertDayNum : ""
property growlAppName : "Dan's Scripts"
property allNotifications : {"General", "Error"}
property enabledNotifications : {"General", "Error"}
property iconApplication : "OmniFocus.app"

tell application "OmniFocus"
  activate
  
  (*delay 1
  tell application "System Events" to key code 49 --Spacebar which causes OmniFocus to duplicate task and mark as complete
  tell application "System Events" to key code 49 --Spacebar which causes reactivatins of original task
  *)
  
  
  tell default document
    set FrontWindow to first document window whose index is 1
    tell FrontWindow
      --tell first document window of front document
      
      display dialog "Snooze start date for how many days from today?" default answer defaultSnooze buttons {"Cancel", "OK"} default button 2
      set snoozeLength to (the text returned of the result) as integer
      set todayStart to (current date) - (get time of (current date))
      if snoozeLength is not 1 then
        set alertDayNum to "s"
      end if
      
      if ((count of leaves of selected trees of content) is 0) then
        set theItems to value of selected trees of content
      else
        set theItems to (value of leaves of selected trees of content) as list
      end if
      
      set successTot to 0
      repeat with anItem in theItems
        if (class of anItem) is list then
          repeat with subItem in anItem
            if (class of subItem is task or class of subItem is inbox task) then
              
              set newitem to duplicate subItem to after subItem
              set repetition of newitem to missing value
              set flagged of newitem to false
              --mark complete selectedItem
              set complete of subItem to true
              set name of subItem to prependText & name of subItem
              set succeeded to my snooze(newitem, todayStart, snoozeLength)
              if succeeded then set successTot to successTot + 1
              
            end if
          end repeat
          
        else if (class of anItem is task or class of anItem is inbox task) then
          set newitem to duplicate anItem to after anItem
          set repetition of newitem to missing value
          set flagged of newitem to false
          set completed of anItem to true
          set name of anItem to prependText & name of anItem
          set succeeded to my snooze(newitem, todayStart, snoozeLength)
          if succeeded then set successTot to successTot + 1
          
        end if
      end repeat
      set alertName to "General"
      set alertTitle to "Script complete"
      if successTot > 1 then set alertItemNum to "s"
      set alertText to successTot & " item" & alertItemNum & " snoozed. The item" & alertItemNum & " will become available in " & snoozeLength & " day" & alertDayNum & "." as string
      my Notify(alertName, alertTitle, alertText)
    end tell
  end tell
end tell



-- ***Now change only the start date from absolute date (today's date) and not relative date.  Keep due date the same since that doesn't typically change (if so I would change that by hand or with another Dan's defer script)

--***The only thing I would change about this is to keep the start time (but not date) the same as it was, and not change it to 12AM like this script does.  I really like that it moves it from today's date because that way I know the exact date all the selected actions will be on.

on snooze(selectedItem, todayStart, snoozeLength)
  set success to false
  tell application "OmniFocus"
    try
      set oldStart to defer date of selectedItem
      if (oldStart is missing value) then
        set oldStart to todayStart -- no start date, pretend it was start of today
      end if
      set fracDay to time of oldStart
      set defer date of selectedItem to todayStart + fracDay + (days * snoozeLength)
      set success to true
    end try
  end tell
  return success
end snooze

on Notify(alertName, alertTitle, alertText)
  if showAlert is false then
    return
  else if useGrowl is true then
    --check to make sure Growl is running
    tell application "System Events" to set GrowlRunning to ((application processes whose (name is equal to "GrowlHelperApp")) count)
    if GrowlRunning = 0 then
      --try to activate Growl
      try
        do shell script "/Library/PreferencePanes/Growl.prefPane/Contents/Resources/GrowlHelperApp.app/Contents/MacOS/GrowlHelperApp > /dev/null 2>&1 &"
        do shell script "~/Library/PreferencePanes/Growl.prefPane/Contents/Resources/GrowlHelperApp.app/Contents/MacOS/GrowlHelperApp > /dev/null 2>&1 &"
      end try
      delay 0.2
      tell application "System Events" to set GrowlRunning to ((application processes whose (name is equal to "GrowlHelperApp")) count)
    end if
    --notify
    if GrowlRunning ≥ 1 then
      try
        tell application "Growl"
          register as application growlAppName all notifications allNotifications default notifications allNotifications icon of application iconApplication
          notify with name alertName title alertTitle application name growlAppName description alertText
        end tell
      end try
    else
      set alertText to alertText & " 
 
p.s. Don't worry—the Growl notification failed but the script was successful."
      display dialog alertText with icon 1
    end if
  else
    display dialog alertText with icon 1
  end if
end Notify

There was a change to the scripting dictionary in OmniFocus 2.12 that changed the terminology for completing actions. I’ve edited two lines, line 92 and line 104 to accommodate the change. I tried it and it appears to work as expected.

--Prepend text to select actions
--http://forums.omnigroup.com/showthread.php?p=101754#post101754


tell application "System Events" to key code 49 --Spacebar which causes OmniFocus to duplicate task and mark as complete

property prependText : "[In Pogress] "

tell application "OmniFocus"
  tell front document
    tell document window 1
      set oTrees to selected trees of content
      set IngTrees to count of oTrees
      if (IngTrees > 0) then
        set strPrepend to prependText
        
        repeat with iTree from 1 to IngTrees
          set oTask to value of (item iTree of oTrees)
          set name of oTask to strPrepend & name of oTask
        end repeat
      else
        display alert "No content selected!"
      end if
    end tell
  end tell
end tell


--See post 8 and 12 http://forums.omnigroup.com/showthread.php?t=22158&page=2


--*** This script uses four scripts combined
-- Some code from Dan Byler's "Snooze" script
(* 	# LICENSE #
  
  Copyright © 2010 Dan Byler (contact: dbyler@gmail.com)
  Licensed under MIT License (http://www.opensource.org/licenses/mit-license.php)
*)

--http://forums.omnigroup.com/showthread.php?p=101869#post101869 post 7 by scb	
--***Copy a list of selected tasks , or an individual task, to non repeating and remove flags if any. The original task is marked as completed.



property showAlert : false --if true, will display success/failure alerts
property useGrowl : true --if true, will use Growl for success/failure alerts
property defaultSnooze : 1 --number of days to snooze by default
property alertItemNum : ""
property alertDayNum : ""
property growlAppName : "Dan's Scripts"
property allNotifications : {"General", "Error"}
property enabledNotifications : {"General", "Error"}
property iconApplication : "OmniFocus.app"

tell application "OmniFocus"
	activate
	
	(*delay 1
  tell application "System Events" to key code 49 --Spacebar which causes OmniFocus to duplicate task and mark as complete
  tell application "System Events" to key code 49 --Spacebar which causes reactivatins of original task
  *)
	
	
	tell default document
		set FrontWindow to first document window whose index is 1
		tell FrontWindow
			--tell first document window of front document
			
			display dialog "Snooze start date for how many days from today?" default answer defaultSnooze buttons {"Cancel", "OK"} default button 2
			set snoozeLength to (the text returned of the result) as integer
			set todayStart to (current date) - (get time of (current date))
			if snoozeLength is not 1 then
				set alertDayNum to "s"
			end if
			
			if ((count of leaves of selected trees of content) is 0) then
				set theItems to value of selected trees of content
			else
				set theItems to (value of leaves of selected trees of content) as list
			end if
			
			set successTot to 0
			repeat with anItem in theItems
				if (class of anItem) is list then
					repeat with subItem in anItem
						if (class of subItem is task or class of subItem is inbox task) then
							
							set newitem to duplicate subItem to after subItem
							set repetition of newitem to missing value
							set flagged of newitem to false
							--mark complete selectedItem
							mark complete subItem -- Changed for OmniFocus 2.12+ compatibility (nostodnayr, 11 Aug 2018)
							set name of subItem to prependText & name of subItem
							set succeeded to my snooze(newitem, todayStart, snoozeLength)
							if succeeded then set successTot to successTot + 1
							
						end if
					end repeat
					
				else if (class of anItem is task or class of anItem is inbox task) then
					set newitem to duplicate anItem to after anItem
					set repetition of newitem to missing value
					set flagged of newitem to false
					mark complete anItem -- Changed for OmniFocus 2.12+ compatibility (nostodnayr, 11 Aug 2018)
					set name of anItem to prependText & name of anItem
					set succeeded to my snooze(newitem, todayStart, snoozeLength)
					if succeeded then set successTot to successTot + 1
					
				end if
			end repeat
			set alertName to "General"
			set alertTitle to "Script complete"
			if successTot > 1 then set alertItemNum to "s"
			set alertText to successTot & " item" & alertItemNum & " snoozed. The item" & alertItemNum & " will become available in " & snoozeLength & " day" & alertDayNum & "." as string
			my Notify(alertName, alertTitle, alertText)
		end tell
	end tell
end tell



-- ***Now change only the start date from absolute date (today's date) and not relative date.  Keep due date the same since that doesn't typically change (if so I would change that by hand or with another Dan's defer script)

--***The only thing I would change about this is to keep the start time (but not date) the same as it was, and not change it to 12AM like this script does.  I really like that it moves it from today's date because that way I know the exact date all the selected actions will be on.

on snooze(selectedItem, todayStart, snoozeLength)
  set success to false
  tell application "OmniFocus"
    try
      set oldStart to defer date of selectedItem
      if (oldStart is missing value) then
        set oldStart to todayStart -- no start date, pretend it was start of today
      end if
      set fracDay to time of oldStart
      set defer date of selectedItem to todayStart + fracDay + (days * snoozeLength)
      set success to true
    end try
  end tell
  return success
end snooze

on Notify(alertName, alertTitle, alertText)
  if showAlert is false then
    return
  else if useGrowl is true then
    --check to make sure Growl is running
    tell application "System Events" to set GrowlRunning to ((application processes whose (name is equal to "GrowlHelperApp")) count)
    if GrowlRunning = 0 then
      --try to activate Growl
      try
        do shell script "/Library/PreferencePanes/Growl.prefPane/Contents/Resources/GrowlHelperApp.app/Contents/MacOS/GrowlHelperApp > /dev/null 2>&1 &"
        do shell script "~/Library/PreferencePanes/Growl.prefPane/Contents/Resources/GrowlHelperApp.app/Contents/MacOS/GrowlHelperApp > /dev/null 2>&1 &"
      end try
      delay 0.2
      tell application "System Events" to set GrowlRunning to ((application processes whose (name is equal to "GrowlHelperApp")) count)
    end if
    --notify
    if GrowlRunning ≥ 1 then
      try
        tell application "Growl"
          register as application growlAppName all notifications allNotifications default notifications allNotifications icon of application iconApplication
          notify with name alertName title alertTitle application name growlAppName description alertText
        end tell
      end try
    else
      set alertText to alertText & " 
 
p.s. Don't worry—the Growl notification failed but the script was successful."
      display dialog alertText with icon 1
    end if
  else
    display dialog alertText with icon 1
  end if
end Notify

My apologies for the delay thank you very much for the help and letting me know the line numbers that changed, this is very helpful!

1 Like