'AppleScript execution sometimes crashes. Can I separate it from the main app?

I'm building an application for macOS and for some of it's functionality I rely on calling AppleScript:

let appleScript = NSAppleScript(source: theScriptIWantToExecute)
var errorDict: NSDictionary? = nil
let possibleResult = appleScript?.executeAndReturnError(&errorDict)

Now I've discovered that some scripts in some circumstances on some applications when they fail they rather than throw an error they crash the whole application. While it's bad that the script fails it's not so critical that the whole UI should crash.

My idea was to separate out the "Apple Script Execution" part into a completely separate process that will be called by the main application. In case it crashes, it can simply be restarted without any consequence for the main app.

I've been thinking what would be the best solution. Since it's more or less a functional problem I've been tempted to use a command line tool, but command line tools only return text if I'm not mistaken. I would rather return some objects. But I don't really need a service.

XPC seems to support this but is geared more towards services.

What is the best way to isolate my main app from these crashes while still being able to use high level objects?



Solution 1:[1]

You can get the summarized data of a selected visual by using exportData and dataSelected event, Please find the below code snippet:

  1. Get the visual name by using dataSelected event:
report.on("dataSelected", function (event) {
    const visualName = event.detail.visual.name;
});
  1. Get the current active page:
const pages = await report.getPages();

let page = pages.filter(function (page) {
    return page.isActive
})[0];
  1. Get the required visual:
const visuals = await page.getVisuals();

let visual = visuals.filter(function (visual) {
    return visual.name === visualName;
})[0];
  1. Get the summarized data using exportData:
const result = await visual.exportData(models.ExportDataType.Summarized);

Please find the references here: https://docs.microsoft.com/javascript/api/overview/powerbi/export-data
https://docs.microsoft.com/javascript/api/overview/powerbi/handle-events#dataselected

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Rohit Ronte