Post

Replies

Boosts

Views

Activity

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
Reply to Could not cast value of type (0x10ca9be10) to 'NSString' (0x7fff86d8bbb0)
That would help if you told exactly where you get the error. We have to search in your code to find it… Is it in this part ? Which line ? 1. for i in 0 ..< jsonResult.count{ 2. 3. jsonElement = jsonResult[i] as! NSDictionary 4. let detalle = DetallesIntereses() 5. 6. let idInteres = jsonElement["idInteres"] 7. let nombreInteres = jsonElement["interesNombre"] 8. 9. detalle.idInteres = idInteres as? String 10. detalle.nombreInteres = nombreInteres as? String 11. 12. detalles.add(detalle) 13. 14. } Could you show also how MallConcierge is defined ?
Topic: Programming Languages SubTopic: Swift Tags:
Jul ’21
Reply to Swift framework localization
Have a look here : https://stackoverflow.com/questions/56917337/cannot-localize-my-swift-5-framework-localization-import-does-nothing Try to call the extended NSLocalizedString API. func NSLocalizedString(_ key: String, tableName: String? = nil, bundle: Bundle= Bundle.main, value: String = "", comment: String) -> String
Topic: Programming Languages SubTopic: Swift 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:
Replies
Boosts
Views
Activity
Jul ’21
Reply to Should I Leave 4.2 Migration Helper Functions in Place?
I usually leave the helper in place and remove them when I modify some code. But do not do it systematically. There is no performance issue. In anycase, you should first migrate to Swift5.
Topic: Programming Languages SubTopic: Swift Tags:
Replies
Boosts
Views
Activity
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:
Replies
Boosts
Views
Activity
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:
Replies
Boosts
Views
Activity
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:
Replies
Boosts
Views
Activity
Jul ’21
Reply to Search in Download / more broken ?
Bug report filed : Jul 26, 2021 at 2:55 PM – FB9403067
Replies
Boosts
Views
Activity
Jul ’21
Reply to the sample code to detect if an iOS device is jailbroken
AFAIK, there is no robust method to detect this. See discussion here: https://developer.apple.com/forums/thread/43073
Topic: Programming Languages SubTopic: Swift Tags:
Replies
Boosts
Views
Activity
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:
Replies
Boosts
Views
Activity
Jul ’21
Reply to Andorid Intentions in iOS context
Could you explain what you are looking for, more clearly ? I could develop android version with intentions and related configuration. I couldn't find a reliable resource for this feature.
Topic: App & System Services SubTopic: Core OS Tags:
Replies
Boosts
Views
Activity
Jul ’21
Reply to XCode Version
Xcode 9.2 should be the last version with 10.12.6 (Sierra).
Replies
Boosts
Views
Activity
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:
Replies
Boosts
Views
Activity
Jul ’21
Reply to Could not cast value of type (0x10ca9be10) to 'NSString' (0x7fff86d8bbb0)
That would help if you told exactly where you get the error. We have to search in your code to find it… Is it in this part ? Which line ? 1. for i in 0 ..< jsonResult.count{ 2. 3. jsonElement = jsonResult[i] as! NSDictionary 4. let detalle = DetallesIntereses() 5. 6. let idInteres = jsonElement["idInteres"] 7. let nombreInteres = jsonElement["interesNombre"] 8. 9. detalle.idInteres = idInteres as? String 10. detalle.nombreInteres = nombreInteres as? String 11. 12. detalles.add(detalle) 13. 14. } Could you show also how MallConcierge is defined ?
Topic: Programming Languages SubTopic: Swift Tags:
Replies
Boosts
Views
Activity
Jul ’21
Reply to Swift framework localization
Could you show a small extract of your localisation file ? comment: "Actual String" is just a comment, never used at runtime.
Topic: Programming Languages SubTopic: Swift Tags:
Replies
Boosts
Views
Activity
Jul ’21
Reply to How do I scan for all devices on a network using Swift?
I would implement a LAN scanner: https://stackoverflow.com/questions/26996537/list-of-connected-devices-to-local-wifi-network You could ping the local IP (usually starting 192.168.) and see who's responding. h t t p s : / / w ww.chriswrites.com/how-to-find-ip-addresses-of-devices-on-your-local-network/
Topic: Programming Languages SubTopic: Swift Tags:
Replies
Boosts
Views
Activity
Jul ’21
Reply to Swift framework localization
Have a look here : https://stackoverflow.com/questions/56917337/cannot-localize-my-swift-5-framework-localization-import-does-nothing Try to call the extended NSLocalizedString API. func NSLocalizedString(_ key: String, tableName: String? = nil, bundle: Bundle= Bundle.main, value: String = "", comment: String) -> String
Topic: Programming Languages SubTopic: Swift Tags:
Replies
Boosts
Views
Activity
Jul ’21