Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you detect Mac Spaces in Java?

I have a java application that was mainly built for the Mac. I need to be able to do either one of three things:

  1. Have my application always follow the current space. Meaning, if I'm on Desktop 2 and start my app, then I switch to Desktop 3, my app will then be visible on Desktop 3 automatically. This is equivalent to right-clicking on the app icon in the dock, select Options, then choose All Desktops. If I can somehow make this the default behavior for my app, without requiring user action, then that would be great!

  2. Capture the screen of a specific space. I currently use the Robot class to take a screen capture. If there is a way I can specify which space to capture, not just the current space, then that would be great!

  3. Be able to at least detect if leaving the current space.

I'm starting to lean towards neither one of these are possible. But if you can give any help, or possible workarounds, that would be awesome!

I have also played around with the GraphicsEnvironment, GraphicsDevice, GraphicsConfiguration, but no luck. It doesn't return any info about Mac's Virtual Desktops (Spaces).

What is also making things difficult is I'm trying to avoid using platform specific code at all costs! I know I can use JNI to accomplish this, or there may be something in the java-apple extension. But currently, I use a single code branch and build for both Mac & Windows. I'm trying to avoid having two separate builds and binaries for both platforms.

like image 718
dream_team Avatar asked May 08 '12 16:05

dream_team


2 Answers

The answer is "no" unless you are writing native code and using JNI. One of the key design features of Java is that it is OS agnostic. What you are asking for is extremely Mac OS X specific. I see nothing in the Apple Java Extensions that helps either.

like image 117
Gray Avatar answered Sep 18 '22 06:09

Gray


No, as far as I know, Java doesn't have any special support for Spaces, so you'll need to use platform-specific, native code. However, JNI--with all its ugliness--isn't your only option. You can use JNA instead, which is much more programmer-friendly, and a lot easier to debug.

In your app, you can detect the platform. If it's a platform for which you've written platform-specific code, you can execute that code. If not, you can fall back to a default, pure Java implementation.

like image 35
rob Avatar answered Sep 21 '22 06:09

rob