无法在“Mail”应用程序中执行AppleScript

时间:2012-12-15 07:39:17

标签: xcode macos plugins applescript apple-mail

我创建了一个邮件插件,并在外发邮件的“发送”按钮旁边添加了“按钮”,

enter image description here

现在我想从我的按钮发送传出的邮件,我也写了像那样的AppleScript

activate application "Mail"
tell application "System Events"
    tell application process "Mail"
        log "foo"
        tell menu bar 1
            tell menu bar item "Message"
                tell menu "Message"
                    click menu item "Send"
                end tell
            end tell
        end tell
    end tell
end tell

此脚本在Applescript编辑器中工作,但不在MY BUTTON CLICK中工作

我也在Xcode中执行applescript,如

NSURL* url = [NSURL fileURLWithPath:path];
NSDictionary* errors = [NSDictionary dictionary];    
NSAppleScript* appleScript = [[NSAppleScript alloc] initWithContentsOfURL:url error:&errors];
[appleScript executeAndReturnError:&errors];

执行脚本后我得到错误: -

NSAppleScriptErrorAppName = "System Events";
NSAppleScriptErrorBriefMessage = "Can't get menu bar 1 of application process \"Mail\". Invalid index.";
NSAppleScriptErrorMessage = "System Events got an error: Can't get menu bar 1 of application process \"Mail\". Invalid index.";
NSAppleScriptErrorNumber = "-1719";
NSAppleScriptErrorRange = "NSRange: {0, 0}";

所以我尝试了所有的事情但是没有成功。

请帮助我解决这个问题

感谢您的任何建议或帮助。

1 个答案:

答案 0 :(得分:4)

该菜单项具有键盘快捷键,因此您也可以使用它。你试过这个吗?

activate application "Mail"
tell application "System Events"
    tell application process "Mail"
        keystroke "d" using {command down, shift down}
    end tell
end tell