python控制macos键盘鼠标
如果安装Xcode就是为了基本操作的话,不如用python,可以省去Xcode占用的40G空间
还有个类似的软件keyboard Maestro.app
,有图形化的界面
import pyautogui
from time import sleep
import pyperclip
# 获取当前鼠标位置
current_mouse_x, current_mouse_y = pyautogui.position()
print(f"当前鼠标位置: ({current_mouse_x}, {current_mouse_y})")
# 移动鼠标到指定位置 (-1880, 140) 并在0.05秒内完成移动
pyautogui.moveTo(-1880, 140, duration=0.05)
# 模拟鼠标双击
pyautogui.doubleClick()
# 模拟鼠标右键点击
pyautogui.rightClick()
# 模拟鼠标拖动操作,将鼠标从当前位置拖动到指定位置 (300, 400),并在2秒内完成
pyautogui.dragTo(300, 400, duration=2)
# 移动鼠标相对于当前位置的偏移量 (100, 100),并在0.5秒内完成
pyautogui.moveRel(100, 100, duration=0.5)
# 模拟鼠标左键点击
pyautogui.click()
# 模拟鼠标中键点击
pyautogui.middleClick()
# 模拟鼠标左键按下
pyautogui.mouseDown()
# 模拟鼠标左键抬起
pyautogui.mouseUp()
# 模拟鼠标滚轮向上滚动500个单位
pyautogui.scroll(500)
# 模拟鼠标滚轮向下滚动500个单位
pyautogui.scroll(-500)
# 截图当前屏幕并保存为文件
screenshot = pyautogui.screenshot('screenshot.png')
# 模拟键盘输入文本
pyautogui.typewrite('Hello, world!')
# 模拟按下键盘上的单个按键,例如按下 'enter' 键
pyautogui.press('enter')
# 模拟组合键按下,例如 Ctrl+C(复制)
pyautogui.hotkey('ctrl', 'c')
# 获取剪贴板内容
clipboard_content = pyperclip.paste()
print(f"剪贴板内容: {clipboard_content}")
# 将内容写入剪贴板
pyperclip.copy('复制的内容')
# 弹出一个对话框,等待用户确认
pyautogui.confirm('这是一个确认对话框。')
# 弹出一个警告对话框
pyautogui.alert('这是一个警告对话框。')
# 弹出一个输入框并获取用户输入
user_input = pyautogui.prompt('请输入内容:')
print(f"用户输入: {user_input}")
# 在特定区域内进行截图
region_screenshot = pyautogui.screenshot('region_screenshot.png', region=(0, 0, 300, 400))
# 模拟拖动鼠标,相对当前位置的偏移量 (-50, 100),并在1秒内完成
pyautogui.dragRel(-50, 100, duration=1)
# 模拟鼠标左键三击
pyautogui.tripleClick()
# 模拟滚动鼠标,指定滚动的x, y轴和点击次数
pyautogui.scroll(clicks=3, x=0, y=0)
# 图像识别并点击
# 这将找到屏幕上与提供的图像文件 `example.png` 匹配的区域,并点击该区域的中心
image_location = pyautogui.locateOnScreen('example.png')
if image_location:
print(f"图像位置: {image_location}")
pyautogui.click(image_location)
else:
print("图像未找到")
# 精确查找图像并点击中心点
# `confidence` 参数可以设置匹配度,默认是 1.0 (100%),可以降低以进行模糊匹配
image_location = pyautogui.locateOnScreen('example.png', confidence=0.8)
if image_location:
center_point = pyautogui.center(image_location)
print(f"图像中心位置: {center_point}")
pyautogui.click(center_point)
else:
print("图像未找到")
# 找到所有匹配的图像位置
all_matches = list(pyautogui.locateAllOnScreen('example.png'))
if all_matches:
print(f"找到 {len(all_matches)} 处匹配图像:")
for match in all_matches:
print(match)
pyautogui.click(match)
else:
print("图像未找到")
# 在指定的区域中查找图像
# `region` 参数表示屏幕区域 (left, top, width, height),此处只在 (0, 0) 到 (800, 600) 的区域中查找
image_location = pyautogui.locateOnScreen('example.png', region=(0, 0, 800, 600))
if image_location:
print(f"图像在指定区域的位置: {image_location}")
pyautogui.click(image_location)
else:
print("指定区域内未找到图像")
# 使用 locateCenterOnScreen 方法直接查找图像的中心点
center_point = pyautogui.locateCenterOnScreen('example.png')
if center_point:
print(f"图像中心位置: {center_point}")
pyautogui.click(center_point)
else:
print("图像未找到")
# 安全退出,禁用pyautogui的fail-safe功能
pyautogui.FAILSAFE = False
OCR:
系统自带ocr可执行文件:https://github.com/xulihang/macOCR/releases/download/build/OCR.zip
使用:
usage:
language fastmode languageCorrection image_path output_path
--langs: list suppported languages
example:
./OCR en-US,zh-Hans false true ./image.jpg out.json
或者另外一个OCR可执行文件:https://github.com/glowinthedark/subtitles-ocr/raw/master/OCR
使用:
OCR zh false false ./image.png