运行imacros并截取所有选项卡的屏幕截图

时间:2015-07-27 20:52:49

标签: python firefox imacros

我创建了一个脚本,在firefox中添加了书签之后会触发一个imacros脚本。

脚本运行imacro,并执行Alt + Tab(在获取每个选项卡的屏幕截图时更改选项卡)。做Alt + Tab的代码来自另一个SO帖子:

import subprocess
import ctypes
import datetime
import time
import os
import sys
import PIL
import PIL.ImageGrab
import win32com.client


###################################
###################################
###################################
###################################

SendInput = ctypes.windll.user32.SendInput

# C struct redefinitions 
PUL = ctypes.POINTER(ctypes.c_ulong)
class KeyBdInput(ctypes.Structure):
    _fields_ = [("wVk", ctypes.c_ushort),
                ("wScan", ctypes.c_ushort),
                ("dwFlags", ctypes.c_ulong),
                ("time", ctypes.c_ulong),
                ("dwExtraInfo", PUL)]

class HardwareInput(ctypes.Structure):
    _fields_ = [("uMsg", ctypes.c_ulong),
                ("wParamL", ctypes.c_short),
                ("wParamH", ctypes.c_ushort)]

class MouseInput(ctypes.Structure):
    _fields_ = [("dx", ctypes.c_long),
                ("dy", ctypes.c_long),
                ("mouseData", ctypes.c_ulong),
                ("dwFlags", ctypes.c_ulong),
                ("time",ctypes.c_ulong),
                ("dwExtraInfo", PUL)]

class Input_I(ctypes.Union):
    _fields_ = [("ki", KeyBdInput),
                 ("mi", MouseInput),
                 ("hi", HardwareInput)]

class Input(ctypes.Structure):
    _fields_ = [("type", ctypes.c_ulong),
                ("ii", Input_I)]

# Actuals Functions

def PressKey(hexKeyCode):

    extra = ctypes.c_ulong(0)
    ii_ = Input_I()
    ii_.ki = KeyBdInput( hexKeyCode, 0x48, 0, 0, ctypes.pointer(extra) )
    x = Input( ctypes.c_ulong(1), ii_ )
    SendInput(1, ctypes.pointer(x), ctypes.sizeof(x))

def ReleaseKey(hexKeyCode):

    extra = ctypes.c_ulong(0)
    ii_ = Input_I()
    ii_.ki = KeyBdInput( hexKeyCode, 0x48, 0x0002, 0, ctypes.pointer(extra) )
    x = Input( ctypes.c_ulong(1), ii_ )
    SendInput(1, ctypes.pointer(x), ctypes.sizeof(x))

###################################
###################################
###################################
###################################


def CtrlTab():
    '''
    Press Alt+Tab and hold Alt key for 2 seconds in order to see the overlay
    '''

    PressKey(0x11) #Ctrl
    PressKey(0x09) #Tab
    ReleaseKey(0x09) #~Tab

    time.sleep(2)       
    ReleaseKey(0x11) #~Ctrl
    time.sleep(5)   

###################################
###################################
###################################
###################################

proc = subprocess.Popen([r'C:\Program Files (x86)\Mozilla Firefox\firefox.exe', '-new-tab', 'imacros://run/?m=Demo-Firefox%5CTabs.iim'],stdin=None, stdout=None, stderr=None)
time.sleep(10)


print('a')
#CtrlTab()   

for x in list(range(0,4)):
    print('getting')
    img = PIL.ImageGrab.grab()
    print('got it')
    save_as = 'mailing_screenshots\\' + (str(datetime.datetime.now())[0:10]).replace('-','') + (str(datetime.datetime.now())[11:19]).replace(':','') + '_' + str(x) + '.jpg'

    print(save_as)          

    img.save(save_as)



    CtrlTab()

这很好用,但是这里有问题,如果我先前没有打开firefox,它就不会做Alt + Tab。如果firefox是(手动)先前打开的,Alt + Tab将起作用。

关于我应该改变什么的任何想法?唯一的问题是,如果firefox完全关闭,标签不会通过Alt +标签切换。

还有另一种方法吗?

谢谢!

0 个答案:

没有答案