WSH in Other Languages - Python
(Page 4 of 5 )
Python development began in the late 1980s; it was first published in 1991. It is a high-level programming language that focuses on productivity rather than readability. It supports multiple programming paradigms, automatic memory management, and has a large, comprehensive standard library. These features have led Python to become one of the most commonly used open-source environments.
While Python is widely used in the Linux community, it is becoming more and more popular to Windows programmers as well. The feature-rich language helps bridge a gap between the two communities while offering a feasible open source solution to Microsoft’s proprietary languages. With increased support in the Windows community, Python is now gaining a lot of momentum as a viable alternative for WSH programming.
The most commonly used Windows distribution is ActiveState’s ActivePython. This free distribution offers the Python core, many popular extensions, and complete documentation. It also includes PyWin32, a set of extensions that provides Python bindings to much of the Windows API, and the ActivePython installer adds support for use in the Windows Script Host environment.
WSH will not automatically detect a script written in Python, so you will have to specify the Python engine when launching the script, as I explained earlier in this article.
cscript //E:Python myscript.py
The command line above can be used to launch a Python script in WSH. You might also consider adding a unique file extension for Python scripts written in WSH. I’ll show you how you can do that in part two of this series.
fso = WScript.CreateObject("Scripting.FileSystemObject")
You’ll have to use the WScript object’s CreateObject method to connect to COM objects. The WScript object is immediately available whenever a Python script is run in the WSH environment.
folder = fso.GetFolder("C: emp")
files = folder.Files
Python’s object dot syntax should look refreshingly familiar when accessing an object’s properties and methods.
Working with Collections
Like VBScript, Python is able to work with collections and its simple For structure makes it easy to iterate through each of the collection’s objects.
for file in files:
print file.name
You may use either the WScript object’s Echo method or Python’s native print command for outputting text to the screen.
Additional Notes About Python
Python does not require line endings and is pretty flexible about whitespace. Case sensitivity is much like Jscript in that it does require objects to be case sensitive while their properties and methods are not.
One caveat for Python users is that you cannot set an object’s property value directly. You must instead use the SetValue method as shown below.
object.SetValue("property name", newvalue)
Finally, a nice feature of Python is that it automatically imports all constants whenever you create an instance of an OLE object. These are created as properties of win32com.client.constants.
win32com.client.constants.wdDoNotSaveChanges
So any time that you create an instance of the Microsoft Word automation object, the above constant will be defined.
Next: More Languages >>
More Windows Scripting Articles
More By Nilpo