[Pythonmac-SIG] AppleEvents in MacPython 2.3?
Jack Jansen
Jack.Jansen at cwi.nl
Sat Aug 16 00:58:37 EDT 2003
On woensdag, aug 13, 2003, at 07:57 Europe/Amsterdam, Larry Meyn wrote:
> My thanks to Mark and Bob for their help. I've thrown together the
> following crude function to generate OS 9 style path names under OS X
> for a scriptable application that needs them.
>
>
> def makeOS9abspath(path):
> """Returns an ":" delimited, OS 9 style absolute path.
> """
> import Carbon.File, os.path
>
> abspath = os.path.abspath(path)
> rootdisk = Carbon.File.FSRef('/').FSGetCatalogInfo(0)[2].as_tuple()[2]
> abspath = rootdisk + ":".join(abspath.split('/')) #make ':' delimited
>
> if os.path.isdir(path):
> return abspath + ":" #add trailing ':' for directories
> else:
> return abspath
This will work for 90% of the paths, but fail on all sorts of
exceptional
ones. Some of the problems:
- It doesn't handle ':' in unix filenames (should be converted to /)
- It only handles 7-bit ascii (unix filenames use utf8 encoding, OS9
paths
use MacRoman or another encoding)
- It will fail for filenames longer than 32 (or 31) characters
What you should do is create an FSSpec for the file, and recursively get
the pathname components from it. Here it is:
def makeOS9abspath(path):
"""Returns an ":" delimited, OS 9 style absolute path.
"""
import Carbon.File, os.path
rv = []
fss = Carbon.File.FSSpec(path)
while 1:
vrefnum, dirid, filename = fss.as_tuple()
rv.insert(0, filename)
if dirid == 1: break
fss = Carbon.File.FSSpec((vrefnum, dirid, ""))
if len(rv) == 1:
rv.append('')
return ':'.join(rv)
This one still has a problem in that it will not work if path doesn't
yet
exist, on OSX FSSpec(path) goes via FSRef's, and these are only defined
on
existing files. The bad news is that I don't think there is a 100%
failsafe
way to create an OS9 path for a nonexisting item on OSX (given a UTF8
unix
path).
--
- Jack Jansen <Jack.Jansen at oratrix.com>
http://www.cwi.nl/~jack -
- If I can't dance I don't want to be part of your revolution -- Emma
Goldman -
More information about the Pythonmac-SIG
mailing list