How to translate this slider value change from AppleScript to JavaScript

给你一囗甜甜゛ 提交于 2020-01-17 06:20:09

问题


This bit of AppleScript works. If I have the System Preferences Sound panel open, and run it in the Script Editor app, it changes the volume to be at 50%.

tell application "System Events"
    tell process "System Preferences"
        set v to value of slider 0 of window 0
        log v
        set value of slider 0 of window 0 to 0.5
    end tell
end tell

This, which tries to be the same thing, fails. Anyone know how to fix it?

var se = Application("System Events");
var spp = se.processes["System Preferences"];

spp.windows[0].sliders[0].value = 0.5

var curr = spp.windows[0].sliders[0].value();
console.log("Current value: " + curr + " - " + typeof(curr));

It ends up setting it to 0. It seems I can only set the volume to 0 or 1. In reality I'm trying to script another application, but this boils down the problem.


回答1:


As I noted in my comment, I'm 90% sure this is a bug. Here's a workaround:

app = Application.currentApplication();
app.includeStandardAdditions = true;
try {
app.doShellScript('osascript -e \'tell application "System Events" to set value of slider 0 of window 0 of process "System Preferences" to 0.2\'');
} catch (error ) {
-1;
}


来源:https://stackoverflow.com/questions/28638409/how-to-translate-this-slider-value-change-from-applescript-to-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!