代码如下

#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

Many people probably already know this, but if you're not using NSDocument in your Cocoa application, the "Open Recent" menu will not get populated automatically when you open files. There are two steps to getting the "Open Recent" menu to work correctly, which are documented in the NSDocumentController documentation, but if you're searching for a way to do this on Google, the NSDocumentController documentation will not come up, and it's not inherently obvious that you should look in the NSDocumentController documentation, and searching the XCode documentation for "Open Recent" returns no results.

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!

Open Recent相关推荐

  1. Traceback (most recent call last): File AttributeError: 'NoneType' object has no attribute 'group'

    Traceback (most recent call last):File "<stdin>", line 1, in <module> Attribut ...

  2. CLion使用WSL的cmake报错解决: configure_file Problem configuring file Call Stack (most recent call first):

    今天想用WSL的cmake,结果刚使用的时候就报错如下: /usr/bin/cmake -DCMAKE_BUILD_TYPE=Debug -G "CodeBlocks - Unix Make ...

  3. android stadio open recent 在同一窗口打开

    Android staido 有一个功能是open recent ,默认是下面这样的: 就出来一个框,给你选择,是在新的窗口打开,还是在当前窗口打开.如果你选了当前窗口,并且点了Remember,do ...

  4. 使用 iptables 限制***猜密码续—深入 recent 模块

    使用 iptables 限制***猜密码续-深入 recent 模块 作者:高张远瞩(HiLoveS) 博客:http://www.cnblogs.com/hiloves/ 转载请保留该信息 在< ...

  5. CV:翻译并解读2019《A Survey of the Recent Architectures of Deep Convolutional Neural Networks》第一章~第三章

    CV:翻译并解读2019<A Survey of the Recent Architectures of Deep Convolutional Neural Networks>第一章~第三 ...

  6. This version of the rendering library is more recent than your version of IntelliJ IDEA.

    今天往idea里导入其他项目时又遇到了一个问题,单独标记一下. 导入后打开一个布局xml文件,发现不能渲染,报错为: This version of the rendering library is ...

  7. Traceback (most recent call last)AttributeError: ‘NumpyArrayIterator‘ object has no attribute ‘ndim‘

    成功解决该问题! Traceback (most recent call last): File "C:/Users/-/Desktop/pythonProject2/main.py&quo ...

  8. android 锁屏 home,android 锁屏界面禁用长按home 和menu(recent apps)

    android 5.1 系统中 public long interceptKeyBeforeDispatching(WindowState win, KeyEvent event, int polic ...

  9. LeetCode MySQL 1549. The Most Recent Orders for Each Product

    文章目录 1. 题目 2. 解题 1. 题目 Table: Customers +---------------+---------+ | Column Name | Type | +-------- ...

  10. LeetCode MySQL 1532. The Most Recent Three Orders(dense_rank + over窗口函数)

    文章目录 1. 题目 2. 解题 1. 题目 Table: Customers +---------------+---------+ | Column Name | Type | +-------- ...

最新文章

  1. 30 个 php 操作 redis 常用方法代码例子
  2. 安装phpredis扩展
  3. 计算机与社会关系的案例,计算机网络与社会关系论文.pdf
  4. 《最后期限》阅读笔记2
  5. iometer-2006_07_27.common-src编译
  6. mac VMware Fusion 虚拟机键盘可以使用,鼠标无法使用排查思路及解决方法
  7. EVC4.0 PPC2003 Emulator 转中文版
  8. Pig安装及简单使用(pig0.12.0 hadoop2.2.0)
  9. Django学习笔记之Ajax入门
  10. Java——面向对象三大特性学习笔记
  11. python property 与get/set方法详解
  12. 实验1 建立数据库
  13. Java开发童年小游戏
  14. java服务器 c 服务器_c 编写服务器
  15. VisualStudio2017专业版和企业版激活密钥
  16. 视频会议软件Zoom存在安全隐患,我们是否需要停止使用
  17. noob_Noob选择JavaScript框架指南
  18. 简单线性回归R和Python预测身高体重国内生产总值二氧化碳排放量
  19. 软工个人随笔3 罗骏 201521123121
  20. C/C++:计算N的N次方的个位数(火眼金睛找规律,解决此题数据问题)

热门文章

  1. 结合Free to Earn和Play to Earn,Monsterra在GameFi领域的尝试
  2. clickhouse authentication failed
  3. etlgr是什么服务器_什么是数据ETL
  4. AttributeError: partially initialized module ‘openai‘ has no attribute ‘Completion‘ 解决方案
  5. 应用于图像识别的深度残差网络:ResNet
  6. web应用开发学习——CSS利用
  7. 开窗函数over(),rank()的用法及例子
  8. antd 水平和垂直居中
  9. python画曲线的趋势线_python - 在时间序列图中添加趋势线 - SO中文参考 - www.soinside.com...
  10. 人工智能交互革命:探索ChatGPT的无限可能 第5章 ChatGPT-语音助手