[Tutorial] Scripting Libraries


It’s surprising that you have to use an “empty tag” though, when there is nothing to be put inside the command tag.

Hindsightly I guess I had saved some hours if I had started out with AppleScriptObjC Explorer in the first place. I managed to wreak havoc with AppleScript Editor over the course, and I actually had to restart it, not only close the script bundle, which I had figured up front I never would have, as long as I didn’t add the script bundle to the dictionary library of AppleScript Editor. Now things are working great, and thanks for all your help and patience! :slight_smile:


The main take away from me besides using “empty tags”, when there wasn’t any content to put within has been the cycle:

  1. Edit and save the sdef file.
  2. Save the bundle.
  3. Add your applescript handler to the bundle.
  4. Save and test the bundle.

UPDATE: It seems that once it compiled it doesn’t matter?

Looking further into this…


I think I’ll just use what you call an “empty” tag, when there are nothing like direct-parameter and other parameters inside the command block. It’s a good rule of thumb, particularily, when Shane’s AsObjC Explorer creates such sdef definitions for such commands.

I think I may have really littered Script Editor in the process, but now the process is controllable again. :slight_smile:

When there is no dictionary added to the library of AppleScript Editor, no script in the editor , that uses the library bundle I am editing in the editor; then edititing the sdef, saving the bundle, adding the handler, then compiling the bundle, and re-saving it works well!

This even works if I have to change words in the command or parameters, due to having used some reserved word, like “using” for instance, and re compile, works like a charm now!

I think I’m losing my mind here :expressionless:

Yesterday, behind the same machine I couldn’t compile your sdef file and when changed it into <command > I could compile it. When I set it back to tag it wouldn’t compile again just to make sure that it was the problem. And again when set to style it would compile. Because I was working with libxml2, and it does have a difference between these two (the check for empty tag will return true for <command > and will return false ), I assumed there is a difference for script libraries as well. But today I wanted to get the same “error” again and everything compiles just fine, even without extra saving. So I guess it has to do with some caching or something in the background.

Einstein once said: “Insanity is doing the same thing over and over again and expecting different results.”. I’m in doubt if I’m going insane or Mavericks :D.

FWIW, I just use:

NSData *sdefData = [self.sdefXMLDocument XMLDataWithOptions:NSXMLDocumentTidyXML];

The only messing with XML I do is when I read in an existing sdef, in which case I turn all type attributes into elements, because it makes the interface simpler. And of course the table handles things like escaping reserved characters.

You young 'uns have it easy :wink: In the old pre-Mavericks days, the slightest mistake in an sdef would usually mean at least quitting and relaunching your editor. After much begging, sdef file modification dates are now compared and caches cleared if necessary.

Well it was you or me, so I’m happy :stuck_out_tongue:


I believe what ruined it totally for me yesterday, was having several scriptbundles, with almost the same terminology, and some driver scripts as well. I guess I managed somehow to trash the cache, but I am not looking back into it.

Next time I get serious issues, I’ll restart the Script Editor and take it from there.

Thanks for the tidy xml command Shane, I didn’t know it convert tags! :slight_smile:

Found it!

I was having cache issue. It seems that the running instance of AppleScript loves to cache script libraries and it’s definition files. Even when I remove the a script library and then create a new one with the same specs the errors will be thrown on my screen knowing it shouldn’t give an error in the first place. I need to logout and login again in order to make the script compile again. I will look into this if Yosemite have these cache issues as well.

In short: The first steps I posted previously is very important you do them correctly the first time! If not, there is no way you can make the scripting definition to load properly and you’re forces to start over or re-login to the system. I will take these steps in side note in the tutorial as well.

FWIW, one of the biggest caching gotchas is that the modification date check is done with the app or library, not the sdef file. Changing a file inside a bundle doesn’t always change the bundle’s modification date. Even saving over an existing bundle doesn’t always change the bundle’s modification date, either. So you need to think about doing something like touching the library.


I tried to run a pair of empty tags thru an xml validator, and it validated just fine. And so does a single empty tag.

We all know that we should use single empty tags, but I was curious and that is why I post the result.

The conclusion is that sdef is based on tidied xml, and just not xml.

And now, over to the chess world championship. :slight_smile:

I was trying to learn how to make my own library based on this tutorial.
I didn’t succeed, and I would like to have your help.
My goal is to make a library which I can call directly, just using

use script "Luciano_Library"

AND NOT using the:

use myLib : script  "Luciano_Library"

For example, in the tutorial, I could not find the " Inside the drawer you’ll see the contents of your bundle. Inside this drawer there is a checkbox to enable AppleScriptObjC, "

What I am asking if someone can make a library for me using the script below, just 3 subroutine of different types (alias, integer, text) as a bundle, which would include also:

  • a simplified version of the ScriptingDictionary.sdef
  • Info.plist
    So from this I can learn and make what I need.
    Or as alternative to direct me to a place where I can learn how to do it.

Thanks a lot

Subroutines for the library:

on BeepAlert(numberOfTimes) -- integer
	beep numberOfTimes
end BeepAlert

on WriteTotextFile(fAlias, textToWrite) -- alias, text
	set fileDescriptor to (open for access fAlias with write permission)
		set eof fileDescriptor to 0
		write textToWrite to fileDescriptor -- as «class utf8»
		close access fileDescriptor
	on error errMsg number errNbr
		close access fileDescriptor
		error errMsg number errNbr
	end try
end WriteTotextFile

on listToString(theList, theDelimiter) -- List, text
	set theBackup to AppleScript's text item delimiters
	set AppleScript's text item delimiters to theDelimiter
	-- Perform the conversion
	set theString to theList as string
	set AppleScript's text item delimiters to theBackup
	return theString
end listToString