0

我知道使用 AppleScript 无法访问新创建的用户编写的内容,所以我使用 GUI Scripting。但我希望 Mail.app 包含自己的按钮,该按钮已添加到我从所需目录发送的新创建文件中。我有一个 Mail.app 插件有一个由我自己的按钮调用的脚本。该脚本本身可以创造奇迹,但从插件调用时它不起作用。我究竟做错了什么?

错误

{
NSAppleScriptErrorAppName = "System Events";
NSAppleScriptErrorBriefMessage = "System Events get error: Can not catch window 1 of process "Mail". Wrong index.";
NSAppleScriptErrorMessage = "System Events get error: Can not catch window 1 of process "Mail". Wrong index.";
NSAppleScriptErrorNumber = "-1719";
NSAppleScriptErrorRange = "NSRange: {0, 0}";
}

和脚本

property theMessage : ""
property allMessages : ""
property myPath : ""
property mySubfolder : "prettyAttachment"

tell application "AppleScript Utility"
    set GUI Scripting enabled to true
end tell

tell application "Mail"

try
    set allMessages to outgoing messages
    set theMessage to front outgoing message
end try

if theMessage is not equal to "" then
    tell application "Finder"
        activate
        set myPath to quoted form of POSIX path of ((path to downloads folder as string) & mySubfolder)
        set shellScriptText to "open " & myPath
        do shell script shellScriptText

        tell application "System Events"
            keystroke "a" using command down
            keystroke "c" using command down
            keystroke "h" using command down
        end tell
    end tell

    activate --mail.app

    tell application "System Events"
        tell process "Mail"
            --?set visible to true
            set allUI to every UI element of front window
        end tell
        repeat with anUI in allUI --as list
            set theRole to role of anUI
            if theRole is equal to "AXScrollArea" then
                set allSubUi to UI elements of anUI
                if (count of allSubUi) is equal to 2 then
                    --set focused of anUI to true
                    set value of attribute "AXFocused" of anUI to true
                    tell application "System Events"
                        keystroke "v" using command down
                        return true
                    end tell
                end if
            end if
        end repeat
    end tell
    end if
end tell

因此,为了进行测试,您可以将一些文件添加到prettyAttachment下载文件夹中的文件夹 -> 打开 Mail.app 并创建消息 -> 启动脚本

但是为什么脚本在 xCode 包中不起作用?问候并希望得到帮助。

EDIT1还看到无法在“邮件”应用程序中执行 AppleScript

4

1 回答 1

1

不是很优雅的解决方案,但它有效。虽然这在各方面都是一对拐杖=)

我在我的 swizzle MailDocumentEditor 类中触发了我的脚本。我从脚本中删除了关于焦点到消息正文和方法的所有内容:

@interface MailDocumentEditor : DocumentEditor<...>{...}
...
- (EditingMessageWebView)webView;
...

@interface EditingMessageWebView : TilingWebView <...>{...}
...
- (BOOL)isActive;
- (void)paste:(id)arg1;
...

一起

[[PrettyBundle sharedInstance] attachPrettyFiles];//fired first half of script (copy to clipboard)

MailDocumentEditor *prettyResult = (MailDocumentEditor*)self;
id prettyWebView = [pbResult webView];
[prettyWebView paste:nil];

仅此而已 =)

于 2013-03-13T11:21:25.483 回答