Maarten Truyens
Maarten Truyens

Reputation: 141

Programmatically hide (but not quit) application on OS X

On OS X, is there a possibility (JDK8u40) to programmatically hide the application, similar to what is typically achieved by a user who presses Cmd-H?

I have, unsuccessfully, tried:

Upvotes: 3

Views: 300

Answers (1)

Jan Gassen
Jan Gassen

Reputation: 3534

You can do that on OS X for example by using the Cocoa native bindings of Eclipse SWT. That might not be the most elegant solution but it should work though. If you include the following dependency to your project (make sure to disable -XstartOnFirstThread in your run configuration)

<dependency>
  <groupId>org.eclipse.swt.org.eclipse.swt.cocoa.macosx.x86_64.4.3.swt</groupId>
  <artifactId>org.eclipse.swt.cocoa.macosx.x86_64</artifactId>
  <version>4.3</version>
</dependency>

you can call

OS.objc_msgSend(NSApplication.sharedApplication().id,
                    OS.sel_hide_);

This will call the exact same procedure as triggered by CMD-h and will thus work on OS X only, of course.

Upvotes: 1

Related Questions