EDIT: Found a way to get the document path in a Unix Script launched by TextWrangler, so I ve rewritten this.
There are multiple ways to work with scripts in TextWrangler through the #! menu, and I m not sure which one you re trying to use. It looks, though, like you re trying to create a Unix Script to convert your LilyPond document.
As your error hints, Unix Scripts unfortunately aren t given any arguments at all, so $1
will be empty. However, it turns out that recent versions of BBEdit/TextWrangler do set some environment variables before running your script (see BBEdit 9.3 Release Notes and scroll down to Changes). In particular, you can use the following environment variable:
BB_DOC_PATH path of the document (not set if doc is unsaved)
So, save this script to ~/Library/Application Support/TextWrangler/Unix Support/Unix Scripts
and you should be good to go.
Other ways you might be trying to do this that don t work well:
- Using a Unix Filter: to do this you would have to select all of your LilyPond code in the document, and it would be saved into a temporary file, which is passed as an argument to your script. OK, so that gets you an input filename, at the cost of some hassle. But then the output of that script (i.e. the LiiyPond compiler output) by default replaces whatever you just selected, which is probably not what you want. Wrong tool for the job.
- Using #! → Run on a LilyPond file: This involves putting a
#!
line at the top of your file and having TextWrangler attempt to execute your file as a script, using the #!
as a guide to selecting the script interpreter. Unfortunately, the #!
line only works with certain scripting languages, and LilyPond (not quite a scripting language) isn t one of them. This is what Peter Hilton is trying to do, and as he notes, you will get LilyPond syntax errors if you try to add a #! line to the top of a LilyPond file. (If you re curious, there is technically a way to get #! → Run to work, which is to embed your LilyPond code inside an executable shell or perl script, using here-document syntax. But this is a gross hack that will quickly become unwieldly.)
There are a few limitations to the script linked above:
- It doesn t check to see whether you saved your document before running LilyPond. It would be nice to have TextWrangler automatically save before running LilyPond.
- It can t take snippets of text or unsaved documents as input, only saved documents.
You can make more sophisticated solutions that would address these by turning to AppleScript. Two ways of doing this:
- Create a script that s specific to TextWrangler and drop it in
~/Library/Application Support/TextWrangler/Scripts
. It then shows up in the AppleScript menu (the weird scrolly S), or you can get at it by bringing up Window → Palettes → Scripts. I believe two folks out there have gone down this path and shared their results:
- Create a Mac OS Service, which would potentially be a method that would be reusable across just about any text editor. This was how we used to compile Common Music files way back in the NeXT days, so I can testify to its elegance. I don t have a good up-to-date example of this, unfortunately.