Questions about OSAKit

I’m trying to figure out some details about the public, but undocumented, OSAKit framework, which I’m trying to use from within an AppleScript script (!). I can initialize, compile and write a script to disk, using code similar to the following:


use framework "Foundation"
use framework "OSAKit"
use scripting additions

-- Path to the script we're going to create
set output to current application's NSURL's fileURLWithPath:((POSIX path of (path to desktop)) & "/test.scpt")
-- LibBundle.scptd is an existing script bundle containing only a script library called 'Lib', which we are going to use in our script
set someLib to current application's NSURL's fileURLWithPath:((POSIX path of (path to desktop)) & "/LibBundle.scptd")
set lang to current application's OSALanguage's defaultLanguage()'s sharedLanguageInstance()

set theScript to current application's OSAScript's alloc's ¬
	initWithSource:"use script \"Lib\"" fromURL:someLib languageInstance:lang usingStorageOptions:(current application's OSANull)

-- Not using this, it's here just as an example:
set {theData, theError} to theScript's compiledDataForType:"osas" usingStorageOptions:(current application's OSANull) |error|:(reference)

set {didSucceed, theError} to theScript's ¬
	writeToURL:output ofType:"osas" usingStorageOptions:(current application's OSANull) |error|:(reference)

if not didSucceed then
	return theError as list
end if
"OK"

First problem: the icon of the resulting script is not the usual script icon (it looks more like a generic document’s icon). Why?

Second: what are the legitimate values for the compiledDataForType and ofType parameters? I have used “osas”, but, in fact, using other strings does not seem to make any difference (possibly because the ones I’ve tried are all wrong, which might explain the icon problem, too).

Third: it seems to me that I should be able to use OSAKit to create an applet (since OSAScript.h has constants like OSAStayOpenApplet to be used when writing), and maybe also a script bundle. But I cannot figure out how to do that. Do you have any idea?

Finally, Script Editor doesn’t let me save the script.

1 and 2. the file type is correct osas (source: man page osacompile)
3. the enum value is 0x10000000 which means a decimal value of 268435456 will do just fine if the enumerator is not supported by the bridge.

You’re creating a script sort of OK, but you should compile it before trying to save it (compileAndReturnError:). I’m not sure what you’re trying to achieve with someLib; the only reason to provide a URL to an initWithSource: method is to provide a path for it to use for any “path to” stuff, and for knowing which bundle to search for script libraries.

When writing to a file, the type is not really “osas”, it’s one of Storage Types listed in OSAScript.h, and you supply storage options like any other ASObjC enum. So to save an applet you might use something like:

set theOptions to (current application's OSAPreventGetSource as integer) +  (current application's OSAStayOpenApplet as integer)
set {didSucceed, theError} to theScript's writeToURL:output ofType:(current application's OSAStorageApplicationBundleType) usingStorageOptions:theOptions |error|:(reference)

The output URL needs the appropriate extension.

The only point to using compiledDataForType::: is if you want to save the data yourself as a .scpt.

the only reason to provide a URL to an initWithSource: method is [.] for knowing which bundle to search for script libraries.

That’s it. See http://macscripter.net/viewtopic.php?id=42209.

I don’t know how I could have missed the storage type definitions, they were right in front of me! Thanks!