Xcode AppleScript本身可以发挥神奇的作用,但从插件调用时不起作用

Xcode AppleScript本身可以发挥神奇的作用,但从插件调用时不起作用,xcode,exception,plugins,applescript,applescript-objc,Xcode,Exception,Plugins,Applescript,Applescript Objc,我知道使用AppleScript无法访问新创建的用户编写的内容,所以我使用GUI脚本。但我希望Mail.app包含自己的按钮,该按钮已添加到我从所需目录发送的新创建文件中。我有一个Mail.app插件,它有一个由我自己的按钮调用的脚本。这个脚本本身就可以创造奇迹,但是当从插件调用它时,它就不起作用了。我做错了什么 错误 { NSAppleScriptErrorAppName = "System Events"; NSAppleScriptErrorBriefMessage = "System E

我知道使用AppleScript无法访问新创建的用户编写的内容,所以我使用GUI脚本。但我希望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
因此,对于测试,您可以将一些文件添加到下载文件夹->打开Mail.app中的
prettyAttachment
文件夹中,并创建消息->启动脚本

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


EDIT1另请参见不是非常优雅的解决方案,但它可以工作。虽然这在各方面都是一对拐杖=)

一、 我在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];