非常奇怪:在ScriptEditor上,applescript工作得很好但是当保存到app时,没有用

时间:2017-12-22 12:31:43

标签: macos applescript

我想更轻松地打开和关闭我的shadowsocks客户端。所以在经过一些搜索后,我编写了这样的代码。

private static final Pattern SINGLE_DIGIT = Pattern.compile("\\b(\\d)\\b");
static String padWithZeroes(String InputString, int digits) {
    final Matcher matcher = SINGLE_DIGIT.matcher(InputString);
    StringBuffer sb = new StringBuffer();
    while(matcher.find()){

        matcher.appendReplacement(sb, pad(digits - matcher.group().length())+matcher.group());
    }
    matcher.appendTail(sb);
    return sb.toString();
}

static String pad(int length) {
    final char[] chars = new char[length];
    Arrays.fill(chars, '0');
    return new String(chars);
}

这主要是时间罚款。情况是,经过一段时间你不运行这个脚本,ScriptEditor会告诉我找不到菜单项。在那之后,点击另一次,它将工作并将工作很多时间。当我将此脚本文件保存到应用程序时,它将显示此错误消息。

ignoring application responses
    tell application "System Events" to tell process "Shadowsocks"
        click menu bar item 1 of menu bar 1
    end tell
end ignoring
do shell script "killall System\\ Events"
delay 0.0
tell application "System Events" to tell process "Shadowsocks"
    tell menu bar item 1 of menu bar 1
        click menu item 2 of menu 1
    end tell
end tell

This is the UI Browser's result

2 个答案:

答案 0 :(得分:0)

这适用于最新版Sierra和ShadowsocksX-NG

clickOn() --  This runs the actions to click "Turn Shadowsocks On"

--  This is the handler to click "Turn Shadowsocks On"
on clickOn()
    tell application "ShadowsocksX-NG" to activate
    delay 0.3 -- Adjust if necessary
    tell application "System Events"
        click menu bar item 1 of menu bar 1 of application process "ShadowsocksX-NG"
        delay 0.1 -- Adjust if necessary
        try
            click menu item "Turn Shadowsocks On" of menu 1 of menu bar item 1 of menu bar 1 of application process "ShadowsocksX-NG"
        end try
    end tell
end clickOn
clickOff()  --  This runs the actions to click "Turn Shadowsocks Off"

--  This is the handler to click "Turn Shadowsocks Off"
on clickOff()
    tell application "ShadowsocksX-NG" to activate
    delay 0.1 -- Adjust if necessary
    tell application "System Events"
        click menu bar item 1 of menu bar 1 of application process "ShadowsocksX-NG"
        delay 0.1 -- Adjust if necessary
        try
            click menu item "Turn Shadowsocks Off" of menu 1 of menu bar item 1 of menu bar 1 of application process "ShadowsocksX-NG"
        end try
    end tell
end clickOff

如果您只是想在未运行时启动应用程序,或者在应用程序正在运行时退出该应用程序......

if running of application "ShadowsocksX-NG" then
    tell application "ShadowsocksX-NG" to quit
else
    tell application "ShadowsocksX-NG" to activate
end if

答案 1 :(得分:-1)

感谢大家的贡献。我以另一种方式完成了这项工作。我只是下载了另一个版本的ss - ShadowsocksX-NG。然后我将其设置为手动模式,之后,我只需要编写一些代码来更改我的计算机的代理设置。 我已将此上传到github(https://github.com/dvstter/Change-Mac-Proxy-Setting-With-Alfred)。如果有人需要相同的需求,请到那里看看。 顺便说一句我是中国人,所以我的英语可能有些问题。 最后,我想再次感谢你。大家好此致!!!