[Pythonmac-SIG] Mac and unix pathnames

Michael J. Barber mjb@uma.pt
Mon, 21 Jan 2002 12:58:32 +0000


On Monday, January 21, 2002, at 10:02  AM, Jack Jansen wrote:

>
> I think we should simply always use "native" pathnames, i.e. 
> colon-separated pathnames in MacPython and unix-pathnames in 
> MachoPython. All the use-cases for "other" pathnames I could think of 
> are really a question of sloppy programming to begin with. If you 
> disagree and can come up with valid (and common) use cases let me know. 
> If the use cases are not all that common I think I prefer to add a 
> special Python module that will do the conversion in stead of bogging 
> down the core with it.
>
I don't disagree, but I do have an example that (I think) isn't just 
sloppy programming. It is a recent discovery, so there may be a simple 
solution that I haven't though of.

Yesterday, I came across a case where I need Mac-style pathnames from 
MachoPython. If you have read the messages on "rewritten documentation 
for OSA," it will come as no surprise that I've been thinking about 
inter-application communication. If you create a temporary file with 
MachoPython, you get Unix-style pathnames, which can't be used with 
osacompile and osascript.

As an example (without messing with the temp files):
Python 2.2 (#1, Jan  9 2002, 15:28:46)
[GCC 2.95.2 19991024 (release)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
 >>> import os
 >>> os.popen("""osascript -e 'tell application "TextEdit" to open alias 
"Macintosh HD:Users:mjb:applescript.html"'""").readlines()
[]
 >>>
 >>> os.popen("""osascript -e 'tell application "TextEdit" to open alias 
"/Users/mjb/applescript.html"'""").readlines()
syntax error: File /Users/mjb/applescript.html wasn't found. (-43)
[]
 >>>

This particular example could be done by calling 'open' instead of 
'osascript,' but it's not the only spot where file names are needed in 
AppleScripts. It appears to me that a module to do the conversion would 
be more suitable for this task, so I'd agree that it should be left out 
of the core.


Here's my own quick little script to make the change:

GETNAMESCRIPT="""osascript -e 'tell app "Finder" to return the name of 
the startup disk'"""

def u2m(upath, diskname=None):
	
	if diskname == None:
		import os		
		diskname = os.popen(GETNAMESCRIPT).readline()[:-1]
	
	return ":".join([diskname] + upath.split('/')[1:])

Python 2.2 (#1, Jan  9 2002, 15:28:46)
[GCC 2.95.2 19991024 (release)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
 >>> import pathDialects
 >>> pathDialects.u2m('/Users/mjb/applescript.html')
'Macintosh HD:Users:mjb:applescript.html'
 >>> pathDialects.u2m('/Users/mjb/applescript.html','Some Other HD')
'Some Other HD:Users:mjb:applescript.html'
 >>>

I'm not too thrilled with the way this functions, but I think the way I 
get the disk name should be an at least somewhat portable way to work 
with temp files. I don't have an inverse function, but haven't needed it 
yet. Regardless, maybe it will be useful to someone.