2015-10-13 53 views
0

我想寫一個程序,可以:程序自動化軟件使用

打開一個應用程序。 (我使用subprocess模塊成功執行應用程序)

使用該應用程序。 (如何在特定軟件上自動執行任務?是否有可能?例如,軟件需要我登錄和python,並自動使用我的憑據登錄,並繼續使我的典型用法(如打開項目)自動化)

關閉應用。

from datetime import datetime 
import time 
import os 
import subprocess 

os.chdir("../../Desktop/Sublime/Sublime Text 2") #Directory need to adjust to find teamviewer 

print "Starting python..." 

switch = True 
activator1 = True 
activator2 = True 

startBoolean = True 
exitBoolean = True 
container = None 

def startTimer(startTime , exitTime): 
    global dateTime 
    global startBoolean 
    global exitBoolean 
    dateTime = datetime.now().strftime('%H%M%S') 
    print "Current Time: " +dateTime+ "| Start Time: " +startTime +"| Exit Time: "+exitTime 
    if dateTime >= startTime: 
     startBoolean = False 
     print "start boolean: "+ str(startBoolean) 
    if dateTime >= exitTime: 
     exitBoolean = False 
     print "exit boolean: "+ str(exitBoolean) 
    return [startBoolean,exitBoolean] 

def startApplication(startTime): 
    if dateTime >= startTime: 
     print "Launching Application..." 
     process = subprocess.Popen(["./sublime_text"],shell=True) #This is using ubuntu terminal command. Might need to change the command to fits other OS. 

def stopApplication(exitTime): 
    if dateTime >= exitTime: 
     print "Exiting Application..." 
     process = subprocess.Popen(["killall sublime_text"],shell=True) 

startTime = raw_input("Project Launch Time: ") 
exitTime = raw_input("Project Exit time: ") 

while switch: 
    startTimer(startTime , exitTime) 
    global container 
    container = startTimer(startTime , exitTime) 
    if container!=None: 
     if activator1==True: 
      if container[0] == False: 
       print "clear1" 
       activator1 = False #Cannot activate < 1. 
       startApplication(startTime) 
     if activator2==True: 
      if container[1] == False: 
       print "clear2" 
       activator2 = False #Cannot activate < 1. 
       stopApplication(exitTime) 
+0

如果您在Windows上,您可能需要考慮AutoHotKey(http://www.autohotkey.com/)而不是Python來自動化軟件使用。請小心將您的憑證存儲在某人可能能夠閱讀的文件中。 –

+0

大家好。我剛剛發佈了我的所有代碼。我只是崇高的例子。 –

+0

@geoffreykoh我可以知道什麼輸出你的專業! – Ravichandra

回答

1

嗨,你可以使用pyAutoIt

我給示例代碼記事本將其保存在居民點和需要的路徑。

import autoit,time 
autoit.run("notepad.exe") 
autoit.win_wait_active("Untitled - Notepad") 
time.sleep(5) 
autoit.control_send("Untitled - Notepad", "Edit1", "hello world{!}") 
autoit.control_send("Untitled - Notepad", "Edit1", "test-2") 
#time.sleep(5) 
autoit.win_close("Untitled - Notepad") 
autoit.control_click("Notepad", "Button1") 
#time.sleep(5) 
autoit.win_wait_active("Save As") 
#time.sleep(5) 
autoit.control_send("Save As", "Edit1","Path-to-save") 
autoit.control_send("Save As", "Edit1", "file-name") 
autoit.control_click("Save As", "Button1") 
0

pywinauto也是很棒的GUI自動化解決方案。

實施例:

from pywinauto import Application 

app = Application().start(r"C:\Program Files\Sublime Text 2\sublime_text.exe") 

app.untitled_SublimeText2.Wait('ready', timeout=10) # just for possible slow start 
app.untitled_SublimeText2.MenuSelect('File->Open file') 
app.Open.FilenameEdit.SetText('path to the project') 
app.Open.Open.Click() 
app.Open.WaitNot('visible') # just to make sure it's closed 

main_window = app["project_name - Sublime Text 2"] 

# print access names for all available controls on the main window 
main_window.PrintControlIdentifiers() 

還有GUI幫手SWAPY,pywinauto對象檢查和代碼生成器。