Quote:
Originally Posted by skillet
I tried this as an experiment by putting in your code from another script you wrote. I modified it so it didn't work on selections, but it didn't work on even something simple!
|
OK, the problem with your first script here is that you aren't paying sufficient attention to the shapes and sizes of the pieces. newTasks is a list of tasks. The statement where you change the name to put on your string at the beginning is one that works on a single task — that's why the original script iterates through the list of tasks in the selection.
Code:
property pMaxAge : 2000 -- max number of seconds since creation for tasks of interest
property prependText : "[Action Created in the last 2000 seconds test] "
tell application "OmniFocus"
set timeNow to current date
set timeThen to timeNow - pMaxAge
tell front document
set newTasks to (every flattened task whose (creation date is greater than timeThen ¬
and repetition is missing value))
--http://forums.omnigroup.com/showthread.php?p=101754#post101754
tell front document
repeat with myTask in newTasks
set name of myTask to prependText & name of myTask
end repeat
end tell
end tell
end tell
The "repeat with myTask in newTasks ... end repeat" construct I added causes the local variable myTask to be filled in turn with each successive entry in the list newTasks. Your second "tell front document ... end tell" is superfluous in this case, as you are still inside the scope of the original, although it probably doesn't do any harm, either.
It makes more sense to collapse the actions done by the multiple scripts down into one pass through the loop. I've done so in the following script, and finished up the date adjustment for you.
Code:
--*** 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 prependText : "[In Progress] "
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
tell default document
set FrontWindow to first document window whose index is 1
tell FrontWindow
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
set completed 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 start date of selectedItem
set daysBetween to (todayStart - oldStart) / days as integer -- compute # of full days between orig start date+time and start of today
-- new start is old start + daysBetween + snooze + 1 (because snoozeLength 1 means starts tomorrow, not today)
set newStart to oldStart + (days * (daysBetween + snoozeLength + 1))
set start date of selectedItem to newStart
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 "GrowlHelperApp"
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