[Java] 在Java中執行Apple Script (How to run apple script in Java?)

這兩行 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

作者

RongSon

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

發佈留言

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