[Mac] run a system notification in Java

Java 程式端可以呼叫終端機來執行 apple script

靠的是 runtime.exec(args); 這行,來執行 args 的參數們

 

而實際執行的指令是

osascript -e 'display notification "Take a break" with title "Time up!!!" sound name ""'
//可直接在終端機執行

 

我是放在 timer 倒數完結後(timeOut Override method),引發一個系統通知:

Runtime runtime = Runtime.getRuntime();
String[] args = { "osascript", "-e", "display notification \"Take a break\" with title \"Time's Up!!!\" sound name \"\"" };

try
{
    Process process = runtime.exec(args);
}
catch (IOException ioError)
{
    ioError.printStackTrace();
}

references:

https://alvinalexander.com/blog/post/java/how-run-execute-applescript-java-mac

https://apple.stackexchange.com/questions/57412/how-can-i-trigger-a-notification-center-notification-from-an-applescript-or-shel

作者

RongSon

Graduate Student of CCU COMM Game Development, Network Communication, macOS/Ubuntu/Android, Arduino/Raspberry Pi/Intel Edison, Java/Python/C/C++

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *