Forum Archive

Access to Apple standard language dictionaries?

cvp

Is it possible in Pythonista to get a word definition from the Apple standard language dictionaries?

omz

There's no API for that. You could only check if a word has a definition in the dictionary, and you could show the standard dictionary UI, but there's no access to the raw dictionary data.

cvp

The standard UI could perhaps is sufficient for me.
How can I try it?

lukaskollmer

If the default iOS dictionary lookup UI is acceptable for you, this script is probably what you're looking for.

cvp

Thanks a lot, it's very good for what I want.

cvp

On all my scripts, I start by "hiding" the Pythonista window with a full_screen ui.view.
But with this code, the standard UI dictionary is not visible.
How can I solve that?

cvp

What to add in your sample to set the ViewController as a sub view of an ui.view?

lukaskollmer

@cvp In theory you can, but it doesn't look nice at all and there are some big problems with navigation.

view = ui.load_view()

lookup_view = referenceViewController.view()
ObjCInstance(view).addSubview_(lookup_view)

view.present()

I wouldn't use that approach

MartinPacker

I think Workflow has a "define this" stage. Not sure how they do it. But maybe you could invoke it.

lukaskollmer

@MartinPacker They just show the UIReferenceLibraryViewController in a popover.

cvp

First, I know for Workflow, but I try to replace all my workflows by Pythonista scripts.

There is my shortest script using what you advice me.
If I comment all statements using "back" (my ui.view), all is ok.
If I uncomment them, except the "subview", the background is shown but not the dictionary window.
If I also uncomment the "subview" statement, Pythonista crashes and I fall on the home screen of the idevice...
Thus, obviously, my "ObjCInstance(back).addSubview_(referenceViewController)" is not correct.
I really need help and I'm sorry to ask you to spend your time for helping a starter like me.

X

Coding: utf-8
from objc_util import ObjCClass, UIApplication, CGSize, on_main_thread,ObjCInstance
import sys
import ui

UIReferenceLibraryViewController = ObjCClass('UIReferenceLibraryViewController')


back= ui.View()
back.background_color='gray'
back.name = 'Dictionary'    
back.present('full_screen',hide_title_bar=False)

input = 'word'
referenceViewController = UIReferenceLibraryViewController.alloc().initWithTerm_(input)

ObjCInstance(back).addSubview_(referenceViewController)

rootVC = UIApplication.sharedApplication().keyWindow().rootViewController()
tabVC = rootVC.detailViewController()

referenceViewController.setTitle_('Definition: {0}{1}{0}'.format('\'', input))
referenceViewController.setPreferredContentSize_(CGSize(540, 540))
referenceViewController.setModalPresentationStyle_(2)       
#tabVC.addTabWithViewController_(referenceViewController)
tabVC.presentViewController_animated_completion_(referenceViewController, True, None)
JonB

You were very close! addSubview takes a view, not a viewcontroller, which can be obtained thusly:

ObjCInstance(back).addSubview_(referenceViewController.view())
cvp

One more time, thanks a lot to be so kind with me...
But I have the same problem of script causing Pythonista to crash without any message...

cvp

If I comment the "tabVC.addTabWithViewController_(referenceViewController)" statement, all is ok
Thanks and have a good end of week-end

cvp

My error! In your original script, this line was commented and during my tests, I've removed the comment, sorry.