SL script seems to be broken

I have a script i made previously in Leopard, but now i am running Snow Leopard. Its idea is to be run from Excel’s script menu and it will convert schedules in excel documents into ical todos. I tried making a few adjustments, and dont know what happened…but it stopped working yesterday.

  1. Can someone look at below script and advise how to fix? it runs OK if it is creating a new calendar (ProjectName), but if there is an existing calendar (ProjectName) then it fails to make any updates to the new dates.

  2. Is there any way to add something to transfer notes/urls when updating existing calendars? I sometimes manually add in urls (to emails) or notes to the todo’s, but when i ran this script in past it would lose the notes/urls when changing the date i think.

sample milestone in xls:
http://rapidshare.com/files/331104488/Test.xls.html

property summaryList : {"Rough Ceramics", "Tooling Quote", "Casting Ceramics", "Tooling PO", "Release Casting", "Paint Photos", "Layout", "Vendor", "Paint Guides/Master", "Decals", "1st Shots", "Chase Decals/Guide", "Mockup", "Packaging Files", "Chromalin", "EP", "1st Deco", "Proofs", "PO", "Chase 1st Deco", "Printing Release", "Blister Release", "Injection Release", "Deco Release", "Deco Breakdown", "PP"}

tell application "Microsoft Excel"
	set ProjectName to the value of cell "$F$4"
	set ReviseDate to the value of cell "$AD$4"
	set range_value to item 1 of (get value of range "E12:Y12")
	set {RoughCeramics, ToolQuote, Casting, CastingApp, ToolPO, blind, PaintPhoto, Layout, blind, VendorConfirm, PaintMaster, FirstShot, Mockup, Packaging, EP, FirstDeco, PO, ReleasePrint, ReleaseInject, ReleaseDeco, PP} to range_value
	close front window
end tell

tell application "iCal"
	if not (exists calendar ProjectName) then
		tell (make new calendar at end of calendars with properties {name:ProjectName, description:ReviseDate as text})
			set its color to {13500, 28500, 48500}
		end tell
	end if
	set dateList to {RoughCeramics, ToolQuote, Casting, ToolPO, CastingApp, PaintPhoto, Layout, VendorConfirm, PaintMaster, PaintMaster, FirstShot, FirstShot, Mockup, Packaging, Packaging, EP, FirstDeco, Packaging, PO, FirstDeco, ReleasePrint, ReleasePrint, ReleaseInject, ReleaseDeco, ReleaseDeco, PP}
	repeat with i from 1 to count summaryList
		set theDate to my calcDate(item i of dateList)
		set {theTodo, isCompleted} to my check_Todo(ProjectName, item i of summaryList)
		if class of theDate is date then
			if item i of summaryList is "Proofs" then set theDate to theDate + 1 * weeks
			if item i of summaryList is "Chase Decals/Guide" then set theDate to theDate + 2 * weeks
			if item i of summaryList is "Chase 1st Deco" then set theDate to theDate + 2 * weeks
			if theTodo is false or (item i of summaryList is "Proofs" and isCompleted is false) or (item i of summaryList is "Chase Decals/Guide" and isCompleted is false) or (item i of summaryList is "Chase 1st Deco" and isCompleted is false) then
				if item i of summaryList is "Proofs" and theTodo is not false then delete theTodo
				if item i of summaryList is "Chase Decals/Guide" and theTodo is not false then delete theTodo
				if item i of summaryList is "Chase 1st Deco" and theTodo is not false then delete theTodo
				make new todo at end of todos of calendar ProjectName with properties {due date:theDate, description:ProjectName, summary:item i of summaryList}
			else
				if isCompleted is false then set due date of theTodo to theDate
			end if
		end if
	end repeat
end tell

on calcDate(d)
	if class of d is date then return d
	if d is "" or d is in {"N/A", "NA", "on hold"} then return false
	set delim to item (((d contains "/") as integer) + 1) of {".", "/"}
	set {TID, text item delimiters} to {text item delimiters, delim}
	try
		set {mn, dy, yr} to text items of d
		set yr to yr mod 1000 + 2000
		set text item delimiters to TID
		tell (current date) to set d to it - (its time)
		tell d to set {its day, its month, its year} to {dy as integer, mn as integer, yr as integer}
		return d
	on error
		set text item delimiters to TID
		return false
	end try
end calcDate

