AppleScript可以自己创建奇迹,但从插件调用时无效

时间:2013-03-13 10:00:59

标签: xcode exception plugins applescript applescript-objc

我知道使用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 也会看到Not able to Execute AppleScript within "Mail" application

1 个答案:

答案 0 :(得分:1)

不是很优雅的解决方案,但它的工作原理。虽然这是各种各样的拐杖=)

我在我的混合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];

全是=)