3

I'm using Swift and I'm trying to figure out how to control my macOS mouse with my iPhone. I figured that the first steps would be to programmatically move the MacOS mouse with a macOS app. I'm not sure what I'm missing or doing wrong.

import Cocoa
import CoreGraphics

class ViewController: NSViewController {

override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.
}

typealias CGDirectDisplayID = UInt32

func CGMainDisplayID() -> CGDirectDisplayID{

    return CGMainDisplayID()

}

func CGDisplayMoveCursorToPoint(_ display: CGDirectDisplayID,
                                _ point: CGPoint){
}

self.CGDisplayMoveCursorToPoint(CGMainDisplayID(),(25,400))

}

I get an error: "Expected Declaration" for self.CGDisplayMoveCursorToPoint(CGMainDisplayID(),(25,400))

5
  • 1
    (25,400) isn't a CGPoint, it's a tuple of type (Int, Int). You should also fix up your function name: static func moveCursor(toPoint point: CGPoint, onDisplay display: CGDirectDisplayID), and call it with: moveCursor(toPoint: CGPoint(x: 25, y: 400), onDisplay: CGMainDisplayID()). These functions don't make sense to be instance members of ViewController. I would recommend you add an extension of CGDirectDisplayID, make it a free function, or make a new type to contain these functions. Commented Jul 11, 2018 at 18:09
  • thank you brother! so the app runs without errors now, but it doesn't move the mouse to that location (or move the mouse to any location at all). any ideas? Commented Jul 11, 2018 at 18:30
  • Well of course not, your function isn't implemented (its body is empty) Commented Jul 11, 2018 at 20:40
  • haha help me out here! I have no idea what to put into the function Commented Jul 11, 2018 at 20:52
  • Idk, you have to search up what functions are available tof rthe purpose in AppKit Commented Jul 11, 2018 at 21:31

1 Answer 1

2

I have rewritten your class a little bit. This should accomplish what you want:

typealias CGDirectDisplayID = UInt32

class ViewController: NSViewController
    {

    override func viewDidLoad()
        {
        super.viewDidLoad()
        moveCursor(onDisplay: mainDisplayID(), toPoint: CGPoint(x: 25, y: 400))
        }

    public func mainDisplayID() -> CGDirectDisplayID
        {
        return CGMainDisplayID()
        }

    public func moveCursor(onDisplay display: CGDirectDisplayID, toPoint point: CGPoint)
        {
        CGDisplayMoveCursorToPoint(display, point)
        }

    }

The most important error in your current code is that your CGDisplayMoveCursorToPoint function has nothing inside its definition, so nothing will happen. Additionally, you don't want to name your functions like this since those are implemented by the Core Graphics framework and the compiler won't like that. The comments from Alexander seem to have helped you out with this.

Inside this custom moveCursor function you may call either CGDisplayMoveCursorToPoint(display, point) or CGWarpMouseCursorPosition(point) - they are both functionally equivalent, but one does not require a display ID. From there you need to call this moveCursor method inside one of your controller lifecycle methods. Alexander's suggestions are good to note, but for starting out and testing if everything works you can just put it inside viewDidLoad and the function will fire off as soon as the view is loaded.

Hope this helps!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.