简体   繁体   English

如何在Mac OS X的Notification Center中添加变量?

[英]How can I add a variables into Notification Center on Mac OS X?

I have managed to cobble this together: 我设法把这个拼凑起来:

You can run it in your Terminal, it is harmless, and will output results only to notification center: 您可以在终端中运行它,它是无害的,并且仅将结果输出到通知中心:

osascript -e 'display notification "The url(s) are on your clipboard from Droppings.app" with title "Droppings has finished sharing your file(s)" subtitle "Some Some title"'

Ideally, I want to add pbpaste in the "Some title" section, but I get the literal string. 理想情况下,我想在“某些标题”部分中添加pbpaste ,但是我得到了文字字符串。 I tried creating a variable, and yet still get the literal string instead. 我尝试创建一个变量,但仍然得到原义字符串。

I tried building the entire string up as one variable, which in the Terminal, echo'd out works, but in Notification Center, I get the actually literal variable as a string. 我尝试将整个字符串构建为一个变量,该变量在终端中回显了,但在Notification Center中,我实际上将文字变量作为字符串。

Suggestions? 建议?

I find this syntax quite easy rather than single-exited, double-double quoted contortions: 我发现此语法非常简单,而不是单引号,双引号引起的扭曲:

echo pbcopiedtext | pbcopy

echo display notification \"Message is $(pbpaste)\" with title \"Funky Title\"  | osascript

In the shell, anything within single quotes ( ' ) is taken verbatim. 在shell中,单引号( ' )内的所有内容均按原样使用。 No substitutions are performed. 不执行替换。

You can substitute the pasteboard contents into the string using something like this: 您可以使用以下方式将粘贴板内容替换为字符串:

osascript -e 'display notification "The url(s) are on your clipboard from Droppings.app" with title "Droppings has finished sharing your file(s)" subtitle "Some title '"$(pbpaste)"'"'

I end the single quote, open a double quote (so the contents of the pasteboard will be considered part of the single argument even if it contains spaces), substitute the output of the pbpaste command, close the double quote, re-open the single quote, and resume with the rest of the string. 我将单引号结尾,打开双引号(因此,粘贴板的内容即使包含空格也将被视为单参数的一部分),替换为pbpaste命令的输出,关闭双引号,然后重新打开单引号引用,然后继续其余的字符串。

This works, except that if the pasteboard contents contains stuff that osascript (ie AppleScript) treats as special, like quote characters, that can affect how osascript parses the resulting text. 这是osascript ,除了如果粘贴板内容包含osascript (即AppleScript)将其视为特殊字符(例如引号字符),这会影响osascript解析结果文本的方式。 To fix that, you'll have to escape the pasteboard contents to be safe against interpretation by AppleScript, which is beyond the scope of this question. 要解决此问题,您必须转义粘贴板内容,以确保可以防止AppleScript进行解释,这不在此问题的范围内。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM