[Pythonmac-SIG] Toxic Edit / Cocoa 'IDE'

Steven D. Majewski sdm7g@Virginia.EDU
Tue, 3 Apr 2001 20:13:42 -0400 (EDT)


On Tue, 3 Apr 2001, Jonathan Wight wrote:

> 
> Cool. I'll probably be putting a new version on
> http://toxicsoftware.com/ToxicEdit/ tonight with some fixes.
>  

I'll be sure to check it out later. 


> > import os
> > print os.getcwd()
> > 
> > gives "/" -- not the directory of either the App or any of the
> > scripts. I'm guessing some of the other problems are due to
> > relative paths being wrong.
> 
> More than likely. I'll fix it tonight. That'll teach me for following the
> examples in Programming Python. I think the cwd == '/' is a Cocoa problem
> though - I'll see what I can do to fix it.
> 

I was guessing that it makes sense that a program launched from double
clicking in the finder doesn't have a current working directory of it's
own. 

I tried the script at bottom from ToxicEdit after launching it:
[1] from the finder, and [2] by "open ToxicEdit.app" from the unix
shell command line in Terminal.app. ( You don't usually need the
".app" extension, but I had the unpacked source directory in the
same path, and "open ToxicEdit" instead opened that directory in
the finder -- which by itself is a cool trick to discover! ) 


For both methods, getcwd() always returns "/" and 
bundlePath() always returns the Application bundle pathname. 

environment variable $HOME is set in both methods. 
But only when launched by method 2 ( 'open ToxicEdit.app' ) does
it inherit a $PWD environment var from the parent shell. 
( Trying to run it from the finder gave a KeyError, which is
 why I added those 'try's to the code. ) 


Just as in Classic MacOS, the system also seems to track the
last folder visited for the file selection dialogs. 
( Not sure if you can access that the same way under OSX -- 
  I *think* I recall it used to be thru a global in pre-Carbon MacOS, 
  so there must be a new method for Carbon. ) 


Out of all of those, I'm not quite sure what the "Right" thing to do
is: maybe check for $PWD and if not set, fall back to $HOME or 
parent dir or bundlePath. 



'open -a ToxicEdit.app  script.py' did launch the app but it didn't
open the document. ( see: 'man open' )

'open script.py' *DID* launch the app with the document. 


( It would be cool if there was a way to have an option to 
  automatically run the script without clicking RUN. ) 


-- Steve 

#####
import pyobjc
import os

print 'Module name:',__name__
try:
        print 'Module file:', __file__
except:
        print 

print os.environ

print 'cwd:', os.getcwd()

try:
	print '$PWD:', os.environ['PWD']
except KeyError:
	print  'No $PWD'

try:
	print '$HOME:', os.environ['HOME']
except KeyError:
	print 'No $HOME'

_Pool = pyobjc.runtime.NSAutoreleasePool()

print "BundlePath:",
print pyobjc.runtime.NSBundle.mainBundle().bundlePath()