即使在 Mac 登录时设置为“隐藏模式”,某些应用程序(例如 Telegram 或 Slack)也会完全打开
有一些脚本或配置可以强制应用程序以隐藏模式启动?
使用 Applescript 我已经尝试过:
tell application "Mail" to launch
tell application "System Events" to set visible of process "Mail" to false
但这不是我搜索的内容
最佳答案
在普通 AppleScript 中,launch
命令应该在后台启动应用程序,但不会隐藏。某些应用程序的界面使用面板(而不是普通窗口),并且面板往往会弹出到窗口层次结构的头部,因此 launch
并没有真正的帮助。我可以尝试找到一个纯 AppleScript 解决方案来解决这个问题,但在这种情况下,调用 Objective-C 很简单(需要 Leopard 或更高版本)。
use AppleScript version "2.4"
use framework "AppKit"
use scripting additions
set targetApp to "Telegram"
my openAppSilently:targetApp
on openAppSilently:appToOpen
set appPath to POSIX path of (path to application appToOpen)
set sharedWorkspace to current application's class "NSWorkspace"'s sharedWorkspace()
set appUrl to current application's class "NSURL"'s fileURLWithPath:appPath
set config to current application's class "NSWorkspaceOpenConfiguration"'s configuration()
set config's activates to false
set config's hides to true
sharedWorkspace's openApplicationAtURL:appUrl configuration:config completionHandler:(missing value)
end openAppSilently:
要在登录时运行此脚本,您需要将其转换为可放入登录项中的 AppleScript 应用程序,或者编写一个在登录时运行脚本的 launchd 代理。请参阅this question's answers .
我已经在 Telegram 上测试过这个,但没有在 Slack 上测试过...
关于xcode - 如何强制应用程序以隐藏模式启动? (Mac 登录应用程序),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65223466/