cocoa - 在 Snow Leopard 中处理 "Open Document"(odoc) 事件

标签 cocoa macos osx-snow-leopard

我的应用程序中有响应“打开文档”(odoc) 事件的代码。在 Mac OS X Tiger 和 Leopard 中,此代码运行良好:

- (void) handleOpenDocumentEvent:
    (NSAppleEventDescriptor*)event
    withReplyEvent:(NSAppleEventDescriptor*)replyEvent
{
    NSAppleEventDescriptor const *const dirObj =
        [event descriptorForKeyword:keyDirectObject];
    DescType const dirObjType = [dirObj descriptorType];

    if ( dirObjType == 'alis' ) {
        //
        // Open a single file.
        //
        NSData const *const data = [dirObj data];
        AliasHandle const fileHandle =
            reinterpret_cast<AliasHandle>( ::NewHandle( [data length] ) );
        if ( fileHandle ) {
            [data getBytes:*fileHandle];
            err = [self queueFile:fileHandle fromSender:senderSig];
        }
    } else if ( dirObjType == 'list' ) {
        //
        // Open multiple files.
        //
        AliasHandle fileHandle =
            reinterpret_cast<AliasHandle>( ::NewHandle( 0 ) );
        if ( fileHandle ) {
            int const numItems = [dirObj numberOfItems];
            for ( int i = 1; i <= numItems; ++i ) {
                NSData const *const data = [[dirObj descriptorAtIndex:i] data];
                ::SetHandleSize( reinterpret_cast<Handle>( fileHandle ), [data length] );
                if ( (err = ::MemError()) != noErr )
                    break;
                [data getBytes:*fileHandle];
                err = [self queueFile:fileHandle fromSender:senderSig];
                if ( err != noErr )
                    break;
            }
        }
    }
}

但是,在 Mac OS X Snow Leopard 下,此代码不起作用。以下是 Leopard 系统中 AppleEvent 的转储:

{ 1 } 'aevt':  aevt/odoc (i386){
          return id: 1012269061 (0x3c560005)
     transaction id: 0 (0x0)
  interaction level: 112 (0x70)
     reply required: 0 (0x0)
             remote: 0 (0x0)
      for recording: 0 (0x0)
         reply port: 150031 (0x24a0f)
  target:
    { 1 } 'psn ':  8 bytes {
      { 0x0, 0x655655 } (iPhoto)
    }
  fEventSourcePSN: { 0x0,0x655655 } (iPhoto)
  optional attributes:
    < empty record >
  event data:
    { 1 } 'aevt':  - 1 items {
      key '----' -
        { 1 } 'list':  - 1 elements {
          { 1 } 'alis':  326 bytes {
            /Users/pjl/Pictures/IMG_8501.JPG
          }
        }
    }
}

以下是 Snow Leopard 系统中 AppleEvent 的转储:

{ 1 } 'aevt':  aevt/odoc (i386){
          return id: 5173 (0x1435)
     transaction id: 0 (0x0)
  interaction level: 112 (0x70)
     reply required: 0 (0x0)
             remote: 0 (0x0)
      for recording: 0 (0x0)
         reply port: 81695 (0x13f1f)
  target:
    { 1 } 'psn ':  8 bytes {
      { 0x0, 0x17c17c } (iPhoto)
    }
  fEventSourcePSN: { 0x0,0x17c17c } (iPhoto)
  optional attributes:
    < empty record >
  event data:
    { 1 } 'aevt':  - 1 items {
      key '----' - 
        { 1 } 'list':  - 1 elements {
          { 1 } 'bmrk':  944 bytes {
                  000: 626f 6f6b  b003 0000  0000 0110  1000 0000     book............
                  001: c002 0000  0500 0000  0101 0000  5573 6572     ............User
                  002: 7300 0000  0300 0000  0101 0000  706a 6c00     s...........pjl.
                  003: 0800 0000  0101 0000  5069 6374  7572 6573     ........Pictures
                  004: 0e00 0000  0101 0000  6950 686f  746f 204c     ........iPhoto L
                  005: 6962 7261  7279 0000  0800 0000  0101 0000     ibrary..........
                  006: 4d6f 6469  6669 6564  0400 0000  0101 0000     Modified........
                  007: 3230 3037  0b00 0000  0101 0000  4a75 6e20     2007........Jun 
                  008: 392c 2032  3030 3700  0c00 0000  0101 0000     9, 2007.........
                  009: 494d 475f  3633 3837  2e6a 7067  2000 0000     IMG_6387.jpg ...
                  ....
                  058: 0000 0000  30f0 0000  3002 0000  0000 0000     ....0...0.......
          }
        }
    }
}

“alis”类型已被新的 Snow Leopard“书签”类型取代。我如何修改此代码以便:

a) 测试并处理新的“bmrk”类型,即获取文件的绝对路径
b) 继续致力于 Tiger 和 Leopard

或者我可以通过某种方式告诉操作系统我仍然想要包含“alis”结构的 odoc 事件?

最佳答案

这里包含的“书签数据”可以使用 Snow Leopard 中引入的一些新的 CFURL 和/或 NSURL API 进行处理。 +[NSURL URLByResolvingBookmarkData:options:relativeToURL:bookmarkDataIsStale:error]是 NSURL API,可用于解析事件描述符中包含的书签数据。

您还可以使用 coerceToDescriptorType: 方法将描述符强制为别名并以这种方式处理它,尽管 Snow Leopard 是否包含内置的强制处理程序没有记录这(看起来确实应该如此)。

对于 Tiger/Leopard 兼容性,您永远不会在这两个系统上传递书签数据,因此调用新的 NSURL 方法应该不成问题,因为该代码路径永远不会在较旧的系统。

顺便说一句,头文件“AEDataModel.h”包含您正在使用的四个字符代码的符号常量,因此您可以使用 typeAlias 而不是 'alis'typeBookmark 而不是 'bmrk' 等等。这往往会使代码更具可读性,并让编译器保护您免受打字错误等问题的影响。

关于cocoa - 在 Snow Leopard 中处理 "Open Document"(odoc) 事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1446835/

相关文章:

python - Snow Leopard 上的 MySQL_Python

string - 不区分大小写的 NSString 比较

objective-c - 在 Objective-C 中实现 "Show in Finder"按钮

swift - 如何将 HSplitView 左右固定到窗口范围

Swift:如何解压缩不包含在我的包中的文件?

c++ - llvm-g++-4.2 和 C++ 新型类型转换

objective-c - 将 NSImage 绘制到 NSView 子类中

objective-c - 询问 Cocoa/Objective-C 中的热键

c - OS X 是 Linux 可执行文件 "compatible"吗?

iphone - Snow Leopard 中的 Xcode 3.2 挂起运行单元测试