Post

Replies

Boosts

Views

Activity

Reply to emulate mouse click
Did you try set the source (and not leave nil)? let source = CGEventSource.init(stateID: .hidSystemState) let position = CGPoint(x: 75, y: 100) let eventDown = CGEvent(mouseEventSource: source, mouseType: .leftMouseDown, mouseCursorPosition: position , mouseButton: .left) let eventUp = CGEvent(mouseEventSource: source, mouseType: .leftMouseUp, mouseCursorPosition: position , mouseButton: .left)
Topic: Programming Languages SubTopic: Swift Tags:
Jul ’21
Reply to Could not cast value of type 'Swift.Array.
You don't show enough code (as usual 😊). How is MapDataManager defined ? (one need to register to the Packt to see it). What is RestaurantItem. Does it have a property like mkAnnotation (I guess so) But, in any case: manager.annotations is an array of MKAnnotation hence you cannot make it a single MKAnnotation In addition, you pass annontations (with a typo for annotations) to addMap and never use it ! So following is a best guess : May be you should have something like: func addMap(_ annotations: [RestaurantItem]) { // mapView.setRegion(manager.currentRegion(latDelta: 0.5, longDelta: 0.5), animated: true) for restaurant in annotations { mapView.addAnnotation(restaurant.mkAnnotation as! MKAnnotation) } } So, please provide enough information to confirm.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Jul ’21
Reply to emulate mouse click
Indeed, the following works perfectly on MacOS 10.15.7. func switchToAppAndClick(withWindow windowNumber: Int32) { let options = CGWindowListOption(arrayLiteral: CGWindowListOption.excludeDesktopElements, CGWindowListOption.optionOnScreenOnly) let windowListInfo = CGWindowListCopyWindowInfo(options, CGWindowID(0)) guard let infoList = windowListInfo as NSArray? as? [[String: AnyObject]] else { return } if let window = infoList.first(where: { ($0["kCGWindowNumber"] as? Int32) == windowNumber}), let pid = window["kCGWindowOwnerPID"] as? Int32 { let app = NSRunningApplication(processIdentifier: pid) app?.activate(options: .activateIgnoringOtherApps) let inWindowClickpoint = CGPoint(x: 100 , y:200) // a clickable point in the targeted window coords print("creating eventMouseDown and Up events") let source = CGEventSource.init(stateID: .hidSystemState) let position = CGPoint(x: 75, y: 100) let eventDown = CGEvent(mouseEventSource: source, mouseType: .leftMouseDown, mouseCursorPosition: position , mouseButton: .left) let eventUp = CGEvent(mouseEventSource: source, mouseType: .leftMouseUp, mouseCursorPosition: position , mouseButton: .left) print("posting \(eventUp)") eventDown?.post(tap: .cghidEventTap) Thread.sleep(forTimeInterval: 0.1) print("posting \(eventDown)") eventUp?.post(tap: .cghidEventTap) } } @IBAction func tryToActivateWindow(_ sender: NSButton) { print("begin -----------") print(self.view.window?.windowNumber) let mousePosition = NSPoint(x: 205,y: 200) // This corresponds to a visible window that's not part of this App print("Mouse coordinates (\(mousePosition.x),\(mousePosition.y))") let screenHeight = NSScreen.main!.frame.height print("Screenheight: \(screenHeight)") var screenPosition: NSPoint = mousePosition screenPosition.y = screenHeight - mousePosition.y print("Screen coordinates (\(screenPosition.x),\(screenPosition.y))") let numberOfHitWindow = NSWindow.windowNumber(at: screenPosition, belowWindowWithWindowNumber: 0) print("Window hit: \(numberOfHitWindow)") CGDisplayMoveCursorToPoint(0, mousePosition) // Just for visual reference switchToAppAndClick(withWindow: Int32(numberOfHitWindow)) } So, I tried on BigSur 11.5. I was asked to authorise accessibility control. What I did. But then it does not work. Hopefully, I found this: https://stackoverflow.com/questions/65073294/is-it-possible-to-simulate-click-using-cgevent-for-ios-app-on-mac-with-m1 Which explains: After the last Big Sur update this code works, but you need to focus window first time. if(firstTime) testClick() testClick() So I adapted the code. No need to test for firstTime. And now, IT WORKS ! Strange, but it works: func testClick() { let source = CGEventSource.init(stateID: .hidSystemState) let position = CGPoint(x: 75, y: 100) let eventDown = CGEvent(mouseEventSource: source, mouseType: .leftMouseDown, mouseCursorPosition: position , mouseButton: .left) let eventUp = CGEvent(mouseEventSource: source, mouseType: .leftMouseUp, mouseCursorPosition: position , mouseButton: .left) print("posting \(eventUp)") eventDown?.post(tap: .cghidEventTap) Thread.sleep(forTimeInterval: 0.1) print("posting \(eventDown)") eventUp?.post(tap: .cghidEventTap) } func switchToAppAndClick(withWindow windowNumber: Int32) { let options = CGWindowListOption(arrayLiteral: CGWindowListOption.excludeDesktopElements, CGWindowListOption.optionOnScreenOnly) let windowListInfo = CGWindowListCopyWindowInfo(options, CGWindowID(0)) guard let infoList = windowListInfo as NSArray? as? [[String: AnyObject]] else { return } if let window = infoList.first(where: { ($0["kCGWindowNumber"] as? Int32) == windowNumber}), let pid = window["kCGWindowOwnerPID"] as? Int32 { let app = NSRunningApplication(processIdentifier: pid) app?.activate(options: .activateIgnoringOtherApps) let inWindowClickpoint = CGPoint(x: 100 , y:200) // a clickable point in the targeted window coords print("creating eventMouseDown and Up events") testClick() testClick() // <----- CALL IT TWICE !!!! } } @IBAction func tryToActivateWindow(_ sender: NSButton) { print("begin -----------") print(self.view.window?.windowNumber) let mousePosition = NSPoint(x: 205,y: 200) // This corresponds to a visible window that's not part of this App print("Mouse coordinates (\(mousePosition.x),\(mousePosition.y))") let screenHeight = NSScreen.main!.frame.height print("Screenheight: \(screenHeight)") var screenPosition: NSPoint = mousePosition screenPosition.y = screenHeight - mousePosition.y print("Screen coordinates (\(screenPosition.x),\(screenPosition.y))") let numberOfHitWindow = NSWindow.windowNumber(at: screenPosition, belowWindowWithWindowNumber: 0) print("Window hit: \(numberOfHitWindow)") CGDisplayMoveCursorToPoint(0, mousePosition) // Just for visual reference switchToAppAndClick(withWindow: Int32(numberOfHitWindow)) }
Topic: Programming Languages SubTopic: Swift Tags:
Jul ’21
Reply to Earlier versions of Xcode not working
You should be able to do this directly in more recent Xcode. Open project. You get this alert: Type OK and open Issues Navigator. Find the warning for project Settings: Click on each alert and validate changes when requested. You should then get a compilation error, with Swift version undefined: Open Project settings and search for Swift There, in Swift Language Version, select Swift 5. Now project should compile (in fact you will get a lot of API errors because of changes since Xcode 8), but project can now be handled in the recent Xcode.
Topic: Programming Languages SubTopic: Swift Tags:
Jul ’21
Reply to emulate mouse click
Did you try to send mouseDown event twice, with no delay ? This thread starts to be a bit too long. You should close it (mark one of my answers that suits best) and open a new one for double click if this doesn't work.
Topic: Programming Languages SubTopic: Swift Tags:
Jul ’21
Reply to WWDC 2020 Fruta version
From the second link https://developer.apple.com/sample-code/wwdc/2020/?q=fruta Click View Code https://developer.apple.com/documentation/swiftui/fruta_building_a_feature-rich_app_with_swiftui And you will get the page to download (click "download" button). What is it that doesn't work ?
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Jul ’21
Reply to Cannot use instance member 'videoName' within property initializer; property initializers run before 'self' is available
Even if you are in a hurry (should have started earlier may be 😊), take time to format code with code formatter tool (<>). struct ExercisingSessionView: View {      let exerciseName: String   let videoName: String        @State var player = AVPlayer(url: URL(fileURLWithPath: Bundle.main.path(forResource: "squats", ofType: "mov")!))    @State var isplaying = false    @State var showcontrols = false      var body: some View {           CustomVideoPlayer(player: $player)       .frame(width: 390, height: 219)       .onTapGesture {         self.showcontrols = true                 }   }       struct CustomVideoPlayer : UIViewControllerRepresentable {            @Binding var player: AVPlayer           func makeUIViewController(context: UIViewControllerRepresentableContext) -> AVPlayerViewController {               let controller = AVPlayerViewController()       controller.player = player       controller.showsPlaybackControls = false       return controller     }           func updateUIViewController(_ uiViewController: AVPlayerViewController, context: UIViewControllerRepresentableContext) {             }   } } A few questions: here do you set videoName, how ? To clear the error, create an init(), and give some initial value to videoName: let videoName: String = "" // To have it initialised. But where do you set it ? @State var player = AVPlayer(url: URL(fileURLWithPath: Bundle.main.path(forResource: videoName, ofType: "mov")!)) init() { player = videoName }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Jul ’21
Reply to Could not cast value of type (0x10ca9be10) to 'NSString' (0x7fff86d8bbb0)
So, the error is here : func selectDeselectCell(tableView: UITableView, indexPath: IndexPath) { self.selectIntereses.removeAll() if let arr = listaInteresesTableView.indexPathsForSelectedRows { for index in arr { selectIntereses.append(feedInterests[indexPath.row] as! String) } } print(selectIntereses) } I don't understand your code: you loop for index, but index is nowhere used ! did you mean: selectIntereses.append(feedInterests[index.row] as! String) Now, feedInterests is set here: func interesesDownload(interest: NSArray) { feedInterests = interest self.listaInteresesTableView.reloadData() } when called by: self.delegate.interesesDownload(interest: detalles) So, feedInterests is an array (NSArray) of DetallesIntereses Hence, each item is DetallesIntereses, not a String. So, you probably want to call: selectIntereses.append(feedInterests[index.row].nombreInteres as! String) or if let interest = feedInterests[index.row] as? DetallesIntereses, let nombre = interest.nombreInteres { selectIntereses.append(nombre) } Note: there are several issues with your code: why use NSMutableArray, NSArray, … and not simply Array ? nombreInteres is a String? why test for String ? forcing a downcast (as!) as you do all over the code is risky (crash if JSON returns a nil). You'd better do : if let nombre = feedInterests[index.row].nombreInteres { selectIntereses.append(nombre) }
Topic: Programming Languages SubTopic: Swift Tags:
Jul ’21