且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在 OSX 中使用 Java 代码打开终端并执行命令

更新时间:2023-11-11 20:19:58

使用 Apple 脚本编辑器打开终端并运行您的代码.

Use Apple Script Editor to open Terminal and run your code.

例如:

String action1 = jsonDriver.readApplicationDetails("OFF");
Runtime runtime = Runtime.getRuntime();
String applescriptCommand = "tell application \"Terminal\"\n" + 
         "activate\n" + "delay 1\n" + "tell application \"System Events\"\n" + 
         "keystroke \"" + action1 + "\"\n" + "end tell\n" + "end tell"; 
String[] args1 = { "osascript", "-e", applescriptCommand };
Process process = runtime.exec(args1);
Thread.sleep(5000);