Search code examples
wolfram-mathematicamathematica-frontend

Detecting if a front end token is available to execute


How can I detect if a front end token is available to execute? Alternately, how can I detect if a front end token failed to execute?

Example: the Edit -> Copy command is not available if nothing is selected. FrontEndTokenExecute["Copy"] will simply beep in this case, but it gives me no (programmatic) indication that it has failed.


Solution

  • I found a method to deal with your second question, but it's not elegant:

    1. In Preferences > Interface > Message and Warning actions set Minor user interface warnings to Beep and Print to Console
    2. Make sure there is at least one error message in the message window
    3. Obtain a handle to the message window notebook (using Notebooks[] or so)
    4. Store the last cell in the message notebook using NotebookGet[NotebookObject[FrontEndObject[LinkObject["55d_shm", 1, 1]], 1]]/. Notebook[{___, Cell[a___]}, ___] :> Cell[a] (your handle will look differently, of course)
    5. Your call: FrontEndTokenExecute["Copy"]
    6. Get the last error message and check whether it differs from the one stored in step 4.
    7. The error cell looks like Cell["You tried to edit a cell that is not editable. You can make the \ cell editable by choosing Cell Editable in the Cell Properties \ submenu.", "Message", "Message", "MSG", PageWidth -> WindowWidth, ShowCellLabel -> True, CellChangeTimes -> {3.534442831*10^9}, CellTags -> "cantEditLockedCell"]
    8. Act appropriately