Open Recent

本文详细介绍了如何在不使用NSDocument的情况下,在Cocoa应用中实现Open Recent功能,包括添加文件到最近打开列表及处理打开文件的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

代码如下

#import "AppDelegate.h"

#import <AppKit/NSDocumentController.h>


@implementation AppDelegate


- (void)applicationDidFinishLaunching:(NSNotification *)aNotification

{

    // Insert code here to initialize your application

    NSURL *urlFile = [NSURL fileURLWithPath:@"/Users/tommymark/Desktop/92/bookContent.swf" isDirectory:YES];

    NSURL *urlFolder = [NSURL fileURLWithPath:@"/Users/tommymark/Desktop/92" isDirectory:NO];

    [self addToRecent:urlFile];

    [self addToRecent:urlFolder];

}


-(void)addToRecent:(NSURL *)url

{

    NSDocumentController *theDocCont = [NSDocumentController sharedDocumentController];

    [theDocCont noteNewRecentDocumentURL:url];

    NSLog(@"addToRecent_%@",url);

}


-(void)application:(NSApplication *)sender openFiles:(NSArray *)filenames

{

    NSLog(@"filenames=%@",filenames);

}


@end


文章1

Using the "Open Recent" Menu in Cocoa without NSDocument


First, you need to tell NSDocumentController to add the file to the menu. To do this, you need to call [[NSDocumentController sharedDocumentController] noteNewRecentDocumentURL:[NSURL fileURLWithPath:filename]];. You can call this at any time after you have the filename, but it might be best to make sure that you can open the file and read it before adding it to the menu.

Next, you need to implement the - (BOOL)application:(NSApplication *)theApplication openFile:(NSString *)filename method in your NSApplication delegate. This method gets called when the user selects an item from the "Open Recent" menu, so its implementation should open the file or call another method which opens the file. If this method returns NO, the file will be removed from the "Open Recent" menu. If it returns YES then the file will be kept in the menu.

文章2

I use the [NSDocumentController sharedDocumentController] to do all my lifting. The documentation's here. Your project doesn't have to be document-based.

Set up an NSDocumentController variable in your header:

NSDocumentController *theDocCont;

Then implement something like the following in your main AppDelegate file:

-(void)addToRecent:(NSArray*)URLs
{
    if (!theDocCont) {
        theDocCont = [NSDocumentController sharedDocumentController];
    }
    [URLs enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
        [theDocCont noteNewRecentDocumentURL:obj];
    }];
}

-(void)application:(NSApplication *)sender openFiles:(NSArray *)filenames
{
    [self openItems:filenames addToRecents:NO]; //see later
}

You can now add to the Recents menu by calling [self addItems:[myNSOpenPanel URLs] addToRecents:YES]; from the completion block of an NSOpenPanel.

Basically, the -addToRecent: method should be given an NSArray of NSURLs. Then they're added to the standard 'Open Recents' menu item (that comes gift-wrapped in the main.xib file when you first set up your project) by the -noteNewRecentDocumentURL:.

When the app's running and you click on an item in that menu the OS will look for the implementation of -application:openFiles: (if it doesn't find it there'll be an NSAlert along the lines of "yourApp can't open files of this type"). fileNames will be an NSArray of NSURLs.

You'll probably want to handle the opening of URLs differently but I've shown mine as it highlights a small issue where (like I initially did) you try to add a Recent item during the call to -application:openFiles:. In my project, I have a communal method to handle the opening of URLs which is called from various parts of the app and also by default adds the URL(s) being opened to the Recents list; but I don't want to re-add an item that's already coming from the 'Open Recents' menu, hence the reason for the addToRecents: part of the signature. If you try to do that there'll be a crash - I suppose it's like an infinite feedback loop!


### 解决 Python 中 `OSError: cannot open resource` 错误 #### 字体路径配置当引发的错误 当使用像 Matplotlib 或 Pillow 这样的图形库时,如果指定的字体文件存在于预期位置,则会抛出 `OSError: cannot open resource` 的异常。这通常是因为代码中指定了一个系统上并存在的字体文件名或是相对路径设置有误[^1]。 #### 安装缺失的字体文件 一种解决方案是确保所需的字体已正确安装在操作系统内,并且可以从应用程序访问这些字体。对于 Linux 用户来说,可能需要通过包管理器来获取特定字体;而对于 Windows 和 macOS 用户而言,可以通过系统的字体管理工具来进行操作。 #### 修改代码中的字体引用 另一个常见的原因是代码里使用的字体名称与实际存在的符。检查函数参数中关于字体的部分,确认所提供的 `.ttf` 文件确实存在于项目目录下或者是绝对路径指向的位置。例如,在定义验证码生成函数时应验证所给定的 `font_file='Monaco.ttf'` 是否准确无误以及该文件是否可被读取[^2]。 ```python from PIL import Image, ImageDraw, ImageFont def check_code(width=120, height=30, char_length=5, font_file='./fonts/Monaco.ttf', font_size=28): try: # 尝试加载字体 font = ImageFont.truetype(font_file, font_size) code = [] img = Image.new(mode='RGB', size=(width, height), color=(255, 255, 255)) draw = ImageDraw.Draw(img, mode='RGB') # 继续处理... except IOError as e: print(f"Error loading font file {font_file}: ", str(e)) ``` 此段代码增加了对字体加载失败情况下的异常捕获机制,以便更好地调试问题所在[^3]。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值