Post

Replies

Boosts

Views

Activity

Reply to Need Help Transfer Data From Button
What I would do here, which is probably the simplest: declare a var in VC var selectedNameInCell : String? get it in willSelectRowAt or in didSelectRowAt if you prefer: override func tableView(_ tableView: UITableView, willSelectRowAt indexPath: IndexPath) -> IndexPath? { // what is the dataSource ? I assume it is an [Item] ; I assume you have a single section selectedNameInCell = dataSource[indexPath.row].yahooName return nil // cancel selection of cell } Now you can use in prepare override func prepare(for segue: UIStoryboardSegue, sender: Any?) { if segue.identifier == "hs" { if let destinationController = segue.destination as? HighlightsVC, let destName = selectedNameInCell { destinationController!.playerName = destName } } }
Topic: Programming Languages SubTopic: Swift Tags:
Jul ’21
Reply to Semaphore
You should watch WWDC sessions an async/wait. They explain in detail how to use, how it compares with completion handlers and semaphores. There are 5 or 6 of them, really instructive.
Topic: Programming Languages SubTopic: Swift Tags:
Jul ’21
Reply to emulate mouse click
When you post code, please use code formatter tool (<>) Could you detail what you want to do using hid ? let position = NSPoint(100,75) CGDisplayMoveCursorToPoint(0, position) let source = CGEventSource.init(stateID: .hidSystemState) let eventDown = CGEvent(mouseEventSource: source, mouseType: .leftMouseDown, mouseCursorPosition: position , mouseButton: .left) let eventUp = CGEvent(mouseEventSource: source, mouseType: .leftMouseUp, mouseCursorPosition: position , mouseButton: .left) eventDown?.post(tap: .cghidEventTap) usleep(50_000) eventUp?.post(tap: .cghidEventTap) Did you check your events are created correctly ? Note: Is it the real code ? The call let position = NSPoint(100,75) appears incorrect. Should be         let position = NSPoint(x: 100, y: 75)
Topic: Programming Languages SubTopic: Swift Tags:
Jul ’21
Reply to emulate mouse click
It is always risky to reuse code we don't understand… I suppose you got it here: https://stackoverflow.com/questions/49082243/swift-4-simulating-left-click-programmatically-causes-right-click Did you notice that require disabling sandboxing (may be the cause of the problem). Here is what I do to modify an event for a mouse click (using perform). let newMouseDownEvent = NSEvent.mouseEvent( with: .leftMouseDown, location: theEvent.locationInWindow, // Use your own location modifierFlags: NSEvent.ModifierFlags.control, timestamp: theEvent.timestamp, windowNumber: theEvent.windowNumber, context: theEvent.context, eventNumber: theEvent.eventNumber+1, clickCount: theEvent.clickCount, pressure: theEvent.pressure) self.perform(#selector(NSResponder.self.mouseDown(with:)), with: newMouseDownEvent) In your case, you could try to use NSEvent: class func mouseEvent(with type: NSEvent.EventType, location: NSPoint, modifierFlags flags: NSEvent.ModifierFlags, timestamp time: TimeInterval, windowNumber wNum: Int, context unusedPassNil: NSGraphicsContext?, eventNumber eNum: Int, clickCount cNum: Int, pressure: Float) -> NSEvent? let eventDown = NSEvent(…) self.perform(#selector(NSResponder.self.mouseDown(with:)), with: eventDown) Parameters type: One of the modifier key masks described in NSEvent.EventType, or an NSInternalInconsistencyException is raised. location: The cursor location in the base coordinate system of the window specified by windowNum. flags: An integer bit field containing any of the modifier key masks described in Getting Unicode Values, combined using the C bitwise OR operator. time: The time the event occurred in seconds since system startup. windowNum: An integer that identifies the window device associated with the event, which is associated with the NSWindow that will receive the event. context: The display graphics context of the event. eventNumber: An identifier for the new event. It’s normally taken from a counter for mouse events, which continually increases as the application runs. clickNumber: The number of mouse clicks associated with the mouse event. pressure: A value from 0.0 to 1.0 indicating the pressure applied to the input device on a mouse event, used for an appropriate device such as a graphics tablet. For devices that aren’t pressure-sensitive, the value should be either 0.0 or 1.0.
Topic: Programming Languages SubTopic: Swift Tags:
Jul ’21
Reply to Command line in Xcode
Is it possible to create a project with multiple 'programmes' within it?  AFAIK, not directly. But you can create an app, where the first window will let you select which 'programme' to run, in its won window? get my programme to compile and run in terminal rather than the window at the bottom of Xcode Program does not run at the bottom. What you see there are console messages. The app runs independently and sends information to Xcode (when launched from Xcode). Run in Terminal: this should help you: h t t p s : / / mahmudahsan.medium. com/running-and-compiling-swift-code-in-terminal-237ee4087a9c
Jul ’21
Reply to emulate mouse click
disabling Sandbox is not an option You're right. The code I posted was to modify a received event. Your case is different, you want to create a brand new one. Have a look at this func, it finds windNum outside of your app.: windowNumber(at:belowWindowWithWindowNumber:) Returns the number of the frontmost window that would be hit by a mouse-down at the specified screen location. https://developer.apple.com/documentation/appkit/nswindow/1419210-windownumber
Topic: Programming Languages SubTopic: Swift Tags:
Jul ’21
Reply to In UIKit, how do you present a confirmation dialog when the user is swiping to delete a table view cell?
I did it using semaphore. There are now better ways in iOS15 with async/wait. func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) { if editingStyle == .delete { var delete = false let titre = "Suppress row" let detailMsg = "Do you confirm row suppression?" let controller = UIAlertController(title: titre, message: detailMsg, preferredStyle: .alert) DispatchQueue.global(qos: .userInitiated).async { let semaphore = DispatchSemaphore(value: 0) // So that delete test be completed let okAction = UIAlertAction(title: "I confirm", style: .destructive) { (handler) in delete = true semaphore.signal() } let cancelAction = UIAlertAction(title: "No!", style: .default) { (handler) in delete = false semaphore.signal() } DispatchQueue.main.async { controller.addAction(okAction) controller.addAction(cancelAction) self.present(controller, animated: true, completion: nil) } let _ = semaphore.wait(timeout: .distantFuture) if delete { // Proceed with deletion } } } }
Topic: UI Frameworks SubTopic: UIKit Tags:
Jul ’21
Reply to emulate mouse click
I got it work (with help of this): https://stackoverflow.com/questions/47152551/activate-a-window-using-its-window-id You need to activate the window you select. Here the changes I made (I connected to a button for easier testing instead of tryToActivateWindow() ) ; I also changed the NSPoint, but that's a detail. Here is the modified code: func switchToApp(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) } } // } // // func tryToActivateWindow() { @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 print("Creating MouseDown event") let newMouseDownEvent = NSEvent.mouseEvent( with: .leftMouseDown, location: mousePosition, //NSPoint(x: 205,y: 200), // this should be in the hitWindow modifierFlags: NSEvent.ModifierFlags.control, timestamp: ProcessInfo.processInfo.systemUptime, windowNumber: numberOfHitWindow, context: nil, eventNumber: 0, //don't know what else to use clickCount: 1, pressure: 1) self.perform(#selector(NSResponder.self.mouseDown(with:)), with: newMouseDownEvent) print("Sleeping for a bit") Thread.sleep(forTimeInterval: 0.1) print("Woke up") print("Creating MouseUp event") let newMouseUpEvent = NSEvent.mouseEvent( with: .leftMouseUp, location: mousePosition, //NSPoint(x: 205,y: 200), // this should be in the hitWindow modifierFlags: NSEvent.ModifierFlags.control, timestamp: ProcessInfo.processInfo.systemUptime, windowNumber: numberOfHitWindow, context: nil, eventNumber: 1, //don't know what else to use clickCount: 1, pressure: 1) self.perform(#selector(NSResponder.self.mouseUp(with:)), with: newMouseUpEvent) print("end -----------") switchToApp(withWindow: Int32(numberOfHitWindow)) // <---- THAT'S THE KEY POINT } As for counter, 0 and 1 work: https://stackoverflow.com/questions/2662128/how-to-get-the-current-eventnumber-for-creating-an-event-with-nsevent For your other question: In a related question: how does one emulate a double-click? Just pas 2 as a value for clickCount in newMouseDownEvent. If that works, don't forget to close the thread on the correct answer. Good luck.
Topic: Programming Languages SubTopic: Swift Tags:
Jul ’21
Reply to emulate mouse click
Just some ideas: is inWindowClickpoint in the window's coordinate (not absolute screen position ?) If I read correctly, post() works only for keyboard. For mouse, one should use eventDown?.post(tap: .cghidEventTap)
Topic: Programming Languages SubTopic: Swift Tags:
Jul ’21