Using OSX system-level shortcuts (specifically Dictionary look-up)? also, How to use a macro to run a script (to interact with another program)?

Hello,

Is there a way to use OSX system-level shortcuts (such as the Dictionary-look up feature) in Writer? I want to be able to highlight a word and have that word pasted into Mac’s built-in dictionary application with a single keystroke.

In most applications, I can do this with a shortcut that is assigned globally within the OSX system, but for whatever reason, this doesn’t work/ is disabled in LibreOffice (I assume so that it doesn’t interfere with LibreOffice’s own shortcuts).

I am a translator so being able to look up a word quickly this way would help me alot, and save me from having to copy the text, switch applications, navigate to the dictionary search bar, paste the text, and hit enter every time I need to look up a word.

an alternative solution would be to (bypass the system shortcut method and simply) copy and paste something from LO into another program using a macro, but I have no idea how I might program a macro to do this.

For info on the system level dictionary shortcut in OSX see the following Apple Support Website: https://support.apple.com/kb/PH11266?locale=en_US&viewlocale=id_ID

Thanks alot!

Tom

Hi lostinthesound

Shortcut-keys + a dialog to help you customise them are found:

(menu):ToolsCustomiseKeyboard

You will first need to create a macro for the action that you want before what follows below. Part of that is easy (“highlight + copy the word into the Clipboard”) but I’m hazy on the external features of the action (“paste into the Dictionary software search bar, search for it, copy the result”) (I no longer use a Mac). Nevertheless, I’m going to assume that you have called the script “dictionaryIntegration”. See bottom for the beginnings of such a script.

The following is drawn from the Writer help using 5.0.0.5:

Step-by-step:

  1. (menu):ToolsCustomiseKeyboard
  1. In the Category list box, scroll down and open the “LibreOffice Macros” entry.
  2. Click to get to where you have stored your script.
    (this can easily be the most difficult part!)
    (see below)
  3. Open any scripting language entry to see the available modules + select the “dictionaryIntegration” module.
  4. In the Function list box you will now see a list of all functions in that module. Select the “dictionaryIntegration” function.
  5. Click the option button for LibreOffice or Writer
    (this sets the scope of the action)
    (the options will change according to which applications are currently open).
  6. In the Shortcut Keys list box, select a key combination that you want to use and click Modify
    (that key combo now appears within Keys next to your script)
  7. Click on OK

Errata:

  • The 5.0.0.5 help says to look in the Category list box, open the LibreOffice Macros entry & look for:-

LibreOffice Macros : LibreOffice installation share directory
(available for all)

or
My Macros : LibreOffice user directory
(available only for the user)

I do not have any universal macros (that I know about) but also do not have the above sections. Instead, I have:-


user : (containing my user-macros)

share : (macros that I did not add)

  • 5.0.0.5 has a Function list-box; the Help calls it a Commands list-box

Notes:

  • Scripts are often composed of multiple sub-routines. Top-level functions that make use of the same sub-routines are collected together into ‘modules’.

Workaround:

There is a simple workaround, as detailed at this mac forum post:-

  1. Select word
  1. Right-click selected word
  2. (from the context menu) choose Synonyms
  3. (from the sub-menu) choose Thesaurus…
  4. (a pop-up dialog gives options to select):

Macro Script:

I’ve written a script that you can use. However, there are lines at the bottom that need to be changed (at this moment it just calls a message-box with the text of the highlighted word(s)). Info on Shell().

dictionaryIntegration.odt (contains just the macros)

Sub dictionaryIntegration()
rem ----------------------------------------------------------------------
rem macro for MacOS use with Dictionary.app
''
rem define variables
dim doc            as object
dim cursor         as object
dim selection      as object
dim selections     as object
dim word_to_lookup as string

dim count%, cr$, lf$, ht$, i%

cr             = chr( 13 )
lf             = chr( 10 )
ht             = chr( 9 )

rem ----------------------------------------------------------------------
'' prog note:  ThisComponent.getCurrentSelection() (or)
''             ThisComponent.CurrentController.getTransferable()
'' prog note:  only one object at a time can contain transferable content
'' prog note:  it is possible to have multiple non-contiguous selections,
''             some of which may be empty. We choose the first, only.            

doc            = ThisComponent

'' sanity checks 1
If IsNull( doc ) Then
  MsgBox "'ThisComponent' is Null." + lf + "Fatal error, cannot continue." + lf + lf + "Exiting this function."
  Exit Sub
End If

selections     = doc.getCurrentSelection()
'' sanity checks 2
If IsNull( selections ) Then
  MsgBox "No current Controller." + lf + "Are you working in headless mode?" + lf + lf + "Exiting this function."
  Exit Sub
