简体   繁体   English

如何强制应用程序以隐藏模式启动? (Mac 登录应用程序)

[英]How do I force apps to start in hide mode? (Mac Login Applications)

Some apps like Telegram or Slack starts wide open even with set in "Hide mode" on Mac login即使在 Mac 登录时设置为“隐藏模式”,某些应用程序(例如 Telegram 或 Slack)也会完全打开

There are some script or config to force an app to launch in hide mode?有一些脚本或配置可以强制应用程序以隐藏模式启动?

With Applescript I already try:使用 Applescript 我已经尝试过:

tell application "Mail" to launch
tell application "System Events" to set visible of process "Mail" to false

But is not what I search但不是我搜索的

In vanilla AppleScript, the launch command is supposed to launch an app in the background, but not hidden.在 vanilla AppleScript 中, launch命令应该在后台启动应用程序,但不是隐藏的。 Some apps use panels for their interfaces (rather than normal windows) and panels tend to pop up to the head of the window hierarchy, so launch doesn't really help.一些应用程序使用面板作为其界面(而不是普通窗口),并且面板往往会弹出到 window 层次结构的头部,因此launch并没有真正的帮助。 I could muck around trying to find a pure AppleScript solution to this, but in this case it's straightforward to invoke objective-C (requires Leopard or above).我可以四处寻找纯粹的 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:

To run this at login, you'll either want to turn it into an AppleScript application that you can put in the Login Items, or write a launchd agent that runs the script at login.要在登录时运行它,您需要将其转换为 AppleScript 应用程序,您可以将其放入登录项中,或者编写一个在登录时运行脚本的启动代理。 See this question's answers .请参阅此问题的答案

I've tested this on Telegram, but not Slack...我已经在 Telegram 上对此进行了测试,但没有在 Slack 上测试过……

Use the run command:使用run命令:

run application "Mail"
run application "Telegram"

Not only much simpler than AppleScriptObjC, it'll be quicker too due as it won't require the additional initialisation time that is needed to load the Objective-C frameworks (of which AppKit is, by far, the largest).它不仅比 AppleScriptObjC 简单得多,而且速度也更快,因为它不需要加载 Objective-C 框架(其中AppKit是迄今为止最大的)所需的额外初始化时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM