O'Reilly logo

Swing Hacks by Chris Adamson, Joshua Marinacci

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Make Mac Applications Behave Normally #81
Chapter 11, Native Integration and Packaging
|
413
HACK
actually does have some other arguments. For example, -t will force the file
to be opened in TextEdit, and
-f will make open read from standard input
rather than a file.
The most useful extra argument is probably
-a
, which lets you force the file
to be opened in a particular program rather than just using the default. For
example, if you wanted to open a text file in Microsoft Word instead of the
default text editor, you could do something like this:
public static void main(String[] args) throws Exception {
Runtime rt = Runtime.getRuntime( );
String[] cmd = {
"open",
"-a",
"Microsoft Word",
"mynotes.txt"
};
rt.exec(cmd);
}
Notice you have to use an array instead of a single string
because Microsoft Word has a space in it. If you don’t do
that, the program will fail with a bus error.
open is a simple but very powerful program because it gives you easy access
to launching programs and files without knowing the user’s settings. The
default applications will be used for each file type, including URLs. You
don’t have to hardcode this information into your program, thus creating a
better experience for your user.
To learn more about the open command, you can type man
open
into a terminal window.
H A C K
#81
Make Mac Applications Behave Normally Hack #81
Setting a few system properties will make your application seem more like
other Mac apps.
Of the desktop platforms your application is likely to run on, the Mac is the
least like the others. Maybe it’s because the various Linux desktops hemmed
closely to Windows’ ways of thinking, or maybe the GNOME guys had
never used a Mac and didn’t “think different.” But the result is that certain
assumptions you might reasonably make on Windows or Linux—like
assuming that windows have menu bars and that any corner or edge of a
window can be dragged to resize the window—aren’t correct on the Mac.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required