gpt4 book ai didi

objective-c - 制作一个显示桌面并退出的简单程序

转载 作者:搜寻专家 更新时间:2023-10-30 20:26:16 25 4
gpt4 key购买 nike

我把这段代码:

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
[[NSWorkspace sharedWorkspace] hideOtherApplications];// Insert code here to initialize your application
[NSApp terminate:self];
}

如果我注释掉 [NSApp terminate:self];那么是的,所有其他应用程序都被隐藏了。但是,如果我退出应用程序,它们会再次出现。

此外,如果我编写这样的代码,该程序实际上不会执行任何操作。它隐藏所有其他应用程序,然后再次退出显示所有其他应用程序。

程序很简单。

隐藏所有应用放弃退出后,我希望所有应用程序都保持隐藏状态。很简单。怎么办?

最佳答案

我相信您想要的实际上是最小化所有其他窗口,而不是隐藏它们。据我所知,没有一种真正简单的方法可以做到这一点,但 IMO 最简单的方法是调用一个 applescript,它会通过每个应用程序的每个窗口并发出 cmd-m 命令。如果所有应用程序都实现了最小化,那么它应该可以正常工作(如果你愿意,你可以选择关闭那些没有最小化的应用程序)。可能需要一些修改才能完全正确地工作,但下面的脚本取自 macscripter.net , 80% 的时间对我有效:

tell application "System Events"
set currentProcesses to name of every application process whose visible = true
end tell

set tmpWindows to 0

repeat with tmpProcess in currentProcesses
tell application tmpProcess
activate
try
set tmpWindows to count windows
on error
set tmpWindows to 1 --> # of windows to try to force closed if it doesn't 'appear' to have any windows
end try
end tell

if tmpWindows > 0 then
tell application "System Events"
tell application tmpProcess
repeat tmpWindows times
try
keystroke "m" using command down
end try
end repeat
end tell
end tell
end if

set tmpWindows to 0

end repeat

另一种选择是使用 Cocoa Accessibility API 获取每个正在运行的应用程序的窗口并设置 AXMinimized 。预先警告:它会变得很粘!这 2 个其他 stackoverflow 问题应该对您有所帮助:

Mac / Cocoa - Getting a list of windows using Accessibility API

Cocoa accessibility API, can I click a window in the background without activating it?

关于objective-c - 制作一个显示桌面并退出的简单程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9351117/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com