Files and Folders

Loading a script file contained in the project bundle

This illustrates a lot of the changes in context required to make AppleScriptObjC to work. The items to note:

  • Note the syntax changes going from NSString *scriptPath = [myBundle pathForResource:fileName ofType:@"txt”]; to ASOC.
  • Note the coercion of the script’s path from POSIX file to alias
  • The syntactic placement POSIX file in the form as POSIX file is key.

Note, this subroutine only tells part of the story. Managing the linking of script files can get messy if not done right (and can result in a lot of duplicate code). Please see the detailed section “Linking Scripts” for more information on a way to manage this.

property kOCFooLibName : "OCFoo"
property kOCFooLib : missing value

set kOCFooLib to loadScriptInBundleWithName(kOCFooLibName)
showFoo() of kOCFooLib

on loadScriptInBundleWithName(scriptName) -- (string) as script
	set scriptPath to current application's NSBundle's mainBundle's pathForResource_ofType_(scriptName, "scpt") as text
	set scriptPath to (scriptPath as POSIX file)
	return load script scriptPath as alias
end loadScriptInBundleWithName

OCFoo.applescript

on showFoo() -- (void) as void		
	display dialog "FOO"
end showFoo