使用 Textwrangler 排版 Lilypond 文件的 Shell 脚本

发布于 2024-11-25 12:30:42 字数 497 浏览 4 评论 0原文

我需要一个 shell 脚本,它允许我从 TextWrangler(Mac 应用程序)排版 Lilypond 文件。 到目前为止,我已经想出了这个:

#!/bin/sh
/Applications/LilyPond.app/Contents/Resources/bin/lilypond -o $1

当然,这是行不通的。 (这就是我在 Stack Overflow 工作的原因。)

当我从 TextWrangler 中的 shebang 菜单运行该脚本时,我得到以下输出:

/Applications/LilyPond.app/Contents/Resources/bin/lilypond: option faultpaper,
--output'' requires an argument

给出了什么?

我正在运行 Snow Leopard、TextWrangler 和 Lilypond。 帮助表示赞赏。

I need a shell script which will allow me to typeset Lilypond files from TextWrangler (A Mac App).
So far I have come up with this:

#!/bin/sh
/Applications/LilyPond.app/Contents/Resources/bin/lilypond -o $1

which, of course, doesn't work. (That's why I'm at Stack Overflow.)

When I run that script from the shebang menu in TextWrangler, I get this output:

/Applications/LilyPond.app/Contents/Resources/bin/lilypond: option faultpaper,
--output'' requires an argument

What gives?

I'm running Snow Leopard, TextWrangler, and Lilypond.
Help appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

街角卖回忆 2024-12-02 12:30:42

编辑:找到了一种在 TextWrangler 启动的 Unix 脚本中获取文档路径的方法,所以我重写了它。

通过 #! 菜单,可以通过多种方式在 TextWrangler 中使用脚本,但我不确定您要使用哪一种。不过,看起来您正在尝试创建一个 Unix 脚本来转换您的 LilyPond 文档。

正如您的错误提示,不幸的是,Unix 脚本根本没有给出任何参数,因此 $1 将为空。然而,事实证明,最新版本的 BBEdit/TextWrangler 在运行脚本之前确实设置了一些环境变量(请参阅BBEdit 9.3 发行说明 并向下滚动到更改)。特别是,您可以使用以下环境变量:

BB_DOC_PATH            path of the document (not set if doc is unsaved)

因此,将此脚本保存到~/库/应用程序支持/TextWrangler/Unix 支持/Unix 脚本,您应该可以开始了。

您可能尝试执行此操作的其他方法效果不佳:

  • 使用 Unix 过滤器:要执行此操作,您必须选择文档中的所有 LilyPond 代码,并且它将被保存到一个临时文件中,该文件作为参数传递给脚本。好的,这样你就可以得到一个输入文件名,但代价是一些麻烦。但是,该脚本的输出(即 LiiyPond 编译器输出)默认会替换您刚刚选择的内容,这可能不是您想要的。不适合这项工作的工具。
  • 使用#! → 在 LilyPond 文件上运行 这涉及在文件顶部放置 #! 行,并让 TextWrangler 尝试使用 < code>#! 作为选择脚本解释器的指南。不幸的是,#! 行仅适用于某些脚本语言,而 LilyPond(不完全是脚本语言)不是其中之一。这就是 Peter Hilton 正在尝试做的事情,正如他所指出的,如果您尝试将 #! 行添加到 LilyPond 文件的顶部,您将收到 LilyPond 语法错误。 (如果您好奇,从技术上讲,有一种方法可以让 #! → Run 工作,即使用此处文档语法将 LilyPond 代码嵌入到可执行 shell 或 perl 脚本中。但是这是一个粗俗的黑客行为,很快就会变得难以控制。)

上面链接的脚本有一些限制:

  • 它不会检查您在运行 LilyPond 之前是否保存了文档。如果 TextWrangler 在运行 LilyPond 之前自动保存就太好了。
  • 它不能将文本片段或未保存的文档作为输入,只能将已保存的文档作为输入。

您可以通过使用 AppleScript 来制定更复杂的解决方案来解决这些问题。有两种方法可以实现此目的:

  • 创建一个特定于 TextWrangler 的脚本并将其放入 ~/Library/Application Support/TextWrangler/Scripts 中。然后它会显示在 AppleScript 菜单中(奇怪的滚动 S),或者您可以通过调出窗口 → 调色板 → 脚本 来获取它。我相信有两个人已经走了这条路并分享了他们的结果:
  • 创建一个 Mac OS 服务,这可能是一种可以在任何文本编辑器中重复使用的方法。这就是我们在 NeXT 时代用来编译 Common Music 文件的方式,所以我可以证明它的优雅。不幸的是,我没有一个很好的最新例子。

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.
她说她爱他 2024-12-02 12:30:42

好问题。如果您这样做,它实际上会在我的系统上运行 Lilypond:

#!/Applications/LilyPond.app/Contents/Resources/bin/lilypond -o $1

...但会失败,因为 # 不是行注释字符,因此 Lilypond 会尝试解析该行。

用块注释包围它会失败,因为 TextWrangler 找不到“shebang”行。

%{
#!/Applications/LilyPond.app/Contents/Resources/bin/lilypond -o $1
%}

另一种方法是使用 Smultron 3,它可以让您定义您需要的命令可以使用键盘快捷键运行。

Good question. It actually runs Lilypond on my system if you do this:

#!/Applications/LilyPond.app/Contents/Resources/bin/lilypond -o $1

… but fails because # is not a line-comment character so Lilypond tries to parse the line.

Surrounding it with a block comment fails because TextWrangler cannot find the ‘shebang’ line.

%{
#!/Applications/LilyPond.app/Contents/Resources/bin/lilypond -o $1
%}

An alternative is to use Smultron 3, which lets you define commands that you can run with a keyboard shortcut.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文