Search code examples
javamacososascript

Programmatically launch bash shell command 'osascript' using java


I've tried with:

ProcessBuilder pb = new ProcessBuilder("osascript script.scpt");
pb.inheritIO();
pb.directory(new File("bin"));
try {
    pb.start();
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

but I always get the error "no such file or directory". I've tried also with:

Runtime.getRuntime().exec("osascript script.scpt");

but nothing happens.

I also tried using this string in both the snippets above, but nothing changed.

osascript -e 'tell application \"Safari\" to quit' 

Solution

  • I was able to fix the issue by using this

    ProcessBuilder proc = new ProcessBuilder("osascript", "script.scpt");
    

    This is because there's no tokenisation: the command to run is assumed to have already been tokenised.