Latest news about Bitcoin and all cryptocurrencies. Your daily crypto news habit.
I recently updated my Mac to MacOs Mojave. It comes with various upgrades, the dark theme is one ofĀ them.
Coming from the background of hardcode Linux user, I love anything which is black and dark. I loved the new dark theme for Mac, there was just oneĀ issue.
Whenever I need to switch the theme to light or dark, I need to go to Appearance screen and select it. Well, not so efficient, so I made a small command line tool to switch the theme to light orĀ dark.
And as always, sharing with you how I madeĀ it.
The problem
We need to somehow manage to do the system call in Mac via command line in order to achieve what we want, I donāt have much experience in developing apps for Mac and thatās anĀ issue.
But, I do have expertise in Node.js and I think I can figure out a way to make a system call using Node.js libraries orĀ APIās.
The Solution
I did find a Javascript automation tool for MacOs. Thanks to sindresorhus for developing thisĀ tool.
This tool allows us to execute any system level command of Mac using Node.js. Itās calledĀ run-jxa
Also, there is an npm module that is using run-jxa to change themes in Mac. I am going to use that and club it with command line feature ofĀ Node.js.
Related: Develop Command Line Apps UsingĀ Node.js
Letās code.
Code
Here is the package.json.
{ ānameā: āmacdarkmodeā, āversionā: ā1.0.0ā, ādescriptionā: āā, āmainā: āindex.jsā, āscriptsā: { ātestā: āecho āError: no test specifiedā && exit 1" }, ākeywordsā: [], āauthorā: āā, ālicenseā: āISCā, ādependenciesā: { ādark-modeā: āĀ³.0.0ā }, ābinā: { ādarkmodeā: āindex.jsā } }
Here is the code to handle command line inputs and switch themes accordingly.
#!/usr/bin/env node const darkMode = require(ādark-modeā);
async function main() {
let darkmodeflag = await darkMode.isDark(); if(process.argv[2] === undefined || [ātrueā, āfalseā].indexOf(process.argv[2]) === -1) return console.log(āInvalid commandā¦run darkmode true | false to toggle between themesā);{ } if(process.argv[2] === ātrueā) { // enable dark mode if(darkmodeflag) { return console.log(ādark mode is already enabledā); } await darkMode.enable(); console.log(āDark mode is enabledā¦ā); } else { // disable dark mode if(!darkmodeflag) { return console.log(ādark mode is already disabledā); } await darkMode.disable(); console.log(āDark mode is disableā¦ā); } }
//execute the function
main();
You can test the code by directly running the index.js file likeĀ this.
node index.js true // true to switch to dark mode, false to light mode
Or install it in your machine using the following command.
npm i -g
then run this command from a terminal or commandĀ prompt.
darkmode true
Or to switch to the lightĀ mode.
darkmode false
Enjoy.
Bonus
I have already published this on npm so if you have Node.js installed you can install the command line package using the following command.
sudo npm i -g macdarkmode
After the installation run this command to switch to darkĀ mode.
Mac will ask your permission to execute the system level command, clickĀ Ok.
After this, Mac theme will switch to dark or light based on your inputĀ command.
Summary
This project was made out of frustration hence no best practices are followed. This is just for fun, so enjoy and let me know if you liked it in comments.
Originally published at codeforgeek.com on November 22,Ā 2018.
Switch Between Light and Dark Mode in macOS Mojave Command Line was originally published in Hacker Noon on Medium, where people are continuing the conversation by highlighting and responding to this story.
Disclaimer
The views and opinions expressed in this article are solely those of the authors and do not reflect the views of Bitcoin Insider. Every investment and trading move involves risk - this is especially true for cryptocurrencies given their volatility. We strongly advise our readers to conduct their own research when making a decision.