on check_Todo(cal, param)
	tell application "iCal"
		tell calendar cal
			repeat with tt in (get todos)
				tell contents of tt
					if summary is param then
						set c to completion date
						try
							c
							return {it, true}
						on error
							return {it, false}
						end try
					end if
				end tell
			end repeat
		end tell
		return {false, false}
	end tell
end check_Todo


any help…would be greatly appreciated

a lil help?

I don’t see it.

try to find your own bug with the log-command

Let’s say you think the bug is located in this part of the script:

 if not (exists calendar ProjectName) then
       tell (make new calendar at end of calendars with properties {name:ProjectName as text})
           set its color to {13500, 28500, 48500}
       end tell
   end if
   set dateList to {RoughCeramics, ToolQuote, Casting, ToolPO, CastingApp, PaintPhoto, Layout, VendorConfirm, PaintMaster, PaintMaster, FirstShot, FirstShot, Mockup, Packaging, Packaging, EP, FirstDeco, Packaging, PO, FirstDeco, ReleasePrint, ReleasePrint, ReleaseInject, ReleaseDeco, ReleaseDeco, PP}

try to check by doing this:

if not (exists calendar ProjectName) then
       log "Making new calendar"
       tell (make new calendar at end of calendars with properties {name:ProjectName as text})
           set its color to {13500, 28500, 48500}
       end tell
   else
       log "Calendar all ready exists, continuing"
   end
   set dateList to {RoughCeramics, ToolQuote, Casting, ToolPO, CastingApp, PaintPhoto, Layout, VendorConfirm, PaintMaster, PaintMaster, FirstShot, FirstShot, Mockup, Packaging, Packaging, EP, FirstDeco, Packaging, PO, FirstDeco, ReleasePrint, ReleasePrint, ReleaseInject, ReleaseDeco, ReleaseDeco, PP}


