How I Used Swift Script in Electron Browser Natively

Electron is a framework for construction thick consumer packages the use of internet applied sciences like HTML, CSS, and JavaScript. Electron supplies flexibility to care for one JavaScript codebase to create cross-platform apps to reinforce Home windows, macOS, and Linux. Mainly, you write as soon as and run all over.

JavaScript works smartly with virtually all of the options one would need in an software, however the issue arises when OS-specific local API interplay is needed. One such factor I confronted whilst running on an Electron-based browser. There was once a particular requirement to discover a record of open software home windows on Mac at particular durations (a couple of seconds). Node or JavaScript does now not supply any interface to get right of entry to this knowledge from the OS layer, so I’ve to get a hold of a customized answer.

Preliminary Answer

I to start with solved this via writing OSAScript. The OSAScript was once in a position to give you the record of open home windows for Mac, however the primary factor we known all over checking out was once CPU utilization. Because the OSAScript was once working at an period, it led to a 40–50% spike in CPU utilization; thus, the answer was once now not applicable.

What Subsequent?

After looking, I stumbled upon quite a lot of answers like:

  1. The use of Python Cocoa library (PyCocoa): This gives an interface between Python and Cocoa, which may also be ate up within the javascript
  2. JsCocoa libraries: I attempted integrating, nevertheless it didn’t paintings
  3. Customized Flask Server: There have been few posts to run a customized flask server inside the browser and devour the API from JavaScript

These kinds of approaches have been both too bulky to make use of or would lead to upper latency and greater app dimension.

The Answer

The use of SWIFT and child_process.exec(): After all, I made up our minds to put in writing a local SWIFT script the use of Cocoa to spot the method home windows and examined it the use of Xcode Playground. The SWIFT script will output a JSON, which may also be simply ate up and parsed. As soon as the checking out is whole, I compiled it to create an executable the use of

swiftc pattern.swift -o pattern

This equipped me with an executable pattern that may be carried out the use of ./pattern and output JSON.

The next move was once to package deal it in Electron and devour it in child_process.exec() means. With a purpose to package deal, I positioned this document in a folder, let’s assume executable , and up to date the reproduction script to incorporate the content material of this document whilst bundling the code. As soon as the executable is provide throughout the package deal, I give you the executable trail to child_process.exec() command. The use of the callback from this command, I used to be effectively in a position to retrieve the SWIFT output JSON to decide within the Electron App.

End result

This ended in a discount of the CPU overhead presented via OSAScript to nearly 0% from 40%

Conclusion

  1. Create binaries for any OS-specific executable like SWIFT for Mac
  2. Package deal this executable for your Electron software
  3. Use child_process command to execute the packaged binary executable
  4. Devour a reaction for your Electron app to decide!

Leave a Comment

Your email address will not be published. Required fields are marked *