Objective c 制作一个显示桌面并退出的简单程序
我把这个代码:Objective c 制作一个显示桌面并退出的简单程序,objective-c,xcode,Objective C,Xcode,我把这个代码: - (void)applicationDidFinishLaunching:(NSNotification *)aNotification { [[NSWorkspace sharedWorkspace] hideOtherApplications];// Insert code here to initialize your application [NSApp terminate:self]; } 如果我注释掉[NSApp terminate:self];然
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
[[NSWorkspace sharedWorkspace] hideOtherApplications];// Insert code here to initialize your application
[NSApp terminate:self];
}
如果我注释掉[NSApp terminate:self];然后,所有其他应用程序都被隐藏了。但是,如果我退出应用程序,它们会再次出现
另外,如果我把代码做成这样,程序实际上什么都不做。它会隐藏所有其他应用程序,然后再次退出显示所有其他应用程序
程序很简单
隐藏所有应用程序
退出
退出后,我希望所有应用程序保持隐藏状态。很简单。怎么做?我相信,你真正想要的是最小化所有其他窗口,而不是隐藏它们。据我所知,没有一种真正简单的方法可以做到这一点,但IMO最简单的方法是调用applescript,它将通过每个应用程序的每个窗口发出cmd-m命令。如果所有应用程序都实现了最小化,那么它应该可以正常工作(如果愿意,您可以选择关闭那些不最小化的应用程序)。可能需要进行一些修补才能完全正常工作,但下面的脚本(取自)在我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获取每个正在运行的应用程序的窗口,并将其设置为最小化。事先警告:它会变得很粘!以下两个其他stackoverflow问题应该可以帮助您:
-[NSWorkspace hideOtherApplications]
隐藏除您的应用程序以外的所有应用程序。某些应用程序必须始终显示;如果通过应用程序菜单执行此操作(⎇⌘H) 在任何应用程序中,然后退出它,我相信Finder会走到最前面。当调试器连接到此进程时,Xcode看起来也会重新显示。除此之外,这段代码按照我的预期工作。