Xcodeで所謂マウスクリックツールを作りたく思い、まずこちらのサイトを参考に(というかコピペして)実行するとマウスをクリックするというplaygroundを作ってみたのですが、うまくいきませんでした。どこを直せばいいでしょうか
swift
1import Cocoa 2import AppKit 3 4func leftClickDown(position: CGPoint) { 5 let source = CGEventSource(stateID: CGEventSourceStateID.hidSystemState) 6 let event = CGEvent(mouseEventSource: source, mouseType: CGEventType.leftMouseDown, 7 mouseCursorPosition: position, mouseButton: CGMouseButton.left) 8 event?.post(tap: CGEventTapLocation.cghidEventTap) 9} 10 11func leftClickUp(position: CGPoint) { 12 let source = CGEventSource(stateID: CGEventSourceStateID.hidSystemState) 13 let event = CGEvent(mouseEventSource: source, mouseType: CGEventType.leftMouseUp, 14 mouseCursorPosition: position, mouseButton: CGMouseButton.left) 15 event?.post(tap: CGEventTapLocation.cghidEventTap) 16} 17 18func leftDragged(to: CGPoint, from: CGPoint) { 19 leftClickDown(position: from) 20 let source = CGEventSource(stateID: CGEventSourceStateID.hidSystemState) 21 let event = CGEvent(mouseEventSource: source, mouseType: CGEventType.leftMouseDragged, 22 mouseCursorPosition: to, mouseButton: CGMouseButton.left) 23 event?.post(tap: CGEventTapLocation.cghidEventTap) 24 leftClickUp(position: to) 25} 26// 現在のマウス座標にてクリックを行う 27var location = NSEvent.mouseLocation 28for screen in NSScreen.screens { 29 if screen.frame.contains(location) { 30 location = CGPoint(x: location.x, y: NSHeight(screen.frame) - location.y) 31 break 32 } 33} 34leftClickDown(position: location) 35leftClickUp(position: location) 36
エラーメッセージ
error: Couldn't lookup symbols: _CGEventCreateMouseEvent _CGEventCreateMouseEvent
あなたの回答
tips
プレビュー