Fermer un programme proprement - simuler touche clavier ?
1 réponse
David Josty
SW_SHOWNORMAL=1
Set Shell = WScript.CreateObject("WScript.Shell")
shell.SendKeys "%{F4}"
J'utilise un script qui :
1 - Lance un executable (sous windows 2000)
2 - L'executable s'execute tout seul, a la fin il produit un fichier qu'il
enregistre (toujours tout seul) dans un repertoire definit
la dificulté, c'est qu'une fois le traitement terminé, l'applicatif ne
se ferme pas tout seul. Il faut cliquer sur fichier quitter !!!
3 - je teste la presence du "nouveau fichier" , si celuici existe, mon
executable a termine son travail. Donc je peux le killer
Le probleme, si je fais comme cela, quand je vais rouvrir mon applicatf
j'obtiendrai une boite, m'informant que le programme s'est mal
fermé, voulez vous recuperer le travail.... Je suis donc a la recherche d'un
moyen "propre" de fermer le programme.
Pour l'instant j'utilise ce moyen :
J'ai un script wsh
script 01.vbs
---------------------------------------------------------
SW_SHOWNORMAL=1
Set Shell = WScript.CreateObject("WScript.Shell")
shell.SendKeys "%{F4}"
----------------------------------------------------------
Ce script simule l'appuis de ALT et F4
et j'ai remplacé :
for process in c.Win32_Process (ProcessId=process_id):
result = process.Terminate ()
# Lance Omnipage avec le fichier xml
c = wmi.WMI ()
process_id, return_value = c.new ("Win32_Process").Create
(CommandLine="C:\Program Files\ScanSoft\OmniPagePro14.0\OmniPage.exe " +
filename)
# Test la presence du nouveau fichier creer dans le dossier
a = 1
# On efface le fichier c:\tmp\opd.opd
nomfichier = ""
while (nomfichier == ""):
time.sleep(5)
a=a+1
print ("marke ",a)
for nomfichier in glob.glob(r'C:\tmp\opd.opd'):
pass
# Kill Omnipage
print ("attente")
time.sleep(5)
print ("tuer")
for process in c.Win32_Process (ProcessId=process_id):
result = process.Terminate ()
Cette action est irreversible, confirmez la suppression du commentaire ?
Signaler le commentaire
Veuillez sélectionner un problème
Nudité
Violence
Harcèlement
Fraude
Vente illégale
Discours haineux
Terrorisme
Autre
David Josty
"David Josty" a écrit dans le message de news:42554308$0$13834$
SW_SHOWNORMAL=1 Set Shell = WScript.CreateObject("WScript.Shell") shell.SendKeys "%{F4}"
J'utilise un script qui : 1 - Lance un executable (sous windows 2000) 2 - L'executable s'execute tout seul, a la fin il produit un fichier qu'il enregistre (toujours tout seul) dans un repertoire definit la dificulté, c'est qu'une fois le traitement terminé, l'applicatif ne
se ferme pas tout seul. Il faut cliquer sur fichier quitter !!! 3 - je teste la presence du "nouveau fichier" , si celuici existe, mon executable a termine son travail. Donc je peux le killer
Le probleme, si je fais comme cela, quand je vais rouvrir mon applicatf j'obtiendrai une boite, m'informant que le programme s'est mal fermé, voulez vous recuperer le travail.... Je suis donc a la recherche d'un
moyen "propre" de fermer le programme.
Pour l'instant j'utilise ce moyen : J'ai un script wsh
script 01.vbs --------------------------------------------------------- SW_SHOWNORMAL=1 Set Shell = WScript.CreateObject("WScript.Shell") shell.SendKeys "%{F4}" ---------------------------------------------------------- Ce script simule l'appuis de ALT et F4
et j'ai remplacé :
for process in c.Win32_Process (ProcessId=process_id): result = process.Terminate ()
# Lance Omnipage avec le fichier xml c = wmi.WMI () process_id, return_value = c.new ("Win32_Process").Create (CommandLine="C:Program FilesScanSoftOmniPagePro14.0OmniPage.exe " + filename)
# Test la presence du nouveau fichier creer dans le dossier a = 1 # On efface le fichier c:tmpopd.opd nomfichier = "" while (nomfichier == ""): time.sleep(5) a=a+1 print ("marke ",a) for nomfichier in glob.glob(r'C:tmpopd.opd'): pass
# Kill Omnipage print ("attente") time.sleep(5) print ("tuer") for process in c.Win32_Process (ProcessId=process_id): result = process.Terminate ()
J'ai pas tester mais sa m'a l'air pas mal
I RuBoard
7.16 Working with Windows Scripting Host (WSH) from Python Credit: Kevin Altis
7.16.1 Problem You need to use the Windows Scripting Host (WSH) to perform the same tasks as in the classic WSH examples, but you must do so by driving the WSH from within a normal Python script.
7.16.2 Solution Python's abilities on Windows are greatly enhanced by win32all's ability to access COM automation servers, such as the WSH. First, we connect to the Windows shell's COM automation interface:
import sys, win32com.client shell = win32com.client.Dispatch("WScript.Shell")Then, we launch Notepad to edit this script in the simplest way (basically, with the same functionality as os.system). This script's name is, of course, sys.argv[0], since we're driving from Python:
shell.Run("notepad " + sys.argv[0])For a .pys script driven from WSH, it would be WScript.ScriptFullName instead. shell.Run has greater functionality than the more portable os.system. To show it off, we can set the window type, wait until Notepad is shut down by the user, and get the code returned from Notepad when it is shut down before proceeding:
ret = shell.Run("notepad " + sys.argv[0], 1, 1) print "Notepad return code:", retNow, we open a command window, change the path to C:, and execute a dir:
shell.Run("cmd /K CD C: & Dir")Note that cmd works only on Windows NT/2000/XP; on Windows 98/ME, you need to run Command instead, and this does not support the & joiner to execute two consecutive commands. The shell object has many more methods besides the Run method used throughout this recipe. For example, we can get any environment string (similar to accessing os.environ):
print shell.ExpandEnvironmentStrings("%windir%")7.16.3 Discussion This recipe shows three Windows Scripting Host (WSH) examples converted to Python. WSH documentation can be found at http://msdn.microsoft.com/library/en-us/script56/html/wsoriWindowsScriptHost.asp.
Note that this recipe shows a Python program driving WSH, so save the code to a file with extension .py rather than .pys. Extension .pys would be used if WSH was driving (i.e., via cscript.exe)-and, thus, if Python was being used via the ActiveScripting protocol. But the point of the recipe is that you don't need to rely on ActiveScripting: you can use WSH system-specific functionality, such as SendKeys and ExpandEnvironmentStrings, from within a regular Python program, further enhancing the use of Python for system administration and automating tasks in a Windows environment.
Note that you do not need to worry about closing the COM objects you create. Python's garbage collection takes care of them quite transparently. For example, if and when you want to explicitly close (release) a COM object, you can use a del statement (in this case, you need to ensure that you remove or rebind all references to the COM object that you want to release).
7.16.4 See Also Documentation for pythoncom and win32com.client in win32all (http://starship.python.net/crew/mhammond/win32/Downloads.html) or ActivePython (http://www.activestate.com/ActivePython/); Windows API documentation available from Microsoft (http://msdn.microsoft.com); Python Programming on Win32, by Mark Hammond and Andy Robinson (O'Reilly).
I RuBoard
begin 666 previous.gif M1TE&.#EA/@`/`)$``&9F9D1$1)F9F?___R'Y! ``````+ `````^`````)V MC(^IR^TLHIRTVHLS-0+X#X;B2);F)W#GRK9D&G3N3)>P_ WBP.>]Q],!A$.@ M+V<$_@`WT!)9C J)46ER>A4UE52NCQA$@K-6)6AK#G5U8V<56UZC5&ZU7?JD
@O5[*KH&6/,72,@R6(CXPJ'!V.B(1 I.2E9```[ `
end
begin 666 next.gif M1TE&.#EA*0`/`)$``&9F9D1$1)F9F?___R'Y! ``````+ `````I`````)9 MC(^IRRH/HYRT0B. WKS[#P("%I;F-P;9R9KI"@R;' T'6LV/G?O?--U9#P> MD/,3$G4VCM'(Q"5S-:&3:AWRIKA;$]O]&*>MLH]D3F_(ZO++`H]7&O2ZO0`` !.P`` ` end
begin 666 pixel.gif K1TE&.#EA`0`!`(#_`,# P ```"'Y! $`````+ `````!``$```("1 $`.P`` ` end
"David Josty" <dav.josty@pasemail> a écrit dans le message de
news:42554308$0$13834$626a14ce@news.free.fr...
SW_SHOWNORMAL=1
Set Shell = WScript.CreateObject("WScript.Shell")
shell.SendKeys "%{F4}"
J'utilise un script qui :
1 - Lance un executable (sous windows 2000)
2 - L'executable s'execute tout seul, a la fin il produit un fichier qu'il
enregistre (toujours tout seul) dans un repertoire definit
la dificulté, c'est qu'une fois le traitement terminé, l'applicatif
ne
se ferme pas tout seul. Il faut cliquer sur fichier quitter !!!
3 - je teste la presence du "nouveau fichier" , si celuici existe, mon
executable a termine son travail. Donc je peux le killer
Le probleme, si je fais comme cela, quand je vais rouvrir mon applicatf
j'obtiendrai une boite, m'informant que le programme s'est mal
fermé, voulez vous recuperer le travail.... Je suis donc a la recherche
d'un
moyen "propre" de fermer le programme.
Pour l'instant j'utilise ce moyen :
J'ai un script wsh
script 01.vbs
---------------------------------------------------------
SW_SHOWNORMAL=1
Set Shell = WScript.CreateObject("WScript.Shell")
shell.SendKeys "%{F4}"
----------------------------------------------------------
Ce script simule l'appuis de ALT et F4
et j'ai remplacé :
for process in c.Win32_Process (ProcessId=process_id):
result = process.Terminate ()
# Lance Omnipage avec le fichier xml
c = wmi.WMI ()
process_id, return_value = c.new ("Win32_Process").Create
(CommandLine="C:Program FilesScanSoftOmniPagePro14.0OmniPage.exe " +
filename)
# Test la presence du nouveau fichier creer dans le dossier
a = 1
# On efface le fichier c:tmpopd.opd
nomfichier = ""
while (nomfichier == ""):
time.sleep(5)
a=a+1
print ("marke ",a)
for nomfichier in glob.glob(r'C:tmpopd.opd'):
pass
# Kill Omnipage
print ("attente")
time.sleep(5)
print ("tuer")
for process in c.Win32_Process (ProcessId=process_id):
result = process.Terminate ()
J'ai pas tester mais sa m'a l'air pas mal
I l@ve RuBoard
7.16 Working with Windows Scripting Host (WSH) from Python
Credit: Kevin Altis
7.16.1 Problem
You need to use the Windows Scripting Host (WSH) to perform the same
tasks as in the classic WSH examples, but you must do so by driving the WSH
from within a normal Python script.
7.16.2 Solution
Python's abilities on Windows are greatly enhanced by win32all's
ability to access COM automation servers, such as the WSH. First, we connect
to the Windows shell's COM automation interface:
import sys, win32com.client
shell = win32com.client.Dispatch("WScript.Shell")Then, we launch Notepad to
edit this script in the simplest way (basically, with the same functionality
as os.system). This script's name is, of course, sys.argv[0], since we're
driving from Python:
shell.Run("notepad " + sys.argv[0])For a .pys script driven from WSH, it
would be WScript.ScriptFullName instead. shell.Run has greater functionality
than the more portable os.system. To show it off, we can set the window
type, wait until Notepad is shut down by the user, and get the code returned
from Notepad when it is shut down before proceeding:
ret = shell.Run("notepad " + sys.argv[0], 1, 1)
print "Notepad return code:", retNow, we open a command window, change the
path to C:, and execute a dir:
shell.Run("cmd /K CD C: & Dir")Note that cmd works only on Windows
NT/2000/XP; on Windows 98/ME, you need to run Command instead, and this does
not support the & joiner to execute two consecutive commands. The shell
object has many more methods besides the Run method used throughout this
recipe. For example, we can get any environment string (similar to accessing
os.environ):
print shell.ExpandEnvironmentStrings("%windir%")7.16.3 Discussion
This recipe shows three Windows Scripting Host (WSH) examples
converted to Python. WSH documentation can be found at
http://msdn.microsoft.com/library/en-us/script56/html/wsoriWindowsScriptHost.asp.
Note that this recipe shows a Python program driving WSH, so save the
code to a file with extension .py rather than .pys. Extension .pys would be
used if WSH was driving (i.e., via cscript.exe)-and, thus, if Python was
being used via the ActiveScripting protocol. But the point of the recipe is
that you don't need to rely on ActiveScripting: you can use WSH
system-specific functionality, such as SendKeys and
ExpandEnvironmentStrings, from within a regular Python program, further
enhancing the use of Python for system administration and automating tasks
in a Windows environment.
Note that you do not need to worry about closing the COM objects you
create. Python's garbage collection takes care of them quite transparently.
For example, if and when you want to explicitly close (release) a COM
object, you can use a del statement (in this case, you need to ensure that
you remove or rebind all references to the COM object that you want to
release).
7.16.4 See Also
Documentation for pythoncom and win32com.client in win32all
(http://starship.python.net/crew/mhammond/win32/Downloads.html) or
ActivePython (http://www.activestate.com/ActivePython/); Windows API
documentation available from Microsoft (http://msdn.microsoft.com); Python
Programming on Win32, by Mark Hammond and Andy Robinson (O'Reilly).
I l@ve RuBoard
begin 666 previous.gif
M1TE&.#EA/@`/`)$``&9F9D1$1)F9F?___R'Y! ``````+ `````^`````)V
MC(^IR^TLHIRTVHLS-0+X#X;B2);F)W#GRK9D&G3N3)>P_ WBP.>]Q],!A$.@
M+V<$_@`WT!)9C J)46ER>A4UE52NCQA$@K-6)6AK#G5U8V<56UZC5&ZU7?JD
@O5[*KH&6/,72,@R6(CXPJ'!V.B(1 I.2E9```[
`
end
begin 666 next.gif
M1TE&.#EA*0`/`)$``&9F9D1$1)F9F?___R'Y! ``````+ `````I`````)9
MC(^IRRH/HYRT0B. WKS[#P("%I;F-P;9R9KI"@R;' T'6LV/G?O?--U9#P>
MD/,3$G4VCM'(Q"5S-:&3:AWRIKA;$]O]&*>MLH]D3F_(ZO++`H]7&O2ZO0``
!.P``
`
end
begin 666 pixel.gif
K1TE&.#EA`0`!`(#_`,# P ```"'Y! $`````+ `````!``$```("1 $`.P``
`
end
"David Josty" a écrit dans le message de news:42554308$0$13834$
SW_SHOWNORMAL=1 Set Shell = WScript.CreateObject("WScript.Shell") shell.SendKeys "%{F4}"
J'utilise un script qui : 1 - Lance un executable (sous windows 2000) 2 - L'executable s'execute tout seul, a la fin il produit un fichier qu'il enregistre (toujours tout seul) dans un repertoire definit la dificulté, c'est qu'une fois le traitement terminé, l'applicatif ne
se ferme pas tout seul. Il faut cliquer sur fichier quitter !!! 3 - je teste la presence du "nouveau fichier" , si celuici existe, mon executable a termine son travail. Donc je peux le killer
Le probleme, si je fais comme cela, quand je vais rouvrir mon applicatf j'obtiendrai une boite, m'informant que le programme s'est mal fermé, voulez vous recuperer le travail.... Je suis donc a la recherche d'un
moyen "propre" de fermer le programme.
Pour l'instant j'utilise ce moyen : J'ai un script wsh
script 01.vbs --------------------------------------------------------- SW_SHOWNORMAL=1 Set Shell = WScript.CreateObject("WScript.Shell") shell.SendKeys "%{F4}" ---------------------------------------------------------- Ce script simule l'appuis de ALT et F4
et j'ai remplacé :
for process in c.Win32_Process (ProcessId=process_id): result = process.Terminate ()
# Lance Omnipage avec le fichier xml c = wmi.WMI () process_id, return_value = c.new ("Win32_Process").Create (CommandLine="C:Program FilesScanSoftOmniPagePro14.0OmniPage.exe " + filename)
# Test la presence du nouveau fichier creer dans le dossier a = 1 # On efface le fichier c:tmpopd.opd nomfichier = "" while (nomfichier == ""): time.sleep(5) a=a+1 print ("marke ",a) for nomfichier in glob.glob(r'C:tmpopd.opd'): pass
# Kill Omnipage print ("attente") time.sleep(5) print ("tuer") for process in c.Win32_Process (ProcessId=process_id): result = process.Terminate ()
J'ai pas tester mais sa m'a l'air pas mal
I RuBoard
7.16 Working with Windows Scripting Host (WSH) from Python Credit: Kevin Altis
7.16.1 Problem You need to use the Windows Scripting Host (WSH) to perform the same tasks as in the classic WSH examples, but you must do so by driving the WSH from within a normal Python script.
7.16.2 Solution Python's abilities on Windows are greatly enhanced by win32all's ability to access COM automation servers, such as the WSH. First, we connect to the Windows shell's COM automation interface:
import sys, win32com.client shell = win32com.client.Dispatch("WScript.Shell")Then, we launch Notepad to edit this script in the simplest way (basically, with the same functionality as os.system). This script's name is, of course, sys.argv[0], since we're driving from Python:
shell.Run("notepad " + sys.argv[0])For a .pys script driven from WSH, it would be WScript.ScriptFullName instead. shell.Run has greater functionality than the more portable os.system. To show it off, we can set the window type, wait until Notepad is shut down by the user, and get the code returned from Notepad when it is shut down before proceeding:
ret = shell.Run("notepad " + sys.argv[0], 1, 1) print "Notepad return code:", retNow, we open a command window, change the path to C:, and execute a dir:
shell.Run("cmd /K CD C: & Dir")Note that cmd works only on Windows NT/2000/XP; on Windows 98/ME, you need to run Command instead, and this does not support the & joiner to execute two consecutive commands. The shell object has many more methods besides the Run method used throughout this recipe. For example, we can get any environment string (similar to accessing os.environ):
print shell.ExpandEnvironmentStrings("%windir%")7.16.3 Discussion This recipe shows three Windows Scripting Host (WSH) examples converted to Python. WSH documentation can be found at http://msdn.microsoft.com/library/en-us/script56/html/wsoriWindowsScriptHost.asp.
Note that this recipe shows a Python program driving WSH, so save the code to a file with extension .py rather than .pys. Extension .pys would be used if WSH was driving (i.e., via cscript.exe)-and, thus, if Python was being used via the ActiveScripting protocol. But the point of the recipe is that you don't need to rely on ActiveScripting: you can use WSH system-specific functionality, such as SendKeys and ExpandEnvironmentStrings, from within a regular Python program, further enhancing the use of Python for system administration and automating tasks in a Windows environment.
Note that you do not need to worry about closing the COM objects you create. Python's garbage collection takes care of them quite transparently. For example, if and when you want to explicitly close (release) a COM object, you can use a del statement (in this case, you need to ensure that you remove or rebind all references to the COM object that you want to release).
7.16.4 See Also Documentation for pythoncom and win32com.client in win32all (http://starship.python.net/crew/mhammond/win32/Downloads.html) or ActivePython (http://www.activestate.com/ActivePython/); Windows API documentation available from Microsoft (http://msdn.microsoft.com); Python Programming on Win32, by Mark Hammond and Andy Robinson (O'Reilly).
I RuBoard
begin 666 previous.gif M1TE&.#EA/@`/`)$``&9F9D1$1)F9F?___R'Y! ``````+ `````^`````)V MC(^IR^TLHIRTVHLS-0+X#X;B2);F)W#GRK9D&G3N3)>P_ WBP.>]Q],!A$.@ M+V<$_@`WT!)9C J)46ER>A4UE52NCQA$@K-6)6AK#G5U8V<56UZC5&ZU7?JD
@O5[*KH&6/,72,@R6(CXPJ'!V.B(1 I.2E9```[ `
end
begin 666 next.gif M1TE&.#EA*0`/`)$``&9F9D1$1)F9F?___R'Y! ``````+ `````I`````)9 MC(^IRRH/HYRT0B. WKS[#P("%I;F-P;9R9KI"@R;' T'6LV/G?O?--U9#P> MD/,3$G4VCM'(Q"5S-:&3:AWRIKA;$]O]&*>MLH]D3F_(ZO++`H]7&O2ZO0`` !.P`` ` end
begin 666 pixel.gif K1TE&.#EA`0`!`(#_`,# P ```"'Y! $`````+ `````!``$```("1 $`.P`` ` end