Skip to content

Automate packing with Enigma Virtual Box

Alex edited this page Feb 12, 2022 · 5 revisions

Here is the script that was used to generate the Enigma Virtual Box samples using GUI automation:

import codext
import os
import pyautogui as pag
import pyperclip
import time

HOME = os.path.join(os.path.expanduser("~"), "Desktop")
PBAR_IMG_B64 = b"""iVBORw0KGgoAAAANSUhEUgAAAioAAAAmCAYAAADqbbDhAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJ
cEhZcwAADsMAAA7DAcdvqGQAAADaSURBVHhe7dZBDYNAAADBvuqk/hFwos4BGIDkkkLYxzzGw3zmnDsAQJGoAABZogIAZIkKAJAlKgBAlqgA
AFmiAgBkLUVljAEAcIuza1xZjsp3+wEA/EVUAIAsUQEAskQFAMgSFQAgS1QAgCxRAQCyRAUAyBIVACBLVACALFEBALJEBQDIEhUAIEtUAIAs
UQEAskQFAMgSFQAgS1QAgCxRAQCyRAUAyBIVACBLVACALFEBALIeiwoAwB3OrnFlKSoAAG8QFQAgS1QAgCxRAQCyRAUAyBIVACBq7gduIA4o
ALAyugAAAABJRU5ErkJggg=="""
PBAR_IMG = os.path.join(HOME, "enigmavb-progressbar.png")
with open(PBAR_IMG, 'wb') as f:
	f.write(codext.decode(PBAR_IMG_B64, "base64"))

# setting: Enigma Virtual Box is open at its default position
def pack(filename):
    fin = os.path.join(HOME, "not-packed", filename)
    fout = os.path.join(HOME, "Enigma Virtual Box", "enigmavb_" + filename)
    # click on "Enter Input File Name" field and fill in the input file
    pag.click(580, 240)
    pag.hotkey("ctrl", "a")
    pag.press("backspace")
    type_path(fin)
    # click on "Enter Output File Name" field and fill in the output file
    pag.click(580, 280)
    pag.hotkey("ctrl", "a")
    pag.press("backspace")
    type_path(fout)
    # click on the "Process" button
    pag.click(1140, 670)
    # wait for the button to become "Close" and then click on it
    button = None
    while not button:
        time.sleep(.5)
        button = pag.locateCenterOnScreen(PBAR_IMG, grayscale=True)
    pag.click(1070, 570)
    print("SUCCESS")
    # now clear the input fields
    pag.click(580, 240)
    pag.hotkey("ctrl", "a")
    pag.press("backspace")
    pag.click(580, 280)
    pag.hotkey("ctrl", "a")
    pag.press("backspace")

# this function is a workaround to this issue:
#  https://stackoverflow.com/questions/59604162/typewrite-character-with-pyautogui
def type_path(path):
    if path.startswith("C:"):
        path = path[2:]
        pag.typewrite("C", interval=.01)
        pyperclip.copy(":")
        pag.hotkey("ctrl", "v", interval=.01)
    pag.typewrite(path, interval=.01)

last_one = None
for f in os.listdir("not-packed"):
    fout = os.path.join(HOME, "Enigma Virtual Box", "enigmavb_" + f)
    if os.path.exists(fout):
        last_one = f

for f in os.listdir("not-packed"):
    if last_one:
        if f == last_one:
            last_one = None
        continue
    m = "Packing '%s'..." % f
    m += " " * (40 - len(f))
    print(m, end="\t")
    pack(f)

os.remove(PBAR_IMG)