[Pythonmac-SIG] Storing path as alias

Tobias Weber towb at celvina.de
Sun Feb 21 19:21:55 CET 2010


On 21.02.2010, at 15:20, has wrote:

Thanks! I made working examples for both, but I guess the first has AppleScript limitations like requiring a UI context.

> mactypes.Alias doesn't provide flatten/unflatten APIs, but it's just a wrapper around aem.ae.AEDesc, which does.

import base64

import mactypes, aem

# Raises ValueError.
alias = mactypes.Alias("/Users/towb/test")
data = alias.desc.flatten()
string = base64.b64encode(data)

# Now store, move and rename file, load back.

data = buffer(base64.b64decode(string))
# aem.ae.MacOSError -50 if bad.
desc = aem.ae.unflattendesc(data)
# makewithdesc does not raise.
alias = mactypes.Alias.makewithdesc(desc)
# aem.ae.MacOSError -43 if lost.
print alias.path

> NSURL has a 'bookmark' feature in 10.6+.

import base64

from Foundation import *

url = NSURL.fileURLWithPath_("/Users/towb/test")
data = url.bookmarkDataWithOptions_includingResourceValuesForKeys_relativeToURL_error_(
    0, None, None, None)
if not data:
    raise IOError("file not found")
string = base64.b64encode(data)

# Now store, move and rename file, load back.

data = buffer(base64.b64decode(string))
url = NSURL.URLByResolvingBookmarkData_options_relativeToURL_bookmarkDataIsStale_error_(
    data, (1 << 8), None, None, None)
    # Named constants don't work?
if not url:
    raise StandardError("bad or lost")
print url.path()



More information about the Pythonmac-SIG mailing list