是否有bash或applescript来查找单词 /Applications/Dictionary.app
从终端窗口?
open -a /Applications/Dictionary.app/ --args word
忽略--args,说“键入一个单词来查找”
Mac字典改进 提示 ^控制 ⌘命令 d 但我希望推出完整的应用程序,而不仅仅是小型popover。
是否有bash或applescript来查找单词 /Applications/Dictionary.app
从终端窗口?
open -a /Applications/Dictionary.app/ --args word
忽略--args,说“键入一个单词来查找”
Mac字典改进 提示 ^控制 ⌘命令 d 但我希望推出完整的应用程序,而不仅仅是小型popover。
Answers:
您可以使用...
open dict://my_word
...将打开Dictionary应用程序并查找字符串 my_word
。如果你想使用多个单词,请使用类似的单词 open dict://"Big Bang Theory"
。
但终端没有输出。
open
。但一般来说, hints.macworld.com 是一个众所周知的隐藏宝石来源。我也曾经知道收集无证件的不同来源 defaults write
命令,但我不记得它只是知道谷歌并没有帮助我...
open
在SuperUser前一段时间 superuser.com/questions/4368/os-x-equivalent-of-windows-run-box/...
使用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
查看帖子 有关使其可在终端上访问的更多说明。
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 ...'
我也打算建议 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", "'")
}
}
我找到了使用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 ?? "")
}
dict.swift
chmod +x dict.swift
./dict.swift word
swiftc dict.swift
并运行 ./dict word
来自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()
结帐这个github回购: https://github.com/aztack/osx-dictionary
安装: brew install https://raw.githubusercontent.com/takumakei/osx-dictionary/master/osx-dictionary.rb --HEAD
我发现这篇文章寻找类似的东西。不满意可用的选项,所以制作简单的脚本。它是一个基于终端的词库,具有文本到语音。可能有意义......
查看以下主题以了解如何在终端中使用Dictionary.app: https://discussions.apple.com/thread/2679911?start=0&tstart=0
尝试 字典OSX (我在坚持使用其他答案并想要一个非Python解决方案后做到了这一点)。它使用来自的定义 Dictionary.app
。
dictionary cat
# cat 1 |kat| ▶noun 1 a small domesticated carnivorous mammal with soft fur...
它用 DictionaryKit ,OSX上可用的私有字典服务的包装器。有关这是如何工作的有趣的背景信息 NSHipster 。