3

I put this code:

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

If I comment out [NSApp terminate:self]; then yea all other applications are hidden. However, if I quit the application they show up again.

Also if I make the code like that the program doesn't do anything actually. It hides all other applications and then quit showing all other applications again.

The program is simple.

Hide all application Quit After quitting I want all applications to remain hidden. it's so simple. How?

user4951
  • 32,206
  • 53
  • 172
  • 282
  • `-[NSWorkspace hideOtherApplications]` hides all applications except _yours_. – Costique Feb 19 '12 at 17:01
  • _Some_ application has to be shown at all times; if you do this via the application menu (⎇⌘H) in any application, and then quit it, I believe Finder is brought to the front. It also looks like Xcode re-shows itself when the debugger is attached to this process. Other than that, this code works as I'd expect it to. – jscs Feb 19 '12 at 18:49

1 Answers1

1

What you want, I believe, is actually to minimize all other windows, not hide them. There isn't a truly simple way to do this that I know of, but the simplest way IMO would be to call an applescript that would go through each window of each app and issue the cmd-m command. If minimize is implemented by all of the apps then it should work fine (you could choose to close those that don't minimize if you wanted to). It may take a bit of tinkering to get working exactly right, but the script below, taken from macscripter.net, works 80% of the time for me:

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

The other option would be to use Cocoa Accessibility API to get the windows of each running app and set the AXMinimized . Be forewarned: It can get pretty sticky! These 2 other stackoverflow questions should help you on the way:

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

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

Community
  • 1
  • 1
VsSoft
  • 288
  • 1
  • 11