Setting win32 console title from Python

Bengt Richter bokr at oz.net
Thu Apr 28 22:52:13 EDT 2005


On 28 Apr 2005 12:42:34 -0700, "runes" <rune.strand at gmail.com> wrote:

>Hi Duncan, sorry, I was unprecise.  I'm thinking of a script, called
>t.py  that can be used in the console  like an ordinary command. Som if
>I change directory from S:\scripts to d:\projects and execute the
>script  the title changes to "projects" etc.
>
>I have that functionality today with a combination of a python script
>and a batch file. I just wondered if I could use python all the way.
>Apparently I cannot.
>
>Here are the scripts:
>
>
>------ DirInPath:\t.bat --------------------------------
>@echo off
>:: reads bare directory name from file
>:: created by external Python script
>set DIR_FILE_NAME=DOS_IS_TERRIBLE.tmp
>PyBareDir.py %DIR_FILE_NAME%
>
>for /F "eol=;" %%t in (%DIR_FILE_NAME%) do (
>	title %%t
>)
>
>del /Q /F DOS_IS_TERRIBLE.tmp
>------------------------------------------------------------
>
>
>------ DirInPath:\PyBareDir.py --------------------------------
># extracts bare directory name and  writes
># it to file with name given as argument.
>
>from os import getcwd
>from os.path import basename
>import sys
>
>try:
>	saveAsName = sys.argv[1]
>	lastDir = basename(getcwd())
>	XWwz(saveAsName, 'w+').write(lastDir + '\n;')
>except:
>	print "PyBareDir failed:", sys.exc_info()[1]
>
>-----------------------------------------------------------------------
>

I think I'd try one of the win32 api packages and see if SetConsoleTitle
would work. I.e., from some old API docs:
----
The SetConsoleTitle function sets the title bar string for the current console window. 

BOOL SetConsoleTitle(

    LPTSTR  lpszTitle 	// address of new title  
   );	
Parameters

lpszTitle

Points to a null-terminated string that contains the string to appear in the title bar of the console window. 

Return Value

If the function succeeds, the return value is TRUE.
If the function fails, the return value is FALSE. To get extended error information, call GetLastError. 

See Also

GetConsoleTitle 
----

Alternatively, you could compile your own extension for
title setting/getting called consoletitle.dll
using the above API (assuming it works) and
its companion GetConsoleTitle.

Regards,
Bengt Richter



More information about the Python-list mailing list