OS X 自带的词典

2016-08-26 更新:
Firefox 49beta6 又修复了 QuickLook 不能使用的问题,暌违大半年的功能又回归,感谢 Mozilla。

so,以下内容又可以先搁置 :D


2014-10-27 更新:

Firefox 34 已经支持系统的「查找」翻译单词了,赞。

so,以下内容愉快地废弃 :D


OS X 平台上,用过「有道词典」以及系统自带的「词典」。 前者功能更多一些,但启动速度则不如后者。 启动之后,系统的「词典」能自动把光标定位到「输入栏」里,可马上输入单词,使用场景对比:

  1. 启动「有道词典」,把鼠标移动到「输入栏」,点击一下,开始输入单词,回车获取结果
  2. 启动系统「词典」,直接输入单词,回车获取结果

除了玩游戏,平时只使用触摸板,第二种更适合我。

在上面的查询后,「重新」查询另一个单词,则:

  1. 把鼠标移动到「有道词典」的「输入栏」,点击一下,删除当前已有的单词,开始输入新的单词,回车获取结果
  2. ⌘+w关闭系统「词典」,重新打开,可立即开始输入新单词,回车获取结果。因为它的启动速度够快(两个软件都有启动的快捷键)但「有道词典」就不适合频繁重启,启动慢等待时间久

在原生Cocoa的软件内,开启了手势后可直接通过「三个手指」轻按一个单词来进行查找/翻译(系统偏好设置 - 触控板 - 光标与点按 - 查找)也可以先选择一个单词,快捷键⌃+⌘+d启动「查找」。 英语水平一般的我在阅读时经常使用这个小功能,非常方便。

但firefox不支持这个功能,曾经一度在阅读英文页面前,先把页面转到safari上打开了再看。

有一个firefox扩展Mac OS X Dictionary Add-On 1.1.1,算是曲线救国。 安装后,对一个单词右键一下,在「右键菜单」里有一个选项:Look Up xxx in Dictionary,通过它实现了在firefox里打开系统「词典」搜索该单词。但没有快捷键,想象一下两个场景:

  1. 把鼠标移动到某个单词,右键,选择Look up,系统「词典」自动开启,显示该单词查找/翻译结果。在「右键菜单」点击Look up的操作,有一个眼睛视线查找过程,感觉很不方便。我很少使用「右键菜单」
  2. 把鼠标移动到某个单词,双击该单词(此时选中它了),按一个快捷键,系统「词典」自动开启,显示结果

现在可以实现第二种操作了,直接使用了扩展Mac OS X Dictionary的部分js代码,实现两个功能:

  1. 获取当前页面「选中」的单词
  2. 打开系统「词典」查询该单词

另外需要为该功能添加一个快捷键,通过另一个firefox扩展keyconfig,为上面的功能分配一个快捷键⌂+⌘+d,该快捷键不能跟系统/firefox已有的冲突。



第一步,在firefox地址栏里输入about:support打开support页面,在Application Basics - Profile Folder项目里,有一个Show in Finder的按钮,通过它打开firefox当前的配置文件夹。在extensions目录下可找到已安装的macosxdictionary扩展,复制到其他地方。

.
└── uldw6wyn.default
   ├── extensions
   │   ├── macosxdictionary@micheldalal.com.xpi

macosxdictionary@micheldalal.com.xpi的xpi后缀修改为zip,解压,按如下路径找到macosxdictionary.js,它里面包含了实现第一步的js代码,我直接拿来使用了。

macosxdictionary@micheldalal.com
├── defaults
│   └── preferences
│       └── macosxdictionary.js

提取的js代码如下,在下一步的keyconfig中将会使用到它。

function getSelectedText()
{
    var node, focusedWindow;
    var selection;

    selection = "";
    node = document.popupNode;
    if ( ( node instanceof HTMLTextAreaElement ) || ( node instanceof HTMLInputElement && node.nodeType == Node.TEXT_NODE ) )
    {
        selection = node.value.substring( node.selectionStart, node.selectionEnd );
    }
    else
    {
        focusedWindow = new XPCNativeWrapper( document.commandDispatcher.focusedWindow, 'document', 'getSelection()' );
        selection = focusedWindow.getSelection().toString();
    }

    selection = selection.replace(/(\n|\r|\t|(\r\n))+/g, " ");

    // Trim the value(remove the space at the beginning and the end)
    selection = selection.replace(/(^\s+)|(\s+$)/g, "");
    selection = selection.split(" ");
    selection = selection.join(" ");

    return selection;
}

var selectedText = getSelectedText();

// Open the dictionary application

// create an nsILocalFile for the executable
var file = Components.classes[ "@mozilla.org/file/local;1" ].createInstance( Components.interfaces.nsILocalFile );
file.initWithPath( "/usr/bin/open" );

// create an nsIProcess
var process = Components.classes[ "@mozilla.org/process/util;1" ].createInstance( Components.interfaces.nsIProcess );
process.init(file);

// Run the process.
// If first param is true, calling thread will be blocked until
// called process terminates.
// Second and third params are used to pass command-line arguments
// to the process.
var args = [ "dict:///" + selectedText ];
process.run(false, args, args.length);

每种文件有一个默认的打开app,/usr/bin/open会调用默认的app来打开该文件。"dict:///" + selectedText则告诉系统「词典」要对selectedText进行翻译。



安装了keyconfig扩展后,打开它的配置。

  1. 选择Add a new key,弹出的输入框中,Name输入该快捷键的名字,比如0Look up in Dictionary, 名字前添加一个数字,方便查找。
  2. /* CODE */这里,粘贴上面的js代码,点击ok保存。
  3. 默认是没有快捷键,显示为<Disabled>(在Apply左边)输入快捷键,比如⌂+⌘+d,Apply保存一下

回到firefox后就可以愉快地:

  1. 双击选中一个单词,⌂+⌘+d,在弹出的「词典」中查看翻译
  2. ⌘+w关闭词典

伸手党万岁。。。强迫症党万岁。。。



link:

  1. Mac OS X Dictionary Add-On 1.1.1
  2. keyconfig



– EOF –

Categories: mac
Tags: mac