Saturday, July 28, 2007

Python - HTA : Capturer la position de la Souris

Cet exemple montre comment capturer la position de la souris en Python

Article de référence :
http://msdn2.microsoft.com/en-us/library/ms531073.aspx

<HTML>
<HEAD><TITLE>Report mouse moves in Python</TITLE>

<BODY onmousemove="reportMove()">
<H1>Python catch the Mouse</H1>

<div id="pos"></div>

<SCRIPT LANGUAGE="Python">
d = document.all
def reportMove():
x = window.event.x
y = window.event.y
d.pos.innerHTML = "posx = %d, posy =%d"%(x, y)
</SCRIPT>

</BODY>
</HTML>

Python - HTA : Simple Wizard


Voici comment afficher des pages contenues dans de simples fichiers texte.
Très pratique pour écrire par exemple, des assistants


Fichier tutorhta.txt (servant dans le script tutor.hta)

%pywizard%
%
<h1>Introduction</h1>
D'apr&egrave;s l'article de microsoft :
<a href="http://msdn2.microsoft.com/en-us/library/bb264002.aspx">Getting Your Page's Data into a Bind</a>
%
%
<h2>Chapitre 1</h2>
%
%
<h2>Chapitre 2</h2>
%

fichier tutor.hta

<html>
<head>
<OBJECT ID="Content" WIDTH=0 HEIGHT=0
CLASSID="CLSID:333C7BC4-460F-11D0-BC04-0080C7055A83">
<PARAM NAME="TextQualifier" VALUE="%">
<PARAM NAME="FieldDelim" VALUE="|">
<PARAM NAME="DataURL" VALUE="tutorhta.txt">
<PARAM NAME="UseHeader" VALUE="true">
</OBJECT>
</head>
<body style="font-family: verdana">

<span style="font-weight: bold; cursor: hand" onclick="goBackward()"><<</span>
Python - HTA
<span style="font-weight: bold; cursor: hand" onclick="goForward()">>></span>
<br>
<span id=Status></span><br>
<div id=TheStory datasrc=#Content datafld="pywizard" dataformatas="HTML"></div>

<SCRIPT language=Python>

doc = document.all
rs = None

def goForward():
global rs
if (rs.absolutePosition != rs.recordCount) :
rs.MoveNext()
else :
rs.MoveFirst()

def goBackward():
global rs
if (rs.absolutePosition > 1) :
rs.MovePrevious()
else :
rs.MoveLast()

def initialize():
global rs
rs=doc.Content.recordset;


window.onload = initialize()

</SCRIPT>
</body>
</html>

Thursday, July 19, 2007

HTA - Python : Gestionnaire de Tâches


<html>
<head>
<title>Python Process Lister</title>
<HTA:APPLICATION
ID="processlister"
APPLICATIONNAME="Python Process Lister"
SCROLL="auto"
SCROLLFLAT = "yes"
SINGLEINSTANCE="yes"
>

<style type="text/css">

body {
background-color: #ababcd;
font-family: Verdana
font-size: 0.9 em;
color: #fff;
}

