在Lion上使用FSEvents跟踪文件重命名/删除 [英] tracking file renaming/deleting with FSEvents on Lion

查看:101
本文介绍了在Lion上使用FSEvents跟踪文件重命名/删除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用FSEvents来检测何时从特定文件夹添加/删除文件.目前,我围绕FSEvents实现了一个简单的包装器,并且工作正常:我获取了所有事件.

I'm trying to use FSEvents to detect when files were added/removed from a specific folder. For the moment, I implemented a simple wrapper around FSEvents, and it works fine : I get all the events.

但是现在的问题是,当我在Finder中重命名文件时,我捕获了两个不同的事件:第一个事件的类型为"renamed"(旧文件名),第二个事件为"renamed"(新文件名),而新文件名则为新文件.文档名称.两次调用之间的事件ID不同.

BUT the problem I have now is that when I rename a file in the Finder, I catch 2 distinct events : the first one of type "renamed" with the old file name, and another one with "renamed" and the new filename. The event ids are different between both calls.

那么,我应该如何知道哪个重命名"事件包含旧名称,哪个事件包含旧名称?我尝试查看文档,但不幸的是,没有记录kFSEventStreamEventFlagItemRenamed……它在Lion中似乎是新的.

So, how am I supposed to know which "renamed" event contains the old name, and which event contains the old one ?? I tried looking in the documentation, but unfortunately, kFSEventStreamEventFlagItemRenamed is not documented ... it seems new in Lion.

PS:我唯一能想到的方法是:在重命名的事件上,我检查UI以查看是否有与事件路径相对应的项目.如果是这样,我将其标记为重命名.如果不是,则检查项目是否标记为重命名,如果是,则将其重命名为新的事件路径.但是我真的不喜欢这个主意...

PS: the only way I could think of was : on a renamed event, I check my UI to see if I have an item corresponding to the event path. If so, I flag it for renaming. If not, I check if an item was flagged for renaming, and if so, then I rename it to the new event path. But I really don't like this idea ...

好的,我在我的"PS"行中添加了一些内容:我注意到在重命名某些内容时,这两个事件的ID是连续的,因此使用包含新名称的事件ID,我可以获取包含旧名称的事件.在发生重命名"事件的情况下,我只需在界面中使用一些字典来存储ID和关联的路径.

Ok, I imlemented something along the line of my "PS" : I noticed that when renaming something, the ids of the 2 events are consecutives, so that with the id of the event containing the new name, I can get the event containing the old name. I simply use a little dictionnary in my interface to store ids and associated paths in the case of a "renamed" event.

无论如何,我现在可以捕获重命名事件,甚至可以移动事件:移动文件时,这是FSEventStream捕获的重命名"事件...

Anyway, I can now catch rename events, and even move events : when you move a file, it's a "renamed" event which is caught by the FSEventStream ...

但是,我还有最后一个问题:删除.当我删除某些内容时,它被移到了回收站:我收到一个重命名"事件.但是问题是我没有收到第二次重命名事件. .DS_Store文件上只有一个已修改"事件.我认为Finder使用此文件来了解垃圾箱中有哪些文件,等等.因此,我可以检查对此文件的修改,并获取最后的重命名"事件以检测文件已发送到垃圾箱.但是我使用的是使用Asepsis的TotalFinder,TotalFinder修改了Finder存储.DS_Store文件的方式:我不再收到对此的修改". 概括地说:我无法检测文件何时发送到垃圾箱...

But, I still have one last problem : deleting. When I delete something, it's moved to the recycle bin : I receive a "renamed" event. But the problem is that I don't receive the second rename event. Only a "modified" event on the .DS_Store file. I think this file is used by the Finder to know which files are in the bin, etc. So I could check modification to this file, and get the last "renamed" event to detect that a file was sent to the bin. But I'm using TotalFinder which uses Asepsis, which modifies the way the Finder stores .DS_Store files : I no longer receive "modified" on this. To sumarize : I can't detect when a file is sent to the bin ...

任何想法我该怎么做?也许使用FSEvents之外的其他东西来仅捕获此事件?

Any idea how I can do that ? Maybe use something else than FSEvents to catch only this event ?

推荐答案

好吧,我没有找到完美的答案,但是我找到了一个最终令我非常满意的解决方案,所以我想我可以分享一下^^

Well, I didn't find the perfect answer to my question, but I found a solution which I eventually was really satisfied with, so I thought I might share ^^

正如我所说,将东西移到垃圾箱中时,如果只看1个文件夹,则不会捕获将图像放入垃圾箱时生成的事件.因此,我决定执行以下操作: 我有一个在根文件夹("/")上创建流的类,以便它将捕获所有事件->这解决了将文件发送到回收站以及所有此类问题的问题.然后,此类允许在某些路径上注册委托.因此,我没有创建多个流,而是创建了一个大流,然后根据需要过滤事件,并创建了许多委托.

As I said, when moving stuff to the trash, if you're only watching 1 folder, you won't catch the event generated when the image is put in the trash. So, I decided to do the following : I have a class which creates a stream on the root folder ("/") so that it will catch all the events -> this solves the problem of files being sent to the trash, and all such stuff. Then, this class allow to register delegates on certain pathes. So, instead of creating many streams, I create one big stream, then filter events as needed, and I create many delegates.

因此,当我想观看特殊文件夹上的事件时,现在要做的只是以下操作:

So all I have to do now when I want to watch events on a special folder is the following :

[[FSEventsListener instance] addListener:self forPath:somePath];

我只需要在应用程序启动时创建FSEventListener的实例,并在应用程序停止时释放它. 我只需要实现以下3种方法即可自动调用:

I just have to create an instance of FSEventListener at application start, and release it when the app stops. And I just need to implement the following 3 methods which will be automatically called :

-(void)fileWasAdded:(NSString *)file;
-(void)fileWasRemoved:(NSString *)file;
-(void)fileWasRenamed:(NSString *)oldFile to:(NSString *)newFile;

如果您对该小实用程序的源代码感兴趣,可以在这里查看: http://blog .pcitron.fr/tools/macosx-imageviewer/(该实用程序的版本为0.8)

If you're interested in the source code of this little utility, you can check here : http://blog.pcitron.fr/tools/macosx-imageviewer/ (the utility was added at the version 0.8)

我将其开发为一个小的图像查看器的一部分,以使UI与磁盘内容保持同步(它显示每个目录中包含的图像数量,等等.)源代码可用,并且该实用程序位于Utils/中. FSEventsListener.h/.m.

I developed it as part of a a little image viewer to keep the UI synchronized with the disk content (it displays the number of images contained in each directories, etc.) The source code is available, and the utility is in Utils/FSEventsListener.h/.m.

如果有人真的下载了该应用程序并查看了源代码,那么如果发现有用的东西(性能/功能改进等),请随时发表评论/邮件^^

And if by any chance someone actually downloads the application and take a look at the sources, if you find anything usefull (performance / feature improvement, whatever) feel free to drop a comment / mail ^^

这篇关于在Lion上使用FSEvents跟踪文件重命名/删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