在Terminal.app中查找一个单词


19

是否有bash或applescript来查找单词 /Applications/Dictionary.app 从终端窗口?

open -a /Applications/Dictionary.app/ --args word

忽略--args,说“键入一个单词来查找”

Mac字典改进 提示 ^控制 ⌘命令 d 但我希望推出完整的应用程序,而不仅仅是小型popover。


您只需在弹出窗口中单击字典的名称,而不是“更多”按钮,即可在字典应用程序中打开搜索。
— gentmatt

Answers:


20

您可以使用...

open dict://my_word

...将打开Dictionary应用程序并查找字符串 my_word。如果你想使用多个单词,请使用类似的单词 open dict://"Big Bang Theory"。

但终端没有输出。


谢谢。是否有开放的magicprefix列表:......某个地方?
— denis

@Denis我不知道专门收集未记录的命令选项的源 open。但一般来说, hints.macworld.com 是一个众所周知的隐藏宝石来源。我也曾经知道收集无证件的不同来源 defaults write 命令,但我不记得它只是知道谷歌并没有帮助我...
— gentmatt

我做了一个简短的总结 open 在SuperUser前一段时间 superuser.com/questions/4368/os-x-equivalent-of-windows-run-box/...
— Josh Hunt

@denis系统维护一个数据库,其中包含所有安装的应用程序告诉它如何处理的所有前缀。如果你能想到知道这个花絮的实际用途,那么问一个完整的问题会很棒。
— bmike

18

使用Python Objective-C绑定,您可以创建一个小的python脚本,以从内置的OS X Dictionary中获取它。 这是一篇文章 详细说明这个脚本“

#!/usr/bin/python

import sys
from DictionaryServices import *

def main():
    try:
        searchword = sys.argv[1].decode('utf-8')
    except IndexError:
        errmsg = 'You did not enter any terms to look up in the Dictionary.'
        print errmsg
        sys.exit()
    wordrange = (0, len(searchword))
    dictresult = DCSCopyTextDefinition(None, searchword, wordrange)
    if not dictresult:
        errmsg = "'%s' not found in Dictionary." % (searchword)
        print errmsg.encode('utf-8')
    else:
        print dictresult.encode('utf-8')

if __name__ == '__main__':
    main()

保存到 dict.py,然后运行 python dict.py dictation

enter image description here

查看帖子 有关使其可在终端上访问的更多说明。


1
我使用了这个脚本,但输出中没有换行符,它看起来像这样: i.imgur.com/ooAwQCA.png (在OS X 10.9上)。
— h__

我的输出中也没有换行符。检查 print repr(dictresult.encode('utf-8')) 告诉我这个: 'dictation |d\xc9\xaak\xcb\x88te\xc9\xaa\xca\x83(\xc9\x99)n| \xe2\x96\xb6noun [ mass noun ] 1 the action of dictating words to be typed, written down, or recorded on tape: the dictation of letters. \xe2\x80\xa2 the activity of taking down a passage that is dictated by a teacher as a test of spelling, writing, or language skills: passages for dictation. \xe2\x80\xa2 words that are dictated: the job will involve taking dictation, drafting ...'
— nnn

我添加了一些字符串替换来模拟换行符..虽然我没有广泛测试它似乎工作正常: gist.github.com/lambdamusic/bdd56b25a5f547599f7f
— magicrebirth

这似乎不再起作用了。
— Toothrot

4

我也打算建议 open dict://word,但谷歌的词典API也使用新牛津美国词典:

#!/usr/bin/env ruby

require "open-uri"
require "json"
require "cgi"

ARGV.each { |word|
  response = open("http://www.google.com/dictionary/json?callback=dict_api.callbacks.id100&q=#{CGI.escape(word)}&sl=en&tl=en&restrict=pr,de").read
  results = JSON.parse(response.sub(/dict_api.callbacks.id100\(/, "").sub(/,200,null\)$/, ""))
  next unless results["primaries"]
  results["primaries"][0]["entries"].select { |e| e["type"] == "meaning" }.each { |entry|
    puts word + ": " + entry["terms"][0]["text"].gsub(/x3c\/?(em|i|b)x3e/, "").gsub("x27", "'")
  }
}

1
Google API已弃用并返回404.看起来像 dictionaryapi.com 可以工作,只需要登录。
— Sam Berry

4

我找到了使用Swift 4的解决方案。

#!/usr/bin/swift
import Foundation

if (CommandLine.argc < 2) {
    print("Usage: dictionary word")
}else{
    let argument = CommandLine.arguments[1]
    let result = DCSCopyTextDefinition(nil, argument as CFString, CFRangeMake(0, argument.count))?.takeRetainedValue() as String?
    print(result ?? "")
}
  1. 保存为 dict.swift
  2. 添加权限 chmod +x dict.swift
  3. 查找字典
    • 与翻译一起运行 ./dict.swift word
    • 由编译器构建 swiftc dict.swift 并运行 ./dict word

2

来自David Perace的更新代码回答,添加一些颜色和新行:

#!/usr/bin/python
# -*- coding: utf-8 -*-

import sys
import re
from DictionaryServices import *

class bcolors:
    HEADER = '\033[95m'
    OKBLUE = '\033[94m'
    OKGREEN = '\033[92m'
    WARNING = '\033[93m'
    FAIL = '\033[91m'
    ENDC = '\033[0m'
    BOLD = '\033[1m'
    UNDERLINE = '\033[4m'

def main():
    try:
        searchword = sys.argv[1].decode('utf-8')
    except IndexError:
        errmsg = 'You did not enter any terms to look up in the Dictionary.'
        print errmsg
        sys.exit()
    wordrange = (0, len(searchword))
    dictresult = DCSCopyTextDefinition(None, searchword, wordrange)
    if not dictresult:
        errmsg = "'%s' not found in Dictionary." % (searchword)
        print errmsg.encode('utf-8')
    else:
        result = dictresult.encode('utf-8')
        result = re.sub(r'\|(.+?)\|', bcolors.HEADER + r'/\1/' + bcolors.ENDC, result)
        result = re.sub(r'▶', '\n\n ' + bcolors.FAIL + '▶ ' + bcolors.ENDC, result)
        result = re.sub(r'• ', '\n   ' + bcolors.OKGREEN + '• ' + bcolors.ENDC, result)
        result = re.sub(r'(‘|“)(.+?)(’|”)', bcolors.WARNING + r'“\2”' + bcolors.ENDC, result)
        print result

if __name__ == '__main__':
    main()




0

尝试 字典OSX (我在坚持使用其他答案并想要一个非Python解决方案后做到了这一点)。它使用来自的定义 Dictionary.app。

dictionary cat
# cat 1 |kat| ▶noun 1 a small domesticated carnivorous mammal with soft fur...

它用 DictionaryKit ,OSX上可用的私有字典服务的包装器。有关这是如何工作的有趣的背景信息 NSHipster 。

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.