Monday, November 08, 2010 at 12:02 AM.


on type2CLK (path) {
	<<run a file of type 2CLK, usually a desktop script
		<<the object db address for the script is in 'STR ' resource 130
		<<the script object is in 'scpt' resource 129
		<<we unpack the script at the address and call it with the path we're passed
	local (returnedVal);
	on openError () {
		scriptError ("Can't open script in file \"" + file.fileFromPath (path) + "\".")};
	local (adr, wasdefined, x);
	if not rez.getStringResource (path, 130, @adr) {
		openError ()};
	if nameOf (adr^) == "" { <<not a valid address in this root - parse the string
		local (name = string.nthField (adr, '.', string.countFields (adr, '.')));
		adr = @system.deskscripts.[name]}
	else {
		adr = address (adr)};
	wasdefined = defined (adr^);
	bundle { <<move the script from the resource fork of the file into the object db at adr
		local (resdata);
		if not rez.getResource (path, scriptType, 129, @resdata) {
			if not rez.getResource (path, codeType, 129, @resdata) {
				openError ()}};
		unpack (@resdata, adr);
		delete (@resdata)};
	if kb.cmdKey () {
		if nameOf (adr^) == string (adr) { <<would be imported as a local (!)
			table.move (adr, @system.deskscripts); <<keep things neat
			adr = @system.deskscripts.[adr]};
		if Frontier.isRuntime () {
			returnedVal = false}
		else {
			export.lastfolder = file.folderFromPath (path); <<set default for script export
			system.deskscripts.path = path; <<make path available for debugging
			returnedVal = edit (adr);
			Frontier.bringToFront ();
			Frontier.findertofront = false}}
	else {
		local (params);
		if not rez.getStringResource (path, 127, @params) {
			params = 1};
		system.deskscripts.path = path; <<make path available to the script
		if number (params) == 1 {
			returnedVal = adr^ (path)} <<call the script, save returned value
		else {
			returnedVal = adr^ ()}; <<call the script with no parameters
		if not wasdefined { <<we created this object just to run it, and now we're done
			if (typeOf (adr^) != scriptType) or script.uncompile (adr) {
				delete (adr)}}};
	return (returnedVal)}

This listing is for code that runs in the OPML Editor environment. I created these listings because I wanted the search engines to index it, so that when I want to look up something in my codebase I don't have to use the much slower search functionality in my object database. Dave Winer.