try to fix macOS login item

This commit is contained in:
pompurin404 2024-09-22 14:49:43 +08:00
parent 671b4cf8d1
commit 43f2b2e701
No known key found for this signature in database
2 changed files with 14 additions and 16 deletions

View File

@ -1,10 +1,3 @@
### Features:
- 支持编辑主题文件
- 快捷键添加通知响应
- 优化默认样式
### Bug Fixes ### Bug Fixes
- 修复主题切换错乱的问题 - 修复某些mac无法开启开机启动的问题
- 修复macOS自动授权错误

View File

@ -2,7 +2,6 @@ import { taskDir, exePath, homeDir } from '../utils/dirs'
import { mkdir, readFile, rm, writeFile } from 'fs/promises' import { mkdir, readFile, rm, writeFile } from 'fs/promises'
import { exec } from 'child_process' import { exec } from 'child_process'
import { existsSync } from 'fs' import { existsSync } from 'fs'
import { app } from 'electron'
import { promisify } from 'util' import { promisify } from 'util'
import path from 'path' import path from 'path'
@ -65,7 +64,11 @@ export async function checkAutoRun(): Promise<boolean> {
} }
if (process.platform === 'darwin') { if (process.platform === 'darwin') {
return app.getLoginItemSettings().openAtLogin const execPromise = promisify(exec)
const { stdout } = await execPromise(
`osascript -e 'tell application "System Events" to get the name of every login item'`
)
return stdout.includes(exePath().split('.app')[0].replace('/Applications/', ''))
} }
if (process.platform === 'linux') { if (process.platform === 'linux') {
@ -82,9 +85,10 @@ export async function enableAutoRun(): Promise<void> {
await execPromise(`schtasks /create /tn "${appName}" /xml "${taskFilePath}" /f`) await execPromise(`schtasks /create /tn "${appName}" /xml "${taskFilePath}" /f`)
} }
if (process.platform === 'darwin') { if (process.platform === 'darwin') {
app.setLoginItemSettings({ const execPromise = promisify(exec)
openAtLogin: true await execPromise(
}) `osascript -e 'tell application "System Events" to make login item at end with properties {path:"${exePath().split('.app')[0]}.app", hidden:false}'`
)
} }
if (process.platform === 'linux') { if (process.platform === 'linux') {
let desktop = ` let desktop = `
@ -117,9 +121,10 @@ export async function disableAutoRun(): Promise<void> {
await execPromise(`schtasks /delete /tn "${appName}" /f`) await execPromise(`schtasks /delete /tn "${appName}" /f`)
} }
if (process.platform === 'darwin') { if (process.platform === 'darwin') {
app.setLoginItemSettings({ const execPromise = promisify(exec)
openAtLogin: false await execPromise(
}) `osascript -e 'tell application "System Events" to delete login item "${exePath().split('.app')[0].replace('/Applications/', '')}"'`
)
} }
if (process.platform === 'linux') { if (process.platform === 'linux') {
const desktopFilePath = path.join(homeDir, '.config', 'autostart', `${appName}.desktop`) const desktopFilePath = path.join(homeDir, '.config', 'autostart', `${appName}.desktop`)