load script and "Can't make ... into type reference" issue

I’ve found this site immensely helpful for learning AppleScript and solving issues that others have run into, but I can’t seem to find anyone else running into this kind of issue. Maybe because I’m not entirely sure what the issue is. :frowning:

The basic issues I’m running into is when I try to do something like this:

copy lib's CalendarDate to firstDate

What happens when I run, I get an error message that reads “error “Can’t make CalendarDate into type reference.” number -1700 from CalendarDate to reference

I feel I should mention that I’m running Mac OS X 10.8.4, but I’ve also tested this on Mac OS X 10.7.5 with the same results.

So, here’s how we get to the issue: I was following this article about Test Driven Development with AppleScript: http://www.mactech.com/articles/mactech/Vol.24/24.04/Test-DrivenDevelopmentUsingAppleScript/index.html

The issue started when I took the CalendarDate script and put it into its own script file. Now when I try to initialize the CalendarDate “object”, well, I guess it doesn’t know how to do that from a different file. :frowning:

Here’s the two script files I’m dealing with. Please Note that you need the ASUnit script from this site and put it into your /Users/[your username]/Library/Scripts folder: http://nirs.freeshell.org/asunit/

I’ve got my Date.scpt file (located on my Desktop) which looks like this:

script |CalendarDate|
	-- CalendarDate has three properties - day, month, and year.
	property calendarDay : 0.0
	property calendarMonth : 0.0
	property calendarYear : 0.0
	
	-- Sets the calendarDay property to the value passed to it.
	on SetDay(theDay)
		set calendarDay to theDay
	end SetDay
	
	-- Sets the calendarMonth property to the value passed to it.
	on SetMonth(theMonth)
		set calendarMonth to theMonth
	end SetMonth
	
	-- Sets the calendarYear property to the value passed to it.
	on SetYear(theYear)
		set calendarYear to theYear
	end SetYear
	
	-- Returns the value of the calendarDay property.
	on GetDay()
		return calendarDay
	end GetDay
	
	-- Returns the value of the calendarMonth property.
	on GetMonth()
		return calendarMonth
	end GetMonth
	
	-- Returns the value of the calendarYear property.
	on GetYear()
		return calendarYear
	end GetYear
	
	on InitializeDate(myDate, myMonth, myYear)
		SetDay(myDate)
		SetMonth(myMonth)
		SetYear(myYear)
	end InitializeDate
end script

And my DateTest.scrpt (also located on my Desktop), which is the file that you run, which produces the error:

property parent : load script file ((path to scripts folder from user domain as string) & "ASUnit.scpt")
property lib : load script file ((path to desktop as string) & "Date.scpt")
property suite : makeTestSuite("My Date Tests")

script |DateTests|
	property parent : registerFixture(me)
	
	property firstDate : missing value
	property secondDate : missing value
	
	on setUp()
		copy lib's CalendarDate to firstDate
		copy lib's CalendarDate to secondDate
		-- Set the values for the first date.
		tell firstDate to InitializeDate(21, 9, 2007)
		-- Set the values for the second date.
		tell secondDate to InitializeDate(25, 9, 2005)
	end setUp
	
	script |CheckSameMonth|
		property parent : registerTestCase(me)
		set p to firstDate's GetMonth()
		set d to secondDate's GetMonth()
		should(p is equal to d, "month not equal!")
	end script
	
	script |CheckDifferentMonth|
		property parent : registerTestCase(me)
		set p to firstDate's GetMonth()
		set d to secondDate's GetMonth()
		should(p is not equal to d, "month is the same!")
	end script
	script |CheckSameDay|
		property parent : registerTestCase(me)
		set p to firstDate's GetDay()
		set d to secondDate's GetDay()
		should(p is equal to d, "day not equal!")
	end script
	
	script |CheckDifferentDay|
		property parent : registerTestCase(me)
		set p to firstDate's GetDay()
		set d to secondDate's GetDay()
		should(p is not equal to d, "day is the same!")
	end script
	
	script |CheckSameYear|
		property parent : registerTestCase(me)
		set p to firstDate's GetYear()
		set d to secondDate's GetYear()
		should(p is equal to d, "year not equal!")
	end script
	
	script |CheckDifferentYear|
		property parent : registerTestCase(me)
		set p to firstDate's GetYear()
		set d to secondDate's GetYear()
		should(p is not equal to d, "year is the same!")
	end script
end script
run makeTextTestRunner(suite)

Thank you so much for your time.

Model: iMac
AppleScript: 2.5.1
Browser: Safari 536.30.1
Operating System: Mac OS X (10.8)

Hi. It may help any would-be scrutator for you to post a clear synopsis of what the code should do, but I immediately see a misuse of the pipe brackets. Pipes are absolutes for reserved words, and variables with weird characters; here, they are superfluous and potentially problematic.

Hi,

I don’t know what this means but:

tell application "Finder"
	googoo of me
end tell

Thank you for the reply. The code is basically directly from the article about Test Driven Development with AppleScript. I’m not at my computer right now but I’ll see if removing the pipes helps.

And the DateTest script creates instances of the CalendarDate “object” from the Date.scpt. It then builds test cases and tests the CalendarDate objects using ASUnit. I recommend reading the article. It’s really great except I can’t get it to work right. :confused:

Hi.

Marc’s right. The error’s occurring because the script object’s called |CalendarDate| and your script’s trying to access something called CalendarDate, which is a different name.

But a script containing just a script object is a superfluous layer. It would be more sensible to give the contents of the script object to the script itself .

-- CalendarDate has three properties - day, month, and year.
property calendarDay : 0.0
property calendarMonth : 0.0
property calendarYear : 0.0

-- Sets the calendarDay property to the value passed to it.
on SetDay(theDay)
	set calendarDay to theDay
end SetDay

-- Plus the other six handlers here.

. and to name the property you use to hold it in the main script directly:

property parent : load script file ((path to scripts folder from user domain as string) & "ASUnit.scpt")
property CalendarDate : load script file ((path to desktop as string) & "Date.scpt")
property suite : makeTestSuite("My Date Tests")

You can then use the original lines to set the firstDate and secondDate variables:

copy CalendarDate to firstDate
copy CalendarDate to secondDate

Another point to note is that the way you’ve defined the loaded scripts as properties, they’ll be loaded when the main script’s compiled. If you subsequently make any changes to the loaded scripts, the main script will still contain the original versions until it’s recompiled. It’s usually better to use ‘load script’ in a run-time setting. You could possibly use it to set firstDate and secondDate directly:

set dateScriptPath to (path to desktop as text) & "Date.scpt"
set firstDate to (load script file dateScriptPath)
set secondDate to (load script file dateScriptPath)

You guys are top notch experts! Seriously, thank you everyone! :smiley:

The bit about |CalendarDate| is not the same as CalendarDate was enough to solve the problem I was having, but the rest of the advice was also very valuable.

Again, thank you very much Marc and Nigel. My problem has been solved in lightning speed.