DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Swift: Master of Decoding Messy JSON
  • Advanced Usage of Decodable in Swift: Handling Dynamic Keys
  • Dynamic Web Forms In React For Enterprise Platforms
  • Custom Elements Manifest: The Key to Seamless Web Component Discovery and Documentation

Trending

  • The 7 Pillars of Meeting Design: Transforming Expensive Conversations into Decision Assets
  • Liquibase: Database Change Management and Automated Deployments
  • DuckDB for Python Developers
  • Architecting Sub-Microsecond HFT Systems With C++ and Zero-Copy IPC
  1. DZone
  2. Coding
  3. Frameworks
  4. How I Used Swift Script in Electron Browser Natively

How I Used Swift Script in Electron Browser Natively

Electron provides flexibility to maintain one JavaScript codebase to create cross-platform apps to support Windows, macOS, and Linux.

By 
Pranav Kumar Chaudhary user avatar
Pranav Kumar Chaudhary
·
Nov. 23, 23 · Tutorial
Likes (2)
Comment
Save
Tweet
Share
5.2K Views

Join the DZone community and get the full member experience.

Join For Free

Electron is a framework for building thick client applications using web technologies like HTML, CSS, and JavaScript. Electron provides flexibility to maintain one JavaScript codebase to create cross-platform apps to support Windows, macOS, and Linux. Basically, you write once and run everywhere.

JavaScript works well with almost all the features one would want in an application, but the problem arises when OS-specific native API interaction is required. One such issue I faced while working on an Electron-based browser. There was a specific requirement to find a list of open application windows on Mac at specific intervals (a few seconds). Node or JavaScript does not provide any interface to access this information from the OS layer, so I have to come up with a custom solution.

Initial Solution

I initially solved this by writing OSAScript. The OSAScript was able to provide the list of open windows for Mac, but the main issue we identified during testing was CPU usage. As the OSAScript was running at an interval, it caused a 40–50% spike in CPU usage; thus, the solution was not acceptable.

What Next?

After searching, I stumbled upon various solutions like:

  1. Using Python Cocoa library (PyCocoa): This provides an interface between Python and Cocoa, which can be consumed in the javascript
  2. JsCocoa libraries: I tried integrating, but it did not work
  3. Custom Flask Server: There were few posts to run a custom flask server within the browser and consume the API from JavaScript

All these approaches were either too cumbersome to use or would result in higher latency and increased app size.

The Solution

Using SWIFT and child_process.exec(): Finally, I decided to write a native SWIFT script using Cocoa to identify the process windows and tested it using Xcode Playground. The SWIFT script will output a JSON, which can be easily consumed and parsed. Once the testing is complete, I compiled it to create an executable using

Swift
 
swiftc sample.swift -o sample


This provided me with an executable sample that can be executed using ./sample and output JSON.

The next step was to bundle it in Electron and consume it in child_process.exec() method. In order to bundle, I placed this file in a folder, let's say executable , and updated the copy script to include the content of this file while bundling the code. Once the executable is present inside the bundle, I provide the executable path to child_process.exec() command. Using the callback from this command, I was successfully able to retrieve the SWIFT output JSON to make a decision in the Electron App.

Result

This resulted in a reduction of the CPU overhead introduced by OSAScript to almost 0% from 40%

Conclusion

  1. Create binaries for any OS-specific executable like SWIFT for Mac
  2. Package this executable in your Electron application
  3. Use child_process command to execute the packaged binary executable
  4. Consume a response in your Electron app to make a decision!
JSON JavaScript Electron (software framework) Swift (programming language)

Opinions expressed by DZone contributors are their own.

Related

  • Swift: Master of Decoding Messy JSON
  • Advanced Usage of Decodable in Swift: Handling Dynamic Keys
  • Dynamic Web Forms In React For Enterprise Platforms
  • Custom Elements Manifest: The Key to Seamless Web Component Discovery and Documentation

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook