fix(electron): restore macOS lock screen by correcting osascript quoting

The osascript -e argument used nested unescaped double quotes, causing sh
to split it into multiple tokens so System Events never saw a valid
AppleScript. This has been broken since a 2022 prettier run stripped the
backslash escapes. Use a template literal so the inner quotes survive
formatting, and drop the CGSession path comment since it was removed in
Big Sur and only kept for pre-Big Sur fallback.

Closes #7217
This commit is contained in:
Johannes Millan 2026-04-16 18:57:54 +02:00
parent 09f26c3a39
commit 8d3b31f9d3

View file

@ -1,9 +1,15 @@
import { exec } from 'child_process';
// NOTE: keep as a template literal so prettier cannot strip backslash escapes
// and re-break the osascript argument (see issue #7217). CGSession was removed
// in macOS Big Sur, so modern macOS relies on the Ctrl+Cmd+Q fallback.
const DARWIN_LOCK_CMD =
`(/System/Library/CoreServices/"Menu Extras"/User.menu/Contents/Resources/CGSession -suspend)` +
` || (osascript -e 'tell application "System Events" to keystroke "q" using {control down, command down}')`;
export const lockscreen = (cb?: (err: unknown, stdout: string) => void): void => {
const lockCommands = {
darwin:
'(/System/Library/CoreServices/"Menu Extras"/User.menu/Contents/Resources/CGSession -suspend) || (osascript -e "tell application "System Events" to keystroke "q" using {control down, command down}")',
darwin: DARWIN_LOCK_CMD,
win32: 'rundll32.exe user32.dll, LockWorkStation',
linux:
'(hash gnome-screensaver-command 2>/dev/null && gnome-screensaver-command -l) || (hash dm-tool 2>/dev/null && dm-tool lock) || (qdbus org.freedesktop.ScreenSaver /ScreenSaver Lock)',