End If

'' sanity checks 3
If selections.getCount() = 0 Then
  MsgBox "Congratulations!" + lf + "You are suffering the impossible 'this should never happen' error." + lf + lf + "Exiting this function."
  Exit Sub
End If

'' choose the first selection (others silently discarded)
selection      = selections.getByIndex( 0 )

'' sanity checks 4
'' does start-selection match end-selection? (empty selection range)
cursor         = selection.getText().CreateTextCursorByRange( selection )
If cursor.IsCollapsed() Then
  MsgBox "No text selection" + lf + "At least one word must be selected." + lf + lf + "Exiting this function."
  Exit Sub
End If

''  count = selections.getCount()
''  For i = 0 To count - 1
''    selection = selections.getByIndex( count )
''    MsgBox selection.getString()
''  Next

word_to_lookup = selection.getString()
'' word_to_lookup = retStrfromTxtClip( doc.getTransferable())

rem !!!TO DO!!!
rem remark-out next line + un-remark 2nd line + match your system.
rem (this is a 'remark' line) 
'' (this is also a 'remark' line) 
MsgBox word_to_lookup

'' Shell("/full/path/to/dictionaryIntegration.script",2,word_to_lookup)

rem ----------------------------------------------------------------------
End Sub '' dictionaryIntegration

If this helps then please tick the answer (:heavy_check_mark:)

Thank you for your response. Of course, I know how to assign shortcuts for functions within LibreOffice, but what I want to do is access a system-level OSX function, i.e. another piece of software. I couldn’t find a way to do that within the normal shortcut-assigning dialog. Perhaps it can be done with macros, but I don’t really know how.

Thank you again for your response. Unfortunately it doesn’t quite answer my question. I know how do assign macros as keyboard shortcuts, as you described in your post, but as you wrote " I’m hazy on the external features of the action… I’m going to assume that you have found a way to do it." I haven’t, and this is the whole point of the question. Perhaps macros are the solution, but I do not know how or if it is possible to write a macro that accesses an external command.

Workaround added. I see from the forum post linked that neither MS-Office nor LO nor OO contain the necessary Mac system-integration code. Feature request?

Thanks again for your answer. So, this workaround simply looks up the word in the LO built-thesaurus? Because I’m doingt translation, a thesaurus won’t help, but even if I were to download one of the available dictionaries it won’t solve my problem, because I have several specialized dictionaries that I need to use the Mac Dictionary app as well as fact two other dictionary apps that I want be able to have access to. So, the “external application” part of the problem is the key.

One possible solution I thought of was somehow having LO run a small command-line like script that accesses those pieces of software? Is that possible? see http://apple.stackexchange.com/questions/90040/look-up-a-word-in-dictionary-app-in-terminal
Googling, I found this as a possibility, but am not quite Macro-savvy enough to know if this will work: http://unix.stackexchange.com/questions/140537/run-terminal-commands-from-within-libreoffices-basic-programming

That last combo should do it (well found!). I’ve looked through my Macros (lots & lots) & I’ve never used ‘Shell’. Typical. Your main issue is going to be the path/command syntax. Either locate the command smack in the middle of your $PATH, or (better) specify the full filename from root (plus, naturally, make sure that that command is accessible by everyone) (chmod 777 filename). I’ll checkout Shell & advise further.

I’m writing as much of the Macro for you that I can. I’ll edit this as soon as is complete (only 6 hours so far).

Blooming heck! 8 hours. I’m badly out of practice. Nevertheless, it works fine (and almost first time, too). Now, if I can, I’ll attach an ODT containing the full macro so that you can easily use it and/or add it to your system. I’ve never done that before, so it may take another 8 hours! (actually, pretty easy). No text, just the macro routines inside.

Hey sorry it’s taken a while to get back. Holy Cow, thanks a lot. 8 hours? Jeez if someone had asked me to do 8 hours of work for free I would have laughed in their face… thanks so much! I haven’t tried it out yet, but I will tomorrow and report back. In the mean time, I will consider this as solved, since I’m pretty sure I can write a script to do the rest of the job.

I’ve just tried on this mac… -D do pop up a dictionary search here, in writer and in calc

… I probably should have mentioned this, but actually part of the problem may that I have reassigned the dictionary command to be f2. Changing the binding back, yes it does work, but I have actually a second dictionary program I need to access in this way as well, and also, I need it to also show the app window not just the pop-up “tool-tip” display, so Alex’s response does it for me. I should also mention that you can do the dictionary look-up with a three-finger tap.

glad to see that you found a solution that works for your need. reading you topic I thought that the ‘default-out-of-the-box’ thing was broken, thanks for the clarification.