這兩行 Apple script
set volSettings to get volume settings
set volume output volume ((output volume of volSettings) + 7)
是讓 Mac 的系統音量提高一格
同理,要讓它降一格就改成 -7 就行了
ps. Mac 的系統音量是 16 格 而 apple script 的音量值大小是以百分比來看 所以就用 100/16 = 6.25 每一格,取整數為 7
這支 Demo 是在 java 中執行上述的兩行 Apple script
完整 Code:
class AppleScriptTesting{
public static void main(String[] args){
Runtime runtime = Runtime.getRuntime();
String applescriptCommand = "set volSettings to get volume settings\n" +
"set volume output volume ((output volume of volSettings) + 7)\n";
String[] script = { "osascript", "-e", applescriptCommand };
try{
Process process = runtime.exec(script);
}catch(Exception e){
e.printStackTrace();
}
}
}
參考:
Java AppleScript: How to run a multi-line Applescript command from a Java program: https://alvinalexander.com/blog/post/java/how-run-multi-multiple-line-applescript-java-program