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
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Electron Js + jQuery + Bootstrap - Dev to Build
  • Dynamic Web Forms In React For Enterprise Platforms
  • Custom Elements Manifest: The Key to Seamless Web Component Discovery and Documentation
  • A Comprehensive Guide To Working With JSON in JavaScript

Trending

  • The Role of Retrieval Augmented Generation (RAG) in Development of AI-Infused Enterprise Applications
  • Recurrent Workflows With Cloud Native Dapr Jobs
  • STRIDE: A Guide to Threat Modeling and Secure Implementation
  • Virtual Threads: A Game-Changer for Concurrency
  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
4.6K 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

  • Electron Js + jQuery + Bootstrap - Dev to Build
  • Dynamic Web Forms In React For Enterprise Platforms
  • Custom Elements Manifest: The Key to Seamless Web Component Discovery and Documentation
  • A Comprehensive Guide To Working With JSON in JavaScript

Partner Resources

×

Comments
Oops! Something Went Wrong

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

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

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 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!