0

我正在编写一个使用 Objective-C-appscript (objc-appscript) 与 Mail 对话的应用程序。我想复制当前选择的邮件消息并稍后对其执行一些处理——此时当前选择可能已更改。

MLApplication *mail = [[MLApplication alloc] initWithBundleID: @"com.apple.mail"];
MLReference *ref = [mail selection];
id theSelection = [[ref getItem] copy];

// Do something here, which may change the contents of ref,  
// but that's okay since I made a copy in theSelection

MLMoveCommand *cmd = [[theSelection move] to: [[mail mailboxes] byName:@"test"]];

// This command should move the selected messages to the mailbox but fails  
// because theSelection

MLReference *ref2 = nil; // Need to turn theSelection into an MLReference *
MLMoveCommand *cmd = [[ref2 move] to: [[mail mailboxes] byName:@"test"]];

我需要将 theSelection 转回 MLReference *。我确定这应该是一个简单的操作,但我是 appscript 的新手,需要一些指导。谢谢!

4

2 回答 2

0

您假设 Apple 事件 IPC 使用分布式对象等代理对象,但事实并非如此:它是 RPC + 查询。(将 XPath over XML-RPC 视为一个粗略的类比。)这是一个常见的误解——Apple 自己完全无法解释它——但掌握 Apple 事件的基于查询的性质对于有效控制可编写脚本的应用程序至关重要。

无论如何,这就是你出错的地方:

id theSelection = [[ref getItem] copy];

这一行复制了一个标识 Mail 属性的 MLReference 对象selection,但作为一个引用基本上类似于一个实际上是无操作的 URL。

MLMoveCommand *cmd = [[theSelection move] to: [[mail mailboxes] byName:@"test"]];

此行告诉 Mail 移动它在引用位置找到的对象。该命令可能会或可能不会起作用,具体取决于 Mail 的脚本支持的能力(某些应用程序可能能够使用单个命令操作多个对象;其他应用程序仅限于每个命令一个对象)。但即使它确实有效,它也会在发送命令时选择的任何内容上运行——这不是你想要的。

在这种情况下,正确的解决方案是使用get命令来检索引用列表(在这种情况下,是 MLReference 实例的 NSArray),稍后您可以对其进行迭代以依次移动每个引用的消息。幸运的是,Mail 返回的引用通过 id 来识别消息,这意味着它们应该继续指向原始消息对象,即使它们同时被移动。(按索引和按名称引用的稳定性要差得多,因此在使用使用它们的应用程序时需要更加小心。)

例如(为清楚起见省略了错误检查):

MLApplication *mail = [MLApplication applicationWithBundleID: @"com.apple.mail"];
NSArray *messageRefs = [[mail selection] getItem];
// do other stuff here
MLReference *message;
for (message in messageRefs) {
    MLMoveCommand *cmd = [[mail move: message] to: [[mail mailboxes] byName: @"test"]];
    id result = [cmd send];
}

有关详细信息,请参阅 appscript 手册。此外,ASTranslate 是您的朋友。

于 2011-01-22T17:03:29.720 回答
0

您始终可以将 theSelection 转换为您想要的任何类型。class您也可以使用该方法查询它并找出它认为是什么类型。不过,您可能不必这样做。

例如,

NSString *something = [(MLReference *)theSelection someFuncIMadeUp];

你可以在苹果文档中阅读所有关于运行时的东西(比如类方法):

http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/ObjCRuntimeRef/Reference/reference.html

于 2011-01-22T03:27:52.753 回答