[Pythonmac-SIG] Carbon.File and resolving aliases
Paul Donovan
paul at donovansbrain.co.uk
Wed Jan 21 16:29:44 EST 2004
On 15 Jan 2004, at 22:48, Jack Jansen wrote:
> If you write the routine I'd be interested in adding it to the
> macostools module.
>
Ok, it took a while (not a lot of spare time), but I've got something
I'm fairly happy with. I've not been doing either Python or Mac
programming for very long, so it might not be the best, but here it is:
import Carbon.File
import os.path
import string
def ResolveFinderAliases(path):
"""Returns a version of the supplied path with all Finder aliases
converted to their real locations
"""
if path is "":
return path
# We need to keep testing a full path until all the aliases have
# been removed
finished = 0
while not finished:
comps = string.split(path,'/')
for i,c in enumerate(comps):
if c == '':
comps[i] = '/'
result = ""
for i, comp in enumerate(comps):
# Add the next component of the path
result = os.path.join(result,comp)
# Check for an alias
fss = Carbon.File.FSSpec(result)
fss, isFolder, aliased = Carbon.File.ResolveAliasFile(fss,0)
if aliased:
# Get where the alias points to
fsr = Carbon.File.FSRef(fss)
path = fsr.FSRefMakePath()
for j in range(i+1,len(comps)):
path = os.path.join(path,comps[j])
# Start the process again - the target of this alias
# might have an alias further up the path.
break
elif i == len(comps)-1:
# Reached the last component without finding
# an alias - finished.
finished = 1
return result
One flaw I am aware of is that handling of errors from the Carbon.File
functions is non-existent.
Cheers,
Paul
-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/pkcs7-signature
Size: 2377 bytes
Desc: not available
Url : http://mail.python.org/pipermail/pythonmac-sig/attachments/20040121/e052f3b7/smime.bin
More information about the Pythonmac-SIG
mailing list