Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save karelbilek/dc2558b10bafc7d77de9bc2e7fb44a66 to your computer and use it in GitHub Desktop.
Save karelbilek/dc2558b10bafc7d77de9bc2e7fb44a66 to your computer and use it in GitHub Desktop.
How to switch macOS VSCode font size based on monitor

I wanted to switch VSCode font size based on monitor; external monitor had too big resolution, the built-in had tiny one. I switch from big one to built-in one all the time.

So I put together a JXA. It's very hacked together, you will need to change the details if you want to use it. (The name of the monitor, the font size.)

Put it whenever as "vscode.js" and run osascript vscode.js.

// JXA doesn't have setTimeout/setInterval. this is copied from some other gist, not my creation.
function timer (repeats, func, delay) {
const args = Array.prototype.slice.call(arguments, 2, -1)
args.unshift(this)
const boundFunc = func.bind.apply(func, args)
const operation = $.NSBlockOperation.blockOperationWithBlock(boundFunc)
const timer = $.NSTimer.timerWithTimeIntervalTargetSelectorUserInfoRepeats(
delay / 1000, operation, 'main', null, repeats
)
$.NSRunLoop.currentRunLoop.addTimerForMode(timer, "timer")
return timer
}
function invalidate(timeoutID) {
$(timeoutID.invalidate)
}
function run() {
$.NSRunLoop.currentRunLoop.runModeBeforeDate("timer", $.NSDate.distantFuture)
}
const setTimeout = timer.bind(undefined, false)
const setInterval = timer.bind(undefined, true)
const clearTimeout = invalidate
const clearInterval = invalidate
setTimeout.run = setInterval.run = run
setInterval(()=>{
const se = Application("System Events")
// this is all very brittle and randomly null in the middle of a switch, that's why all these null checks
const desktops = se.desktops
if (desktops.length == 0) {
return
}
const desktop = desktops[0]
if (desktop == null) {
return
}
const mainName = desktops.displayName()
if (mainName == null) {
return
}
const mainNameStr = mainName.toString() // sometimes mainName is object, sometimes string, who knows why... this works always
let sed = ""
// you need to edit all this for your monitor, username, font size
if (mainNameStr.includes("BenQ")) {
sed = `sed -i'' -e 's/"editor.fontSize": 12/"editor.fontSize": 14/' "/Users/karelbilek/Library/Application Support/Code/User/settings.json"`
} else {
sed = `sed -i'' -e 's/"editor.fontSize": 14/"editor.fontSize": 12/' "/Users/karelbilek/Library/Application Support/Code/User/settings.json"`
}
const app = Application.currentApplication();
app.includeStandardAdditions = true;
app.doShellScript(sed);
}, 1000)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment