Recent

Author Topic: Triggering Dictionary look up using Lazarus / FPC  (Read 932 times)

Manlio

  • Full Member
  • ***
  • Posts: 145
  • Pascal dev
Triggering Dictionary look up using Lazarus / FPC
« on: March 07, 2022, 10:24:01 pm »
When you select a word in Safari, in a text editor, etc. and right click on it, one of the items in the popup menu is to look up the word in Dictionary.

It looks like this: https://imgur.com/9xcr93t

Then, if you select Look Up, something like this is displayed: https://imgur.com/mcAanei

Then, if you dig further into it, the Dictionary app is opened completely.

Now, I would like to be able to do the same, i.e. trigger the look-up of words, from my app.

I did some research and it looks like I need to use DictionaryServices, and it doesn't even seem particularly complicated. According to this document only two API calls are needed:

https://developer.apple.com/library/archive/documentation/UserExperience/Conceptual/DictionaryServicesProgGuide/access/access.html

Unfortunately I have no idea of how to translate the above calls into Lazarus/FPC code.

If anyone can kindly post some code to do that, or if anyone knows about any examples or open source components that perform that function, which I can copy or learn from, I would be very grateful.

Thanks
manlio mazzon gmail

trev

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1983
  • Former Delphi 1-7, 10.2 user
Re: Triggering Dictionary look up using Lazarus / FPC
« Reply #1 on: March 07, 2022, 11:22:41 pm »
FPC has the infrastructure to do this. See: fpcsrc/fpc-3.2.2/packages/univint/src/DictionaryServices.pas

No, I've not used it myself and so have no working examples. You will be the pioneer here :)
Lazarus 2.3, FPC 3.3.1 macOS 12.3.1 x86_64 Xcode 13.4
Lazarus 2.3, FPC 3.3.1 macOS 12.3.1 aarch64 Xcode 13.4

dbannon

  • Hero Member
  • *****
  • Posts: 2045
    • tomboy-ng, a rewrite of the classic Tomboy
Re: Triggering Dictionary look up using Lazarus / FPC
« Reply #2 on: March 08, 2022, 12:34:05 am »
One approach might be to launch a define page in your browser. For example, open you browser now and type into the search bar -

define: conception

And you will see similar definitions to your example.  And its dead easy to launch a browser from a lazarus app. But you are moving the user's focus to the browser and away from your app.

A bit harder to capture the content and display it in a lazarus window but not, I suggest, too hard.

Davo
Lazarus 2, Linux (and reluctantly Win10, OSX)
My Project - https://github.com/tomboy-notes/tomboy-ng

Manlio

  • Full Member
  • ***
  • Posts: 145
  • Pascal dev
Re: Triggering Dictionary look up using Lazarus / FPC
« Reply #3 on: March 08, 2022, 12:56:05 am »
One approach might be to launch a define page in your browser. For example, open you browser now and type into the search bar -

define: conception

And you will see similar definitions to your example.  And its dead easy to launch a browser from a lazarus app. But you are moving the user's focus to the browser and away from your app.

A bit harder to capture the content and display it in a lazarus window but not, I suggest, too hard.

Davo

Thanks, I see that your approach would work, but in this case I need something else -- I don't want to open a browser window, or to get the word definition data to be used in my program, instead I want to launch that OS-managed window with the definition in it, which I believe is part of the Dictionary app, or at least of DictionaryServices.

For others who want to do the same thing as me, see the next message where the function HIDictionaryWindowShow is mentioned.
manlio mazzon gmail

Manlio

  • Full Member
  • ***
  • Posts: 145
  • Pascal dev
Re: Triggering Dictionary look up using Lazarus / FPC
« Reply #4 on: March 08, 2022, 12:57:41 am »
FPC has the infrastructure to do this. See: fpcsrc/fpc-3.2.2/packages/univint/src/DictionaryServices.pas

No, I've not used it myself and so have no working examples. You will be the pioneer here :)

That's great, thanks! Challenge accepted :)

One more question if I may:

In the unit DictionaryServices.pas I read:

Quote
The result range can be used as an input parameter of DCSCopyTextDefinition() and HIDictionaryWindowShow() in Carbon framework.

Now, HIDictionaryWindowShow() is what I want to do -- trigger the opening of a window with the definition in it, managed by macOS. But above it says that this can only be done with Carbon (i.e. not Cocoa, which is what we need nowadays). And indeed HIDictionaryWindowShow is not implemented in the above unit.

Note that the current Apple documentation says:

Quote
An alternative to getting the definition returned to you is to let Dictionary Services obtain and display the search results for you. In this case, instead of calling DCSCopyTextDefinition, you would pass the text range to the HIDictionaryWindowShow function.

So is seems that the "WindowShow" functionality is available in Cocoa.

I think I will try to implement it anyway, by copying the way things are done with the other methods that are already implemented in DictionaryServices.pas, and see if I manage to make it work.

But if anyone has a better understanding of the matter, then any suggestion or explanation in this regard will be greatly appreciated and probably same me from going down a lot of rabbit holes that lead to nowhere...

Thanks again!

[Edited to fix tags.]
« Last Edit: March 08, 2022, 04:39:18 am by trev »
manlio mazzon gmail

trev

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1983
  • Former Delphi 1-7, 10.2 user
Re: Triggering Dictionary look up using Lazarus / FPC
« Reply #5 on: March 08, 2022, 04:47:40 am »
Quote
Application Manager

Carbon: MacApplication.h

A number of functions in the Application Manager are not available to 64-bit applications. Refer to Application Manager Reference for detailed information about function availability.

Because there are no Cocoa equivalents, the following functions are still available:
* GetSystemUIMode
* SetSystemUIMode
* GetApplicationTextEncoding
* HISearchWindowShow
* HIDictionaryWindowShow

Source: https://developer.apple.com/library/archive/documentation/Carbon/Conceptual/Carbon64BitGuide/HIToolboxChanges/HIToolboxChanges.html

FPC still has this method available. See: fpcsrc/fpc-3.2.2/packages/univint/src/MacApplication.pas
Lazarus 2.3, FPC 3.3.1 macOS 12.3.1 x86_64 Xcode 13.4
Lazarus 2.3, FPC 3.3.1 macOS 12.3.1 aarch64 Xcode 13.4

 

TinyPortal © 2005-2018