Question about 'use framework "Foundation"

This script fails UNLESS I comment out the 'use framework “Foundation” line. Why is that?

I was hoping I could just use these three header lines as general import statements regardless of whether they were needed or not, but it looks like that’s not going to be an option.

use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation" -- needs to be commented out
use scripting additions

tell application "Script Debugger"
	set theFormats to every AppleScript format
end tell

set the_file to ((path to desktop as text) & "formats.txt")
try
	set dataStream to open for access file the_file with write permission
	set eof of dataStream to 0
  	write theFormats to dataStream starting at eof as list
  	close access dataStream
on error
	try
		close access file the_file
	end try
end try

Returns:
"unable to coerce the date to the deisred type (errAECoercionFail:-1700)

If I comment out the ‘use framework…’ line, the script completes successfully and saves the file.

It exposes an issue with file specifiers. Rather than using file, you often need to use «class furl».

Try it like this:

use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use scripting additions

tell application "Script Debugger"
	set theFormats to every AppleScript format
end tell

set the_file to ((path to desktop as text) & "formats.txt") as «class furl»
try
	set dataStream to open for access the_file with write permission
	set eof of dataStream to 0
	write theFormats to dataStream starting at eof as list
	close access dataStream
on error
	try
		close access  the_file
	end try
end try

And please log it as a bug. Now’s a good time to try to get this sort of thing fixed.

1 Like

You mean with Apple?

EDIT: done.

Yes, with Apple. While they presumably have the code open for Sierra.

1 Like

I’ve reported this.

BTW, this still fails, even after adding «class furl» and not using SD:

use AppleScript version "2.4" -- Yosemite (10.10) or later 
use framework "Foundation"
use scripting additions

set x to "hello world"

set the_file to ((path to desktop as text) & "hello_world.txt") as «class furl»
try
	set dataStream to open for access file the_file with write permission
	set eof of dataStream to 0
	write x to dataStream starting at eof as list
	close access dataStream
on error
	try
		close access file the_file
	end try
end try

The script appears to complete, but the file is never created. Again, I have to comment out the “use framework” line AND remove the ‘class furl’ coercion to get it to successfully create the file.

Now that you’ve made the_file a furl/file, you should remove the word file before it when you use it. That should fix the problem.

Right, it does.

Is that another bug for Apple, then? Shouldn’t the compiler or runtime have warned me of that instead of just letting the script complete with no indication of anything going wrong (there are errors shown in SD’s event log, but nothing in SE’s replies or events)?

No, the compile never does any checking of what variables might or might not contain. The problem is that your two try blocks are keeping the run-time errors from you.

Assuming the error you’re trying to trap is the inability to open the file, the stuff following on error is unnecessary anyway – you can’t close what you couldn’t open.

You’d be better to do something like this:

use AppleScript version "2.4" -- Yosemite (10.10) or later 
use framework "Foundation"
use scripting additions

set x to "hello world"

set the_file to ((path to desktop as text) & "hello_world.txt") as «class furl»
try
	set dataStream to open for access the_file with write permission
on error errMess number errNum
	display dialog "Error: " & errMess & " (" & errNum & ")" -- or whatever
	error number -128
end try
set eof of dataStream to 0
write x to dataStream as list
close access dataStream

That’s a bug in SE, yes.

Ah, thanks. I’d forgotten about doing that.