Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making a simple program that show desktop and quit

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?

like image 618
user4951 Avatar asked Dec 30 '25 12:12

user4951


1 Answers

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?

like image 113
VsSoft Avatar answered Jan 02 '26 01:01

VsSoft



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!