a:link {text-decoration: none; color:#343456}
a:visited {text-decoration: none;color:#343456}
a:active {text-decoration: none; color:#343456}
a:hover {text-decoration: underline; color:452200;}

</style>
</head>


<body onLoad="Initialisation()">
<center><h2>Liste des Process</h2></center>
<span id = "ProcessList"></span>


<!-- Script Python -->
<SCRIPT LANGUAGE="Python">
from win32com.client import GetObject
import win32gui

doc = document.All

ChaineConnexion = r"WinMgMts:\\%s\%s"

listProcess = []

process = None

def displayProc(args):
proc = None
numproc = int(args)
rep = window.confirm("Voulez-vous terminer le processus %s ?"%args )
if rep:
for p in process:
proc = p
if int(p.Handle) == numproc:
alert(u"Le processus %d va être supprimé"%numproc)
break
proc.Terminate

def getAllObjects(serveur = ".", espaceDenom = "root\cimv2", classe = "Win32_Process"):
c = GetObject(ChaineConnexion%(serveur,espaceDenom))
objects = c.ExecQuery(r"select * from %s"%classe)
return objects

def Initialisation():
win32gui.MessageBox(0, " Hello From Python", "Message d'Invite", 0)
RefreshList()
iTimerID = window.setInterval("RefreshList()", 10000)

def RefreshList():
global process

process = getAllObjects()

result = "<table>"
for processus in process:
result += """<tr><td align="right">%4d</td>
<td style="padding-left:15px;"><a class="maya" href="#" onClick="displayProc(%d)">%s</a></div></td>
<td>%s</td>
</tr>"""%(int(processus.Handle),int(processus.Handle),processus.Name,processus.CommandLine)

result += "</table>"

doc.ProcessList.InnerHTML = result

</SCRIPT>

</body>
</html>

Sunday, July 15, 2007

HTA : Arguments en ligne de commande


<HTML>

<HEAD>

<TITLE>HTA Demo</TITLE>
<HTA:APPLICATION ID="oHTA"
APPLICATIONNAME="myApp"
BORDER="thin"
BORDERSTYLE="normal"
CAPTION="yes"
ICON=""
MAXIMIZEBUTTON="yes"
MINIMIZEBUTTON="yes"
SHOWINTASKBAR="no"
SINGLEINSTANCE="no"
SYSMENU="yes"
VERSION="1.0"
WINDOWSTATE="maximize"/>

<SCRIPT language="Python">
d = document.all
lienMSN = "http://msdn2.microsoft.com/en-us/library/ms536495.aspx"
def window_onload():
sTempStr = "applicationName = " + d.oHTA.applicationName + "<br />"
sTempStr += "commandLineName = " + d.oHTA.commandLine + "<br />"
d.oPre.innerHTML = '<a href="%s">%s</a>'%(lienMSN,lienMSN) + "<br />" + sTempStr

</SCRIPT>

</HEAD>

<BODY SCROLL="no">

<p>Les arguments doivent suivre le nom du script et être entourés de guillemets</p>
<PRE ID=oPre></PRE>

</BODY>

</HTML>

Python et WMI

WMI (Windows Managment Instrumentation) est un ensemble de classes permettant d'auditer des serveurs Windows.
L'accés à ces classe est très aisée en Python comme le montre cet exemple:


from win32com.client import GetObject

ChaineConnexion = r"WinMgMts:\\%s\%s"

def getAllObjects(serveur = ".", espaceDenom = "root\cimv2", classe = "Win32_Process"):
c = GetObject(ChaineConnexion%(serveur,espaceDenom))
objects = c.ExecQuery(r"select * from %s"%classe)
return objects

def test():
allObj = getAllObjects()
for proc in allObj:
print proc.Name

if __name__ == "__main__":
test()

Thursday, July 12, 2007

Python et HTA

Python peut parfaitement être utlisé dans les scripts HTA.
Ce qui est un avantage indéniable par rapport à l'utilisation de vbscript.


<html>
<head>
<TITLE>HTML Application Example</TITLE>
<HTA:APPLICATION ID="HTAEx" APPLICATIONNAME="HTAEx" ICON="e.ico" WINDOWSTATE="normal">
</head>

<body onLoad="Initialisation()">
<center><h1>Python in HTML Application</h1></center>

<FORM>
<INPUT TYPE="text" name="gauche">
<INPUT TYPE="button" name="gbutton" value = "MAJ Gauche">
</FORM>

<FORM>
<INPUT TYPE="text" name="centre">
<INPUT TYPE="button" name="cbutton" value = "MAJ Centre">
</FORM>

<FORM>
<INPUT TYPE="text" name="droite">
<INPUT TYPE="button" name="dbutton" value = "MAJ Droite">
</FORM>

<table width="100%" border=0>
<tr>
<td width="33%" valign="top" border= "black"><div id="tabgauche">&nbsp;</div></td>
<td width="33%" valign="top"><div id="tabcentre">&nbsp;</div></td>
<td width="33%" valign="top"><div id="tabdroite">&nbsp;</div></td>
</tr>
</table>

<div id="espace"></div>

S&eacute;lectionnez une option :<br />
<select size="3" name="liste" onChange="SelectOption()">
<option value="1">Option 1</option>
<option value="2">Option 2</option>
<option value="3">Option 3</option>
<option value="4">Option 4</option>
</select>


<!-- Script -->

<script language=Python>

import win32gui
import os


def GetValue(id):
#return document.getElementById(id).value
return document.getElementById(id).getAttribute("value")

def Initialisation():
win32gui.MessageBox(0, " Hello From Python", "Message d'Invite", 0)
d = document.getElementById('espace')
sp = "&nbsp;"*5
d.innerHTML = sp

def SelectOption(*args):
option = GetValue('liste')
win32gui.MessageBox(0, "Option : %s"%option, "Valeur de la Selection", 0)

def gbutton_Onclick():
valeur = GetValue('gauche')
div = document.getElementById('tabgauche')
div.innerHTML = valeur

def cbutton_Onclick():
valeur = GetValue('centre')
div = document.getElementById('tabcentre')
div.innerHTML = valeur

def dbutton_Onclick():
valeur = GetValue('droite')
div = document.getElementById('tabdroite')
div.innerHTML = valeur

</script>
</body>
</html>

Les Scripts HTA


Les applications 'hta' sont de simples fichiers html ayant pout extension 'hta'.
Leur intérêt, entre autre, est qu'il sont interprétes par Windows comme de véritables
applications. Ils permettent ainsi l'utilisation de tous les composants 'html' dans les
scripts écrits en vbscript ou tout autre langage supporté par wsh.


<html>
<head>
<title>Running a Script from Text</title>
<HTA:APPLICATION
ID="objScriptFromText"
APPLICATIONNAME="Running a Script from Text"
SCROLL="auto"
SINGLEINSTANCE="yes"
>
</head>

<SCRIPT Language="VBScript">

Sub RunScript
Msgbox "The script has run."
End Sub

Sub Pointer
document.body.style.cursor = "hand"
End Sub

Sub DefaultCursor
document.body.style.cursor = "default"
End Sub

</SCRIPT>

<body bgcolor="buttonface">

<span id="ClickableSpan" onClick="RunScript" onmouseover="Pointer"
onmouseout="DefaultCursor">
Click here to run the script</span>

</body>
</html>