! The information I based my answer on was out of date, see under my answer for the update.
The reason why the script errors is because:
"The importation of frameworks via the use statement is only supported in script libraries, not in other scripts or applets."
So basically: you're not allowed to use the use statement in a regular AppleScript script. You can only use it in script libraries.
To fix this we create a "script library", which is just another AppleScript file. Let's say we call this script library chewbacca.scpt. You need to place the script in a specific location on your Mac. (You have a few options for this location). AppleScript only looks in those locations when trying to import script libraries.
Then, to use the script library do something like:
tell script "chewbacca"
display dialog "foo"
end tell
That should give the desired result.
Update:
After reading some answers and reading some more documentation:
The way AppleScript is extended is by importing a library, these libraries are called osax (or "Scripting Additions") because their file names end in .osax. OSAX stands for "Open Scripting Architecture eXtension". To import an osax we write use library in our script (I'm not 100% sure about this). By importing an osax we can use the commands in that osax.
AppleScript (the language) does not have commands for things like: user interaction dialogs (display dialog), reading and writing files, file system commands, date functions, and text and mathematical operations.
But: Apple does provide an osax that offers these commands: StandardAdditions.osax, it's not hard to see why this is one of the most commonly used osax.
To import this osax:
use scripting additions
Now back to my question:
I see AppleScript behaving differently under certain conditions:
- a script does not import an osax
- a script imports an osax (but not
StandardAdditions.osax)
In situation 1 it seems like AppleScript (the runtime?) silently auto-imports StandardAdditions.osax. I think this is the case because I can use display dialog.
In situation 2 AppleScript (the runtime) does not auto-import StandardAdditions.osax.
I can theorize about this different behavior:
I suspect for situation 1 they want to make it easier for people to get started with AppleScript so they auto-import the basic commands most people/beginners probably want to use.
The thinking behind situation 2 might have been something like:
"the developer is explicitly importing an osax so they may not have a
need for StandardAdditions.osax so let's not auto-import it".
I've found somewhere that it's a good idea to always explicitly import StandardAdditions.osax by adding use scripting additions to your script.
I'll follow this advice in the future.