-
-
Save lambdamusic/bdd56b25a5f547599f7f to your computer and use it in GitHub Desktop.
#!/usr/bin/env python | |
""" | |
# Version | |
2021-08-31 | |
# Tested on | |
Python 3.9 | |
# Originally Modified from | |
http://macscripter.net/viewtopic.php?id=26675 | |
http://apple.stackexchange.com/questions/90040/look-up-a-word-in-dictionary-app-in-terminal | |
# HowTo | |
chmod 755 define.py # make file executable | |
alias define="'/Users/me/Script/define.py'" # => add to .bash_profile | |
-> then (from command line) | |
define recursive # or any other word | |
-> output .. | |
Dictionary entry for: | |
recursive | rɪˈkəːsɪv | adjective characterized by recurrence or repetition. | |
• Mathematics & Linguistics relating to or involving the repeated application of a rule, definition, or procedure to successive results: this restriction ensures that the grammar is recursive. | |
• Computing relating to or involving a program or routine of which a part requires the application of the whole, so that its explicit interpretation requires in general many successive executions: a recursive subroutine. | |
DERIVATIVES | |
recursively | rɪˈkəːsɪvli | adverb | |
ORIGIN | |
late 18th century (in the general sense): from late Latin recurs- ‘returned’ (from the verb recurrere ‘run back’) + -ive. Specific uses have arisen in the 20th century. | |
--------------------------------- | |
""" | |
import sys | |
try: | |
from DictionaryServices import * | |
except: | |
print("WARNING: The pyobjc Python library was not found. You can install it by typing: 'pip install -U pyobjc'") | |
print("..................\n") | |
try: | |
from colorama import Fore, Back, Style | |
except: | |
print("WARNING: The colorama Python library was not found. You can install it by typing: 'pip install colorama'") | |
print("..................\n") | |
def main(): | |
""" | |
define.py | |
Access the default OSX dictionary | |
2021-08-31 | |
-improved so to work with Python 3.9 | |
2015-11-27 | |
-added colors via colorama | |
""" | |
try: | |
searchword = sys.argv[1] | |
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) | |
else: | |
# print(dictresult) | |
s = dictresult | |
#split numbered items up to ten (hackish..) | |
for n in reversed(range(1, 11)): | |
new_n = doColor(f"\n{str(n)} ", "red") | |
s = s.replace(f" {str(n)} ", new_n) | |
for x in ["noun", "adverb", "adjective"]: | |
particle = doColor(x, "green") | |
s = s.replace(x, particle) | |
bullet = doColor("\n •", "red") | |
s = s.replace('•', bullet) # bullet | |
phrases_header = doColor("\n\nPHRASES\n", "important") | |
s = s.replace('PHRASES', phrases_header) | |
phrasal_verbs_header = doColor("\n\nPHRASAL VERBS\n", "important") | |
s = s.replace('PHRASAL VERBS', phrasal_verbs_header) | |
derivatives_header = doColor("\n\nDERIVATIVES\n", "important") | |
s = s.replace('DERIVATIVES', derivatives_header) | |
origin_header = doColor("\n\nORIGIN\n", "important") | |
s = s.replace('ORIGIN', origin_header) | |
print(doColor("Dictionary entry for:\n", "red")) | |
print(s) | |
print("\n---------------------------------") | |
def doColor(s, style=None): | |
""" | |
util for returning a colored string | |
if colorama is not installed, FAIL SILENTLY | |
""" | |
try: | |
if style == "comment": | |
s = Style.DIM + s + Style.RESET_ALL | |
elif style == "important": | |
s = Style.BRIGHT + s + Style.RESET_ALL | |
elif style == "normal": | |
s = Style.RESET_ALL + s + Style.RESET_ALL | |
elif style == "red": | |
s = Fore.RED + s + Style.RESET_ALL | |
elif style == "green": | |
s = Fore.GREEN + s + Style.RESET_ALL | |
except: | |
pass | |
return s | |
if __name__ == '__main__': | |
main() |
Can we query non-osx dictionary? I have a ton of custom dictionaries.
After pip install pyobjc
I get the following installed:
pip list
pyobjc 8.5.1 pyobjc-core 8.5.1 pyobjc-framework-Accessibility 8.5.1 pyobjc-framework-Accounts 8.5.1 pyobjc-framework-AddressBook 8.5.1 pyobjc-framework-AdServices 8.5.1 pyobjc-framework-AdSupport 8.5.1 pyobjc-framework-AppleScriptKit 8.5.1 pyobjc-framework-AppleScriptObjC 8.5.1 pyobjc-framework-ApplicationServices 8.5.1 pyobjc-framework-AppTrackingTransparency 8.5.1 pyobjc-framework-AudioVideoBridging 8.5.1 pyobjc-framework-AuthenticationServices 8.5.1 pyobjc-framework-AutomaticAssessmentConfiguration 8.5.1 pyobjc-framework-Automator 8.5.1 pyobjc-framework-AVFoundation 8.5.1 pyobjc-framework-AVKit 8.5.1 pyobjc-framework-BusinessChat 8.5.1 pyobjc-framework-CalendarStore 8.5.1 pyobjc-framework-CallKit 8.5.1 pyobjc-framework-CFNetwork 8.5.1 pyobjc-framework-ClassKit 8.5.1 pyobjc-framework-CloudKit 8.5.1 pyobjc-framework-Cocoa 8.5.1 pyobjc-framework-Collaboration 8.5.1 pyobjc-framework-ColorSync 8.5.1 pyobjc-framework-Contacts 8.5.1 pyobjc-framework-ContactsUI 8.5.1 pyobjc-framework-CoreAudio 8.5.1 pyobjc-framework-CoreAudioKit 8.5.1 pyobjc-framework-CoreBluetooth 8.5.1 pyobjc-framework-CoreData 8.5.1 pyobjc-framework-CoreHaptics 8.5.1 pyobjc-framework-CoreLocation 8.5.1 pyobjc-framework-CoreMedia 8.5.1 pyobjc-framework-CoreMediaIO 8.5.1 pyobjc-framework-CoreMIDI 8.5.1 pyobjc-framework-CoreML 8.5.1 pyobjc-framework-CoreMotion 8.5.1 pyobjc-framework-CoreServices 8.5.1 pyobjc-framework-CoreSpotlight 8.5.1 pyobjc-framework-CoreText 8.5.1 pyobjc-framework-CoreWLAN 8.5.1 pyobjc-framework-CryptoTokenKit 8.5.1 pyobjc-framework-DataDetection 8.5.1 pyobjc-framework-DeviceCheck 8.5.1 pyobjc-framework-DictionaryServices 8.5.1 pyobjc-framework-DiscRecording 8.5.1 pyobjc-framework-DiscRecordingUI 8.5.1 pyobjc-framework-DiskArbitration 8.5.1 pyobjc-framework-DVDPlayback 8.5.1 pyobjc-framework-EventKit 8.5.1 pyobjc-framework-ExceptionHandling 8.5.1 pyobjc-framework-ExecutionPolicy 8.5.1 pyobjc-framework-ExternalAccessory 8.5.1 pyobjc-framework-FileProvider 8.5.1 pyobjc-framework-FileProviderUI 8.5.1 pyobjc-framework-FinderSync 8.5.1 pyobjc-framework-FSEvents 8.5.1 pyobjc-framework-GameCenter 8.5.1 pyobjc-framework-GameController 8.5.1 pyobjc-framework-GameKit 8.5.1 pyobjc-framework-GameplayKit 8.5.1 pyobjc-framework-ImageCaptureCore 8.5.1 pyobjc-framework-IMServicePlugIn 8.5.1 pyobjc-framework-InputMethodKit 8.5.1 pyobjc-framework-InstallerPlugins 8.5.1 pyobjc-framework-InstantMessage 8.5.1 pyobjc-framework-Intents 8.5.1 pyobjc-framework-IntentsUI 8.5.1 pyobjc-framework-IOSurface 8.5.1 pyobjc-framework-iTunesLibrary 8.5.1 pyobjc-framework-KernelManagement 8.5.1 pyobjc-framework-LatentSemanticMapping 8.5.1 pyobjc-framework-LaunchServices 8.5.1 pyobjc-framework-libdispatch 8.5.1 pyobjc-framework-LinkPresentation 8.5.1 pyobjc-framework-LocalAuthentication 8.5.1 pyobjc-framework-LocalAuthenticationEmbeddedUI 8.5.1 pyobjc-framework-MailKit 8.5.1 pyobjc-framework-MapKit 8.5.1 pyobjc-framework-MediaAccessibility 8.5.1 pyobjc-framework-MediaLibrary 8.5.1 pyobjc-framework-MediaPlayer 8.5.1 pyobjc-framework-MediaToolbox 8.5.1 pyobjc-framework-Metal 8.5.1 pyobjc-framework-MetalKit 8.5.1 pyobjc-framework-MetalPerformanceShaders 8.5.1 pyobjc-framework-MetalPerformanceShadersGraph 8.5.1 pyobjc-framework-MetricKit 8.5.1 pyobjc-framework-MLCompute 8.5.1 pyobjc-framework-ModelIO 8.5.1 pyobjc-framework-MultipeerConnectivity 8.5.1 pyobjc-framework-NaturalLanguage 8.5.1 pyobjc-framework-NetFS 8.5.1 pyobjc-framework-Network 8.5.1 pyobjc-framework-NetworkExtension 8.5.1 pyobjc-framework-NotificationCenter 8.5.1 pyobjc-framework-OpenDirectory 8.5.1 pyobjc-framework-OSAKit 8.5.1 pyobjc-framework-OSLog 8.5.1 pyobjc-framework-PassKit 8.5.1 pyobjc-framework-PencilKit 8.5.1 pyobjc-framework-Photos 8.5.1 pyobjc-framework-PhotosUI 8.5.1 pyobjc-framework-PreferencePanes 8.5.1 pyobjc-framework-PushKit 8.5.1 pyobjc-framework-Quartz 8.5.1 pyobjc-framework-QuickLookThumbnailing 8.5.1 pyobjc-framework-ReplayKit 8.5.1 pyobjc-framework-SafariServices 8.5.1 pyobjc-framework-SceneKit 8.5.1 pyobjc-framework-ScreenCaptureKit 8.5.1 pyobjc-framework-ScreenSaver 8.5.1 pyobjc-framework-ScreenTime 8.5.1 pyobjc-framework-ScriptingBridge 8.5.1 pyobjc-framework-SearchKit 8.5.1 pyobjc-framework-Security 8.5.1 pyobjc-framework-SecurityFoundation 8.5.1 pyobjc-framework-SecurityInterface 8.5.1 pyobjc-framework-ServiceManagement 8.5.1 pyobjc-framework-ShazamKit 8.5.1 pyobjc-framework-Social 8.5.1 pyobjc-framework-SoundAnalysis 8.5.1 pyobjc-framework-Speech 8.5.1 pyobjc-framework-SpriteKit 8.5.1 pyobjc-framework-StoreKit 8.5.1 pyobjc-framework-SyncServices 8.5.1 pyobjc-framework-SystemConfiguration 8.5.1 pyobjc-framework-SystemExtensions 8.5.1 pyobjc-framework-UniformTypeIdentifiers 8.5.1 pyobjc-framework-UserNotifications 8.5.1 pyobjc-framework-UserNotificationsUI 8.5.1 pyobjc-framework-VideoSubscriberAccount 8.5.1 pyobjc-framework-VideoToolbox 8.5.1 pyobjc-framework-Virtualization 8.5.1 pyobjc-framework-Vision 8.5.1 pyobjc-framework-WebKit
Does this make sense if I only need the DIctionaryService
? It seems that all python objects are installed and most of em never used. The dic now works.
pip freeze | grep pyobjc-framework | xargs pip uninstall -y
will be sensible.
pyobjc-framework-CoreServices
and macfsevents
did not help to make the dic work.
Try installing both
pip3 install pyobjc-framework-CoreServices
pip3 install pyobjc-framework-DictionaryServices
I cleaned up the script and tested it on python 3.9 - the formatting is not perfect, but it works