Skip to content

Instantly share code, notes, and snippets.

@mizchi
Created October 18, 2010 08:45
Show Gist options
  • Select an option

  • Save mizchi/631927 to your computer and use it in GitHub Desktop.

Select an option

Save mizchi/631927 to your computer and use it in GitHub Desktop.
mouse operator
#!/usr/bin/python
#-*- encoding:utf-8 -*-
"""
OSXのマウスイベントを操作するスクリプトのサンプル
require : PyObjC
v{CGPoint=dd}は環境によってddだったりffだったりした
"""
import objc
from AppKit import NSScreen
from time import sleep
def clickMouse(x, y, button): # 1が左,2が右,3がホイールボタン
bndl = objc.loadBundle('CoreGraphics', globals(), '/System/Library/Frameworks/ApplicationServices.framework')
objc.loadBundleFunctions(bndl, globals(), [('CGPostMouseEvent', 'v{CGPoint=dd}III')])
CGPostMouseEvent((x, y), 1, button, 1)
CGPostMouseEvent((x, y), 1, button, 0)
def moveMouse(x, y):
bndl = objc.loadBundle('CoreGraphics', globals(), '/System/Library/Frameworks/ApplicationServices.framework')
objc.loadBundleFunctions(bndl, globals(), [('CGWarpMouseCursorPosition', 'v{CGPoint=dd}')])
CGWarpMouseCursorPosition((x, y))
if __name__ == "__main__":
cnt = 0
for i in range(3):
moveMouse( 100+i*10, 100)
sleep(1)
clickMouse(10, 10, 1)
@eduandup
Copy link
Copy Markdown

eduandup commented Nov 3, 2019

Hi, I have tried this, but it gives me this error: line 11, in moveMouse
objc.loadBundleFunctions(bndl, globals(), [('CGWarpMouseCursorPosition', 'v{CGPoint=dd}')])
TypeError: a bytes-like object is required, not 'str'. Can you please help me?

@casiez
Copy link
Copy Markdown

casiez commented Feb 25, 2020

This is because you use python 3. Write instead objc.loadBundleFunctions(bndl,` globals(), [('CGPostMouseEvent', b'v{CGPoint=dd}III')])

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment