问题
I know that using AppleScript cannot get access to the content of the newly created user-writing, so I use GUI Scripting. But I want Mail.app to contain its own button, which has added to the newly created files I send from my desired directory. I have a plugin for Mail.app have a script that is called by my own button. The script works wonders on its own, but it does not work when called from the plugin. What am I doing wrong?
error
{
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}";
}
and script
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
So for testing you can add some files to prettyAttachment
folder in Download folder -> Open Mail.app and create message -> start script
But why didn't script work in xCode bundle? Regards and hope for help.
EDIT1 also see Not able to Execute AppleScript within "Mail" application
回答1:
Not very elegant solution, but it works. Although this is a pair of crutches in every way =)
I,m fired my script in my swizzle MailDocumentEditor class. I removed from the script everything about focus to the body of the message and the method:
@interface MailDocumentEditor : DocumentEditor<...>{...}
...
- (EditingMessageWebView)webView;
...
and
@interface EditingMessageWebView : TilingWebView <...>{...}
...
- (BOOL)isActive;
- (void)paste:(id)arg1;
...
and all together
[[PrettyBundle sharedInstance] attachPrettyFiles];//fired first half of script (copy to clipboard)
MailDocumentEditor *prettyResult = (MailDocumentEditor*)self;
id prettyWebView = [pbResult webView];
[prettyWebView paste:nil];
that's all =)
来源:https://stackoverflow.com/questions/15382062/applescript-works-wonders-on-its-own-but-does-not-work-when-called-from-the-plug