LoginSignup
2
5

More than 1 year has passed since last update.

一番上のファイルに行ったGUI操作を同一フォルダ内の全てに適用させる

Last updated at Posted at 2022-03-28

対象

・コード書けない人
・コード書くの面倒な人
・コード書く時間がない人

10分程度で実装できます。

できること

・ファイルの一括操作

使い方

step1「自動化プログラム作成」
(1)作成用プログラムをデスクトップに保存する。

(2)画面右半分にターミナルを起動する。
スクリーンショット 2022-03-30 23-31-35.png
(3)画面左半分に繰り返したいフォルダを開く。
スクリーンショット 2022-03-30 23-30-31.png
(4)ターミナルに「pip install pyautogui」と入力する。(初回のみ)

terminal
pip install pyautogui

(5)「python」と打ち、続けて作成用のプログラム(auto1.py)をドラッグ&ドロップして貼り付ける。
実行する。

terminal
python /~~/~~/~~/desktop/auto1.py

スクリーンショット 2022-03-30 23-32-38.png
(6)1を押して5秒以内に一番上のファイルにカーソルを合わせる。(次のプログラムが始まるまでカーソル固定)
スクリーンショット 2022-03-30 23-30-31.png
(7)4→2の順に押す。(ここで一番上のファイルが開いたことになる)
(8)一番上のファイルを画面左半分に開く。

スクリーンショット 2022-03-30 23-30-49.png
(9)自動化させたい処理を入力していく。
(10)3→0の順に押すとファイルを保存して閉じる。
(11)繰り返したい数を入力する。
(12)デスクトップに自動化ファイルが完成する。

step2「自動化プログラム起動」
(1)画面右半分にターミナルを開く
(2)画面左半分に繰り返したいフォルダを開く
(3)step1で作られたプログラムをドラッグ&ドロップ、実行

terminal
python /~~/~~/~~/desktop/step1で作られたプログラム.py

作成用プログラム(windowsの場合)

auto1.py
import pyautogui as p
import time
import re

def where_click(c,x,i):
    print("5秒以内にカーソルを合わせてください")
    time.sleep(5)
    x = p.position()
    print(f"{c}回目にクリックする位置情報を保存しました!")
    return x

#デスクトップにファイルを新規作成  
file_path = input("デスクトップまでのパスを入力してください")
file_name = r"/" + input("ファイル名を入力してください") + ".py"
my_file = file_path + file_name
print(my_file)
open(my_file,"w",encoding="utf-8")
text = "import pyautogui as p\nimport time\n\ndef automation(s):\n"
with open(my_file,"a",encoding="utf-8") as file:
    file.write(text)

x = "a"
i = 0
c = 1
s = 0
y = True

while y:

    option = int(input("シングルクリックする→1\nファイルを開く→2\nファイルを保存して閉じる→3\n一番上のファイルを選択→4\n終了する場合→0\n"))

    if option == 1:
        j = where_click(c,x,i)
        text = re.findall(r"\((.+)\)", str(j))
        with open(my_file, "a", encoding="utf-8") as file:
            file.write("    p.click(" + text[0] + ")\n")
            file.write("    time.sleep(2) \n")
        x += str(i)
        i += 1
        c += 1
    elif option == 2:
        text = "    p.hotkey('ctrl', 'o')\n    time.sleep(2)\n"
        with open(my_file, "a", encoding="utf-8") as file:
            file.write(text)
    elif option == 3:
        text = "    p.hotkey('ctrl', 's')\n    time.sleep(2)\n"
        with open(my_file, "a", encoding="utf-8") as file:
            file.write(text)
        time.sleep(2)
        text = "    p.hotkey('ctrl', 'w')\n    time.sleep(2)\n"
        with open(my_file, "a", encoding="utf-8") as file:
            file.write(text)
        time.sleep(2)
        text = "    p.hotkey('ctrl', 'q')\n    time.sleep(2)\n"
        with open(my_file, "a", encoding="utf-8") as file:
            file.write(text)
    elif option == 4:
        text = "    p.press('up')\n    time.sleep(2)\n"
        with open(my_file, "a", encoding="utf-8") as file:
            file.write(text)
        text = "    if s > 0:\n        p.press(['down'] * s)\n    time.sleep(2)\n"
        with open(my_file, "a", encoding="utf-8") as file:
            file.write(text)
    elif option == 0:
        y = False
    else :
        print("操作なし")

loop_count = int(input("繰り返す回数を入力してください"))
time.sleep(2)
text = f"\ns = 0\n\nfor i in range({loop_count}):\n    automation(s)\n    time.sleep(2)\n    s += 1\n"
with open(my_file, "a", encoding="utf-8") as file:
    file.write(text)

作成用プログラム(Macの場合)

auto1.py
import pyautogui as p
import time
import re

def where_click(c,x,i):
    print("5秒以内にカーソルを合わせてください")
    time.sleep(5)
    x = p.position()
    print(f"{c}回目にクリックする位置情報を保存しました!")
    return x

#デスクトップにファイルを新規作成  
file_path = input("デスクトップまでのパスを入力してください")
file_name = r"/" + input("ファイル名を入力してください") + ".py"
my_file = file_path + file_name
print(my_file)
open(my_file,"w",encoding="utf-8")
text = "import pyautogui as p\nimport time\n\ndef automation(s):\n"
with open(my_file,"a",encoding="utf-8") as file:
    file.write(text)

x = "a"
i = 0
c = 1
s = 0
y = True

while y:

    option = int(input("シングルクリックする→1\nファイルを開く→2\nファイルを保存して閉じる→3\n一番上のファイルを選択→4\n終了する場合→0\n"))

    if option == 1:
        j = where_click(c,x,i)
        text = re.findall(r"\((.+)\)", str(j))
        with open(my_file, "a", encoding="utf-8") as file:
            file.write("    p.click(" + text[0] + ")\n")
            file.write("    time.sleep(2) \n")
        x += str(i)
        i += 1
        c += 1
    elif option == 2:
        text = "    p.hotkey('command', 'o')\n    time.sleep(2)\n"
        with open(my_file, "a", encoding="utf-8") as file:
            file.write(text)
    elif option == 3:
        text = "    p.hotkey('command', 's')\n    time.sleep(2)\n"
        with open(my_file, "a", encoding="utf-8") as file:
            file.write(text)
        time.sleep(2)
        text = "    p.hotkey('command', 'w')\n    time.sleep(2)\n"
        with open(my_file, "a", encoding="utf-8") as file:
            file.write(text)
        time.sleep(2)
        text = "    p.hotkey('command', 'q')\n    time.sleep(2)\n"
        with open(my_file, "a", encoding="utf-8") as file:
            file.write(text)
    elif option == 4:
        text = "    p.press('up')\n    time.sleep(2)\n"
        with open(my_file, "a", encoding="utf-8") as file:
            file.write(text)
        text = "    if s > 0:\n        p.press(['down'] * s)\n    time.sleep(2)\n"
        with open(my_file, "a", encoding="utf-8") as file:
            file.write(text)
    elif option == 0:
        y = False
    else :
        print("操作なし")

loop_count = int(input("繰り返す回数を入力してください"))
time.sleep(2)
text = f"\ns = 0\n\nfor i in range({loop_count}):\n    automation(s)\n    time.sleep(2)\n    s += 1\n"
with open(my_file, "a", encoding="utf-8") as file:
    file.write(text)

Reference

2
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
5