(Because the forum won't let me post the continuation of my post???????? :/ see the next post for further information) ...

(the previous post didn’t let me continue my answer so I’ll have to do it here)

By making an excel document with some data and the name of a calendar that all ready exists, you can look in the events-tab (in Script Editor) to see if there’s an event that looks like this: (* Calendar all ready exists, continuing *). If not, you’ll have to check for another method to check if a calendar all ready exists. If so, the bug is located somewhere else, and you have to repeat the above steps in your script at another location, until you find the real bug.

Hope it works,
ief2

it looks as though it continues in a loop of get the summary for todos…but doesnt actually do anything. It stops the loop with no errors, but the loop repeats itself (from what i can tell) maybe a dozen times.

also, i do not see anything for it saying that it found an existing calendar.

i posted a sample milestone…in case that helps anyone catch this error.
http://rapidshare.com/files/331104488/Test.xls.html

i appreciate the help.

I believe the interpretation of short year numbers changed in Snow Leopard. In your calcDate() handler, this line:

if yr as integer < 10 then set yr to (yr as integer) + 2000

. doesn’t add 2000 to “10”, so you wind up with a (wrong!) date in the year 0010. I’d change the line to:

set yr to yr mod 1000 + 2000

Hopefully that should be OK for the next 990 years.

thanks for help, but it didnt work. still same problem. if creating a new calendar its no problem, but if updating an existing calendar it does not change any dates for todos.

Upon running your script I received the following error {error “The variable ReviseDate is not defined.” number -2753 from “ReviseDate”} it appears to crash at this block if…

if not (exists calendar ProjectName) then
		tell (make new calendar at end of calendars with properties {name:ProjectName, description:ReviseDate as text})
			set its color to {13500, 28500, 48500}
		end tell
	end if

I merely copied and pasted your script into my AS editor, so not sure what happened. But can you tell me where that variable was defined or is drawing its value from?

Thanks,
Mark

Model: iMac
AppleScript: 2.1.1 (ASE 2.3)
Browser: Safari 531.21.10
Operating System: Mac OS X (10.6)

sorry about that, i realized that too and added that entry back in, but forgot to update my code. 1st post is now updated with correct code that fixes this error. but sadly it is not the reason for the script failing to update my existing calendars.

Here is your fix…had to trap the missing value in the completion date (not and error) with if…then

Keeps urls too when updating. Let me know if it works when you try. Ran several iterations here and seems to hold. Left your code in to show what/where I changed it.

Cheers,
Mark

property summaryList : {"Rough Ceramics", "Tooling Quote", "Casting Ceramics", "Tooling PO", "Release Casting", "Paint Photos", "Layout", "Vendor", "Paint Guides/Master", "Decals", "1st Shots", "Chase Decals/Guide", "Mockup", "Packaging Files", "Chromalin", "EP", "1st Deco", "Proofs", "PO", "Chase 1st Deco", "Printing Release", "Blister Release", "Injection Release", "Deco Release", "Deco Breakdown", "PP"}

tell application "Microsoft Excel"
	set ProjectName to the value of cell "$F$4"
	set ReviseDate to the value of cell "$Z$4"
	set range_value to item 1 of (get value of range "E12:Y12")
	set {RoughCeramics, ToolQuote, Casting, CastingApp, ToolPO, blind, PaintPhoto, Layout, blind, VendorConfirm, PaintMaster, FirstShot, Mockup, Packaging, EP, FirstDeco, PO, ReleasePrint, ReleaseInject, ReleaseDeco, PP} to range_value
	close front window
end tell

tell application "iCal"
	if not (exists calendar ProjectName) then
		tell (make new calendar at end of calendars with properties {name:ProjectName, description:ReviseDate as text})
			set its color to {13500, 28500, 48500}
		end tell
	end if
	set dateList to {RoughCeramics, ToolQuote, Casting, ToolPO, CastingApp, PaintPhoto, Layout, VendorConfirm, PaintMaster, PaintMaster, FirstShot, FirstShot, Mockup, Packaging, Packaging, EP, FirstDeco, Packaging, PO, FirstDeco, ReleasePrint, ReleasePrint, ReleaseInject, ReleaseDeco, ReleaseDeco, PP}
	repeat with i from 1 to count summaryList
		set theDate to my calcDate(item i of dateList)
		set {theTodo, isCompleted} to my check_Todo(ProjectName, item i of summaryList)
		if class of theDate is date then
			if item i of summaryList is "Proofs" then set theDate to theDate + 1 * weeks
			if item i of summaryList is "Chase Decals/Guide" then set theDate to theDate + 2 * weeks
			if item i of summaryList is "Chase 1st Deco" then set theDate to theDate + 2 * weeks
			if theTodo is false or (item i of summaryList is "Proofs" and isCompleted is false) or (item i of summaryList is "Chase Decals/Guide" and isCompleted is false) or (item i of summaryList is "Chase 1st Deco" and isCompleted is false) then
				if item i of summaryList is "Proofs" and theTodo is not false then delete theTodo
				if item i of summaryList is "Chase Decals/Guide" and theTodo is not false then delete theTodo
				if item i of summaryList is "Chase 1st Deco" and theTodo is not false then delete theTodo
				make new todo at end of todos of calendar ProjectName with properties {due date:theDate, description:ProjectName, summary:item i of summaryList}
			else
				if isCompleted is false then set due date of theTodo to theDate
			end if
		end if
	end repeat
end tell

on calcDate(d)
	if class of d is date then return d
	if d is "" or d is in {"N/A", "NA", "on hold"} then return false
	set delim to item (((d contains "/") as integer) + 1) of {".", "/"}
	set {TID, text item delimiters} to {text item delimiters, delim}
	try
		set {mn, dy, yr} to text items of d
		set yr to yr mod 1000 + 2000
		-- old line for above-- if yr as integer < 10 then set yr to (yr as integer) + 2000
		set text item delimiters to TID
		tell (current date) to set d to it - (its time)
		tell d to set {its day, its month, its year} to {dy as integer, mn as integer, yr as integer}
		return d
	on error
		set text item delimiters to TID
		return false
	end try
end calcDate

on check_Todo(cal, param)
	tell application "iCal"
		tell calendar cal
			repeat with tt in (get todos)
				tell contents of tt
					if summary is param then
						set c to completion date
						if c is missing value then
							return {it, false}
						else
							return {it, true}
						end if
						--replaced following try with above catch for Missing Value constant, which is what c is for no completion date, doesn't give error
						--try
						--	c
						--	return {it, true}
						--on error
						--	return {it, false}
						--end try
					end if
				end tell
			end repeat
		end tell
		return {false, false}
	end tell
end check_Todo

Model: iMac
AppleScript: 2.1.1 (ASE 2.3)
Browser: Safari 531.21.10
Operating System: Mac OS X (10.6)

i love you. thank you so much it works perfectly and the notes/urls are carrying over when updated too. awesome job.

sorry…but something still doesnt seem to be working.
Very often i am getting a new calendar created with same title, but adding a # at the end (2, 3, 4, etc) instead of updating the existing original calendar.

anyone can help to get this fixed and working properly again?