Once I start a new project everything seems fine and the first commit is always successful. Once I change one of the parameters e.g. set TimeInterval = 6
any consequent run would do absolutely nothing, the only thing I can do is switch back to the old value or start a new project with the new values. I'm currently stuck, anything could help, thanks!
import Foundation
import CoreGraphics
// A helper function to move the cursor smoothly from one coordinate to another.
func moveCursor(from start: CGPoint, to end: CGPoint, duration: TimeInterval) {
let steps = 100 // Number of steps for smoothness
let delay = duration / TimeInterval(steps) // Time delay between each step
for i in 0...steps {
let progress = Double(i) / Double(steps)
// Interpolating X and Y coordinates based on progress (linear interpolation)
let newX = start.x + CGFloat(progress) * (end.x - start.x)
let newY = start.y + CGFloat(progress) * (end.y - start.y)
// Move the cursor
let point = CGPoint(x: newX, y: newY)
moveCursor(to: point)
// Sleep for a short time to create a smooth movement
usleep(useconds_t(delay * 1_000_000)) // usleep takes microseconds
}
}
// Function to set the cursor position using CGEvent
func moveCursor(to point: CGPoint) {
let moveEvent = CGEvent(mouseEventSource: nil, mouseType: .mouseMoved, mouseCursorPosition: point, mouseButton: .left)
moveEvent?.post(tap: .cghidEventTap)
}
// Main function to take multiple coordinates and move between them
func moveCursorAlongPath(coords: [CGPoint], totalTime: TimeInterval) {
guard coords.count > 1 else { return }
let segmentTime = totalTime / TimeInterval(coords.count - 1) // Split time between segments
for i in 0..<(coords.count - 1) {
let start = coords[i]
let end = coords[i + 1]
moveCursor(from: start, to: end, duration: segmentTime)
}
}
// Example usage:
// Coordinates you want to move between
let coordinates: [CGPoint] = [CGPoint(x: 100, y: 100), CGPoint(x: 400, y: 400), CGPoint(x: 800, y: 200)]
let totalDuration: TimeInterval = 5 // Total duration in seconds
// Move the cursor smoothly along the path
moveCursorAlongPath(coords: coordinates, totalTime: totalDuration)```
