Using dictionary from command line
I work in the terminal most of the times and I am just wondering if I could get all the information in terminal itself.
Any ideas? Thanks!
Message was edited by: kt.kthapar
iMac 27' Quadcore, Mac OS X (10.6.4)
You can make a difference in the Apple Support Community!
When you sign up with your Apple Account, you can provide valuable feedback to other community members by upvoting helpful replies and User Tips.
When you sign up with your Apple Account, you can provide valuable feedback to other community members by upvoting helpful replies and User Tips.
iMac 27' Quadcore, Mac OS X (10.6.4)
Linc Davis wrote:
you could install the POSIX 'dict' command (e.g. from MacPorts) which is a client for online dictionaries.
The suggestion given above launches Dictionary.app, which is apparently not what you want.
// insert code here...
NSLog(@"Hello, World!");
if(argc < 2)
{
printf("Usage: dict <word to define> ");
return -1;
}
NSString * search =
[NSString stringWithCString: argv[1] encoding: NSUTF8StringEncoding];
CFStringRef def =
DCSCopyTextDefinition(NULL,
(CFStringRef)search,
CFRangeMake(0, [search length]));
NSString * output =
[NSString
stringWithFormat:
@"Definition of <%@>: %@", search, (NSString *)def];
printf("%s", [output UTF8String]);
VikingOSX wrote:
One could put the following code in their ~/.bashrc file as a shell function.
dict () {
curl dict://dict.org/d:"${1}"
}
Then:
source .bashrc
dict Apple | more
For those considering this, note that
i. it requires an internet connection to work
ii. it'll be as fast (or slow) as your connection + the server respond time.
While it's an interesting way to do it, it seems wasteful to go online to query a dictionary when there's one instantly accessible sitting on your hard drive.
Phil,
It was another approach to the post theme. Personally, I keep the Apple Dictionary in my Dock, and use it often. As always, there are multiple solution forms, and individual workflow determination comes into play.
Sure, as I said I thought the solution was quite interesting from a technical point of view. Always good to know of different ways to skin a cat. I was just pointing out some practical drawbacks is all. 🙂
In fact my own workflow is your second suggestion. Accessing Spotlight from the keyboard is a very fast way to accomplish a lot of things in OS X, not just the dictionary.
Phil Stokes wrote:
For those considering this, note that
i. it requires an internet connection to work
So, if you don't have an internet connection, you wouldn't be reading this 🙂
Anyway, a better note is not to use any scripts that you don't understand
(i.e., if you don't know what curl is, then man curl before using)
Tony T1 wrote:
So, if you don't have an internet connection, you wouldn't be reading this 🙂
If you only own a desktop workstation I can appreciate that you'd miss the point. However, by far the majority of units that Apple sell are portables. Unlike air, the internet is not everywhere, and that's an important consideration for notebook users (I'll forget that you totally overlooked the response time issue...).
Anyway, a better note is not to use any scripts that you don't understand
Why are scripts a special case from any other software? If people were required to understand how every utility they use on their computer actualy works, then they'd be able to use very little, and software developers wouldn't have much of a market.
There's no reason why every user has to understand how every script on their computer works. What matters is at best that the user understands what is needed to make it work and what its major limitations are. In this case, as I pointed out, that's the internet and response time, respectively.
Phil Stokes wrote:
If you only own a desktop workstation I can appreciate that you'd miss the point.
There's no reason why every user has to understand how every script on their computer works.
The point was that before using a script posted in any forum, it's best understand what it does, Capisce? (that an Internet connection is needed for curl is sort of useless information if your not using a script from a trusted source)
Great. All you need to do now is tell the OP how to do that in the command line (you might want to review the thread title there)...
Here, I got you started:
Still no word on comparative response times...maybe you have Google fiber and no data cap. 😁
No I don't "capisce" cos we're not in an episode of the Sopranos.
However, at last we do agree on something. Indeed, etresoft's was the best answer because its a command line utility that uses local resources and doesn't rely on a possibly non-existent or flakey internet connection.
I've just been toying with your pride since then.
Here is the way I like the most. It uses Princeton University's Wordnet(https://wordnet.princeton.edu/) database:
1) If you don't have it already, get MacPorts(https://www.macports.org/)
2) afterwards, get the port wn and its dependencies - sudo port install wn
3) then, just type 'wn <word you want to look up> -over' in the terminal and then press enter
4) you will then have something like this in the terminal:
Overview of adj ambiguous
The adj ambiguous has 3 senses (first 3 from tagged texts)
1. (9) equivocal, ambiguous -- (open to two or more interpretations; or of uncertain nature or significance; or (often) intended to mislead; "an equivocal statement"; "the polling had a complex and equivocal (or ambiguous) message for potential female candidates"; "the officer's equivocal behavior increased the victim's uneasiness"; "popularity is an equivocal crown"; "an equivocal response to an embarrassing question")
2. (4) ambiguous -- (having more than one possible meaning; "ambiguous words"; "frustrated by ambiguous instructions, the parents were unable to assemble the toy")
3. (1) ambiguous -- (having no intrinsic or objective meaning; not organized in conventional patterns; "an ambiguous situation with no frame of reference"; "ambiguous inkblots")
Oh, did I forget to mention that wn can be used as a thesaurus? Well, it most certainly can! In the terminal, type 'wn <word you want to look up> -antsa'
*type '-ants(n)oun | (v)erb | (a)djective | (r)adverb' to look up antonyms of a word
*type '-syns(n)oun | (v)erb | (a)djective | (r)adverb' to look up synonyms of a word
For more information about what you can do with wn, type 'man wn' in the terminal. Have fun! 😎
You should understand that even if you have syntax checked your Python code, and there are absolutely no PEP errors indicated, the Jive hosting software in the Support Community will jack your indentation around and copy/pasting it will result in syntax errors while running it in the Terminal after its visit to TextEdit. The latter is not a programmer's editor and it may actually introduce additional code indentation and encoding issues.
The same code, when copied and pasted from your post into SublimeText 3 using its Paste and Indent menu item, fixed all but two indentation errors caused by Jive. Since I use the Anaconda syntax checking package for Sublime Text 3, I can see all of the Python PEP syntax errors that are flagged in the code with English language reason, and watch them disappear as I fix them. After running your code through the Sublime Text 3 editor, it now works without any errors on El Capitan 10.11.1.
You do not need to explicitly refer to Python 2.7. Just use Python and the most recently installed version will be used. As of El Capitan, Apple does not provide Python 3. It would be useful to include those versions of OS X where you have tested your code.
Looks like tabs and newlines are primarily the risk of retrieving usable code that is indentation dependent. Good that you are using Sublime Text though.
One can replace tabs with exactly four spaces (for posting) by adapting a Sublime Text 3 Preference setting in Settings-User. The following would apply as the last two lines in the user settings file.
"tab_size": 4,
"translate_tabs_to_spaces": true
Ideally, one would create language specific settings files named (e.g. Python.sublime-settings) in the following location:
~/Library/Application Support/Sublime Text 3/Packages/User
PEP 8 E303 is the reason for the main function in your image getting lit up. Wants exactly two blank lines preceding each function definition. The hosting software added one on you. It would also appear that an extra trailing space was added too. Python wants a newline at the end of the file, which is why your line 25 was lit up by a PEP 8 W292.
Hello
I know this is so very late reply but anyway here's my version of pyobjc code which works under OS X 10.6.8. Since pyobjc bridgesupport file for DictionaryServices.framework is broken under OS X 10.6.8, fixed bridgesupport metadata is loaded manually here. Don't know whether this should work under later OSes.
#!/usr/bin/python # coding: utf-8 import sys, objc from CoreFoundation import * DCS_BRIDGESUPPORT = '''<?xml version="1.0" standalone="yes"?> <!DOCTYPE signatures SYSTEM "file://localhost/System/Library/DTDs/BridgeSupport.dtd"> <signatures version="0.9"> <function name="DCSCopyTextDefinition"> <arg type="^{__DCSDictionary=}"></arg> <arg type="^{__CFString=}"></arg> <arg type64="{_CFRange=qq}" type="{_CFRange=ii}"></arg> <retval type="^{__CFString=}"></retval> </function> </signatures>''' objc.parseBridgeSupport( DCS_BRIDGESUPPORT, globals(), objc.pathForFramework('/System/Library/Frameworks/CoreServices.framework/Frameworks/DictionaryServices.framework') ) for q in [ e.decode('utf-8') for e in sys.argv[1:] ]: r = DCSCopyTextDefinition(None, q, (0, len(q))) print '[%s]\n%s' % tuple( [e.encode('utf-8') for e in [q, r or 'nil\n']] )
Tested with pyobjc 2.2b3 and python 2.6.1 under OS X 10.6.8.
Cheers,
H
And the output on El Capitan 10.11.1 with Python 2.7.10 and standard Apple PyObjc scripting bridge. Appropriate Dictionaries enabled in the Dictionary app preferences.
odin: ~$ hiroto2.py hippopotamus rhinoceros 人 佛
[hippopotamus]
hippopotamus |ˌhɪpəˈpɑdəməs| ▶noun (pl.
hippopotamuses or hippopotami || ) a large
thick-skinned semiaquatic African mammal, with
massive jaws and large tusks. [Family
Hippopotamidae: the very large Hippopotamus
amphibius, frequenting rivers and lakes, and the
smaller pygmy hippopotamus (Choeropsis
liberiensis), frequenting forests near fresh
water in West Africa.] ORIGIN Middle English:
via Latin from Greek hippopotamos, earlier
hippos ho potamios ‘river horse’ (from hippos
‘horse,’ potamos ‘river’).
[rhinoceros]
rhinoceros |raɪˈnɑs(ə)rəs| ▶noun (pl. same or
rhinoceroses) a large, heavily built plant-
eating mammal with one or two horns on the nose
and thick folded skin, native to Africa and
southern Asia. All kinds have become endangered
through hunting. [Family Rhinocerotidae: four
genera and five species.] ORIGIN Middle English:
via Latin from Greek rhinokerōs, from rhis,
rhin- ‘nose’ + keras ‘horn.’
[人]
人 rén①名指由类人猿进化而来的,能思维,能制造并使用工具进行劳动,并能进行语言交际的高等动物
。街上人多 | 男人 | 人类②→名指某种人。证人 | 军人 |
外国人③名指成年人。长大成人④→名别人;他人。舍己救人 |
助人为乐⑤→名指每个人或一般人。人手一册 | 人同此心⑥→名指人手或人才。学校很缺人 |
向社会公开招人⑦→名指人的品质、名声。老冯人很正直 | 丢人现眼⑧→名指人的身体。别把人累坏了
| 人在心不在
[佛]
佛 bì①古同“弼”②○名姓。用法说明除以上意义外,作某些人的名、字时,也读
bì,如佛肸、佛狸。另见 fó;fú。
Using dictionary from command line