2

Some MacOSX applications like iTunes don't really have problems when I move a file around - they still find the moved file easily (and I'm pretty sure they have not have opened the file before).

How are they doing it? One possibility I could think of is the FS event notification system. (related question)

But I remember that I have seen some absolute file handle / id or so and I thought that maybe they can always query the current file name by that file handle. Is there something like that? Maybe the inode nr but that is a bit too less because I'm not sure how to get the filename from an inode nr and how to open the file.

Community
  • 1
  • 1
Albert
  • 65,406
  • 61
  • 242
  • 386

3 Answers3

0

I just found out about the Alias Manager which sounds like what I am searching for. Unfortunately, that seems deprecated. (related question) Some info about the deprecation.

(Another related question) There is also the NSURL class which provides such functions.

Python code:

import AppKit

def getFileNativeId(filepath):
    if not os.path.isfile(filepath): return None
    filepath = os.path.abspath(filepath)
    filepath = unicode(filepath)

    url = AppKit.NSURL.alloc().initFileURLWithPath_(filepath)   

    bookmark = url.bookmarkDataWithOptions_includingResourceValuesForKeys_relativeToURL_error_(AppKit.NSURLBookmarkCreationPreferFileIDResolution,None,None,None)
    bytes = bookmark[0].bytes().tobytes()

    return bytes

def getPathByNativeId(fileid):
    nsdata = AppKit.NSData.alloc().initWithBytes_length_(fileid, len(fileid))
    url, _, _ = AppKit.NSURL.URLByResolvingBookmarkData_options_relativeToURL_bookmarkDataIsStale_error_(nsdata, AppKit.NSURLBookmarkResolutionWithoutUI, None,None,None)

    if not url: return None

    return unicode(url.path())
Community
  • 1
  • 1
Albert
  • 65,406
  • 61
  • 242
  • 386
0

I think what you're looking for is NSURL's bookmarks — I'm on an iPad do unable to produce proper links due to Apple's shortsighted web page design but the essence of it is that you obtain a bookmark from a URL and can subsequently resolve a bookmark back to a URL. The system works around any movements in the file in the interim.

Tommy
  • 99,986
  • 12
  • 185
  • 204
0

As Albert mentioned Alias Manager has been depreciated, the replacement is the URL Bookmark methods

Nathan Day
  • 5,981
  • 2
  • 24
  • 40