Larry,Moe,Curly

在“短和Curlies” yesterday, I posted a short AppleScript for BBEdit (and Mailsmith) that converts curly quotes, ellipses, and en- and em-dashes to their corresponding plain ASCII equivalents (three dots for an ellipsis, two hyphens for an em-dash, etc.).

Dan Pouliot通过电子邮件询问为什么我没有使用BBEdit的“转换为ASCII”插件编写脚本答案:因为我忘了转换为ASCII插件是可编写脚本的。

我原来的14行脚本可以只用三行代替:

tell application "BBEdit"
    convert to ASCII with change in place and selection only
end tell

变化到位只选择options give the script the same behavior as my original script: if there is a selection, only the selected text will be converted; if there’s no selection, the entire front window will be converted此外,新脚本还具有以下优点:

  • 它更快The old script wasn’t slow, per se, but the repeated search-and-replace operations took a fraction of a second eachThe Convert to ASCII plug-in works nearly instantaneously.

  • 它不会搞砸你的撤销堆栈当原始脚本运行时,每个搜索和替换操作都会计入BBEdit的多个撤消堆栈中的单独操作因此,在运行脚本之后,您需要撤消五次才能返回到运行脚本之前的位置转换为ASCII命令是原子的,因此您只需按一次撤消即可。

  • 它将更多字符转换为ASCII原始的只有转换的引号,em-和en-dashes以及省略号转换为ASCII命令可以转换您可以抛出的任何字符Trademark™和copyright©符号转换为“(TM)”和“(c)”像“é”这样的重音字符会转换为非重音字符等等。

另请注意,只需更改,即可在Mailsmith中使用相同的脚本告诉申请目标是“Mailsmith”。

将文本翻译为HTML

好的,现在我们有了一个按键式解决方案,用于将曲线引号转换为纯ASCIIBut what if you don’t want plain ASCII, but instead you want to convert non-ASCII characters to their corresponding HTML entities? Instead of, say, turning literal curly quotes into straight quotes, you want to turn them into curly quote HTML entities.

BBEdit的Translate命令就是答案在菜单中,它是“Markup→Utilities→Translate ...”,但它非常有用,你需要记住键盘快捷键:默认情况下,Command-Option-T。

翻译对话框的屏幕截图。

Translate命令是如此有用,如此方便,以至于像你这样的旧BBEdit用户很容易忘记许多人可能不知道它The Translate command works in both directions: “Text to HTML” turns non-ASCII characters into entities; “HTML to Text” translates entities back to normal characters.

将文本翻译为HTML时,打开“Paragraphs”复选框会告诉BBEdit换行<P>tags around each block of lines如果您的输入已经关闭,请将其关闭<P>标签Likewise, if your input text already has HTML tags of any kind, turn on the “Ignore < and >” checkbox; otherwise, the angle brackets will be translated into&LT;&GT;实体。

If you apply the same Translate settings on a regular basis, you can skip the dialog box by creating an AppleScript to perform the translation不幸的是,与许多BBEdit的Markup命令一样,Translate不可记录但它的语法相当简单这是我在编写HTML时经常使用的脚本(包括我编写Fireball时):

tell application "BBEdit"
    translate text to html entity encoding method entity name value ¬
        with entity conversion, ignore angle brackets and selection only ¬
        without create new document and paragraph conversion
end tell

实体编码方法实体名称值“告诉BBEdit使用命名的HTML实体,例如&贸易;用于商标符号如果您想要十进制或十六进制编码的实体(对于商标符号将产生&#8482;&#x2122;,分别),只需指定实体十进制值要么实体十六进制值代替实体名称值

接下来的两行设置命令的布尔选项我绝对想要实体转换因为这是剧本的主要目的我想要忽略尖括号on also, because my text already contains HTML tags; I don’t want my angle brackets turned into&LT;&GT;实体。只选择将翻译限制为所选文本,但如果没有选择,则翻译整个文档。

我想要创建新文档off; when it’s on, the translated text goes to a new untitled document window instead of replacing the text in the current window最后,我不想要段落转换,因为我的标记已经包含<P>标签。

I’ve assigned this script the keyboard shortcut Shift-Command-Option-T; just like the shortcut for the built-in Translate command, plus the Shift key我用它所有的时间我承诺记忆的唯一实体值是最常见的我只需输入实际字符,而不是手动输入或使用BBEdit的HTML实体调色板例如,要生成版权实体,我键入一个真正的©符号(Option-g),然后运行我的脚本So instead of dealing with entities manually, I just type real characters, then run my script.

以前: 短和Curlies
下一个: 安装喜欢1991年