简体   繁体   中英

Using Mac OS X Services-menu from a Java/Swing application

I would like to make my Java/Swing application compatible with the Services-menu available on Mac OS X. For example, so that the user could select some text in JTextArea and have it converted into speech by Services -> Speech -> Start Speaking Text . Is there a simple way to achieve that? (The application should still be able to run on platforms other than Mac OS X.)

Have a look at apple's OSXAdapter package (link requires free apple developer login) for java development. The samples included in the package shows you how to integrate nicely to the OS X application menu in a way that is only activated when your application is running under OS X.

I'm inclined to say no. If I recall correctly, services are only available to Cocoa applications, and Java apps are not Cocoa applications.

If all you want is the end result of the text being converted to speech, you could try invoking the " say " command using ProcessBuilder, something like this:

String stuffYouWantToSay = "StackOverflow Rocks!";
Process p = null;
try {
    ProcessBuilder pb = new ProcessBuilder("/usr/bin/say", stuffYouWantToSay);
    p = pb.start();
} catch (Exception e) {
    // handle the error
    return;
}

This won't add it to the services menu, but you can still get the same effect.

Be sure to check out the man page for "say" as you can change the voice.

This seems to work on Mac OS X Leopard, with no change to the original application. So I've lost interest in the answer (to how to make it work on Tiger). Thanks for your contribution, however.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM