【问题标题】:Cocoa/Obj-C - Open file when dragging it to application iconCocoa/Obj-C - 将文件拖到应用程序图标时打开文件
【发布时间】:2011-03-16 21:08:01
【问题描述】:

目前我的应用程序界面上有一个按钮可以打开文件,这是我的打开代码:

在我的 app.h 中:

- (IBAction)selectFile:(id)sender;

在我的 app.m 中:

@synthesize window;

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {

}

- (IBAction)selectFile:(id)sender {

    NSOpenPanel *openPanel  = [NSOpenPanel openPanel];
    NSArray *fileTypes = [NSArray arrayWithObjects:@"xml",nil];

    NSInteger result  = [openPanel runModalForDirectory:NSHomeDirectory() file:nil types:fileTypes ];

    if(result == NSOKButton){

        NSString * input =  [openPanel filename];

如何编辑我的代码以允许通过应用程序图标拖放打开?
注意:我编辑了 .plist 文件并为“xml”添加了一行,但它改变了任何内容,当我的文件被拖放到图标上时出现错误。
注意 2:我将“文件 -> 打开...”链接到 selectFile:请参阅我的代码
注意 3:我的应用不是基于文档的应用


感谢您的帮助!
米斯基亚

【问题讨论】:

  • 为什么不使用 NSDocument?不够灵活?

标签: objective-c cocoa xcode macos nsdocument


【解决方案1】:

首先在 .plist 文件中为 CFBundleDocumentTypes 添加适当的扩展名。

接下来实现以下委托:
- 应用程序:openFile:(删除了一个文件)
- 应用程序:openFiles:(删除了多个文件)

参考:
NSApplicationDelegate Protocol Reference

对评论的回应:

一步一步的例子,希望它能让一切清楚:)

添加到 .plist 文件中:

 <key>CFBundleDocumentTypes</key>
        <array>
            <dict>
                <key>CFBundleTypeExtensions</key>
                <array>
                    <string>xml</string>
                </array>
                <key>CFBundleTypeIconFile</key>
                <string>application.icns</string>
                <key>CFBundleTypeMIMETypes</key>
                <array>
                    <string>text/xml</string>
                </array>
                <key>CFBundleTypeName</key>
                <string>XML File</string>
                <key>CFBundleTypeRole</key>
                <string>Viewer</string>
                <key>LSIsAppleDefaultForType</key>
                <true/>
            </dict>
        </array>

添加到 ...AppDelegate.h

- (BOOL)processFile:(NSString *)file;
- (IBAction)openFileManually:(id)sender;

添加到 ...AppDelegate.m

- (IBAction)openFileManually:(id)sender;
{
    NSOpenPanel *openPanel  = [NSOpenPanel openPanel];
    NSArray *fileTypes = [NSArray arrayWithObjects:@"xml",nil];
    NSInteger result  = [openPanel runModalForDirectory:NSHomeDirectory() file:nil types:fileTypes ];
    if(result == NSOKButton){
        [self processFile:[openPanel filename]];
    }
}

- (BOOL)application:(NSApplication *)theApplication openFile:(NSString *)filename
{
    return [self processFile:filename];
}

- (BOOL)processFile:(NSString *)file
{
    NSLog(@"The following file has been dropped or selected: %@",file);
    // Process file here
    return  YES; // Return YES when file processed succesfull, else return NO.
}

【讨论】:

  • 我向 CFBundleDocumentTypes (*.xml 文件) 添加了适当的扩展但我不明白如何配置代表...当时我得到一个“selectFile”并且我的所有代码都依赖我的 selectFile 的结果...我如何使用 selectFile 和 openFile 进行工作和配置(以允许拖放功能)...感谢您的帮助!
  • 我添加了一些额外的信息,这解释了如何使用一段代码来处理删除和选择的文件。
  • @Anne 这很好用。但是如何调整 plist 以接受任何文件类型和文件夹?
  • 找到另一个回答它的线程:stackoverflow.com/questions/4356620/…
  • 附加信息:如果您在应用未运行时在图标上放置了一些东西,“openFile..”方法将在“applicationDidFinishLaunching”之前调用。
【解决方案2】:

快速而肮脏的解决方案:

Cocoa: Drag and Drop any file type

在 Xcode 版本 11.4 (11E146) 下测试 卡特琳娜 10.15.4 (19E266)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-12-16
    • 2011-01-30
    • 2022-09-24
    • 1970-01-01
    • 2017-02-24
    • 2011-09-19
    • 2011-03-10
    • 2018-06-28
    相关资源
    最近更新 更多