[Pythonmac-SIG] Dock: adding/removing/checking
brad.allen at omsdal.com
brad.allen at omsdal.com
Sun Oct 17 15:57:10 CEST 2004
Bob Ippolito <bob at redivi.com> wrote on 10/15/2004 12:26:54 PM:
> I don't believe there's a "native API" for manipulating the dock
> because software applications are not "allowed" (by convention) to
> modify the dock.
I see. No wonder it's been so difficult to find documentation on this.
Still, some software installers do add icons to the Dock. Example: MS
Office 2004.
> I have two comments about what you're doing though:
>
> - The way you call the defaults application is HORRENDOUS! Wow. I
> highly suggest using the subprocess module (formerly process, formerly
> popen5) that is going to be in Python 2.4, or at least
> os.spawnl/os.spawnv:
I've used this "getCommandOutput" function fairly extensively with success,
so I'd like to find out what's wrong with it.
#--------------begin Python Cookbook popen example code---------
#because FCNTL has been deprecated in favor of fcntl, I found changes
posted by a commenter
#at http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/52296
#I don't really understand the nonBlocking stuff, but it seems to work --BA
def makeNonBlocking(fd):
fl = fcntl.fcntl(fd, fcntl.F_GETFL)
try:
fcntl.fcntl(fd, fcntl.F_SETFL, fl | os.O_NDELAY)
except AttributeError:
fcntl.fcntl(fd, fcntl.F_SETFL, fl | os.FNDELAY)
def getCommandOutput(command):
child = popen2.Popen3(command, 1) # Capture stdout and stderr from
command
child.tochild.close( ) # don't need to write to child's
stdin
outfile = child.fromchild
outfd = outfile.fileno( )
errfile = child.childerr
errfd = errfile.fileno( )
makeNonBlocking(outfd) # Don't deadlock! Make fd's
nonblocking.
makeNonBlocking(errfd)
outdata = errdata = ''
outeof = erreof = 0
while 1:
ready = select.select([outfd,errfd],[],[]) # Wait for input
if outfd in ready[0]:
outchunk = outfile.read( )
if outchunk == '': outeof = 1
outdata = outdata + outchunk
if errfd in ready[0]:
errchunk = errfile.read( )
if errchunk == '': erreof = 1
errdata = errdata + errchunk
if outeof and erreof: break
select.select([],[],[],.1) # Allow a little time for buffers to
fill
err = child.wait( )
if err != 0:
raise RuntimeError, '%s failed with exit code %d\n%s' % (
command, err, errdata)
return outdata
#--------------end Python Cookbook popen example code-----------
> - You can also read/write plist files with PyObjC, using the
> NSUserDefaults and/or NSDictionary's plist
> serialization/deserialization methods instead of the defaults command,
> which is about as native as you're going to get. The standard
> library's plistlib might also work, but it's not the most complete and
> accurate implementation out there, so I don't know how much I'd trust
> it for this purpose.
Ok, thanks. I will look into this.
More information about the Pythonmac-SIG
mailing list