Moving windows programmatically on MacOS in Swift - swift

I'm trying to move a window programmatically on my desktop. For this I've tried:
let options = CGWindowListOption(arrayLiteral: .excludeDesktopElements, .optionOnScreenOnly)
let windowsListInfo = CGWindowListCopyWindowInfo(options, CGWindowID(0))
let windowsList = windowsListInfo as NSArray? as? [[String: AnyObject]]
let visibleWindows = windowsList?.filter{ $0["kCGWindowLayer"] as! Int == 0 }
for window in visibleWindows! {
let windowTitle = window["kCGWindowOwnerName"] as! String
let windowNumber = window["kCGWindowNumber"] as! Int32
if windowNumber == 124 { // Safari
let nsWindow = NSApp.window(withWindowNumber: Int(windowNumber))
nsWindow?.cascadeTopLeft(from: NSPoint(x: 100.0, y: 100.0))
nsWindow?.setFrameTopLeftPoint(NSPoint(x: 100.0, y: 100.0))
}
}
}
but when I'm trying to get NSWindow by using windowNumber:
NSApp.window(withWindowNumber: Int(windowNumber))
I get nil.
So my question is, how can I move any window programmatically by knowing its windowNumber?
I've even tried to bring that window to front and
let pid = window["kCGWindowOwnerPID"] as? Int32 {
let app = NSRunningApplication(processIdentifier: pid)
app?.activate(options: .activateIgnoringOtherApps)
doing this but nothing helped me.
Could someone help me to find out the way how I can move the Safari window(for example) programmatically?

This should get the trick done:
let owner = entry[kCGWindowOwnerName as String] as! String
var bounds = entry[kCGWindowBounds as String] as? [String: Int]
let pid = entry[kCGWindowOwnerPID as String] as? Int32
if owner == "Safari"
{
let appRef = AXUIElementCreateApplication(pid!);
var value: AnyObject?
let result = AXUIElementCopyAttributeValue(appRef, kAXWindowsAttribute as CFString, &value)
if let windowList = value as? [AXUIElement] {
print ("windowList #\(windowList)")
if let window = windowList.first
{
var position : CFTypeRef
var size : CFTypeRef
var newPoint = CGPoint(x: 0, y: 0)
var newSize = CGSize(width: 300, height: 800)
position = AXValueCreate(AXValueType(rawValue: kAXValueCGPointType)!,&newPoint)!;
AXUIElementSetAttributeValue(windowList.first!, kAXPositionAttribute as CFString, position);
size = AXValueCreate(AXValueType(rawValue: kAXValueCGSizeType)!,&newSize)!;
AXUIElementSetAttributeValue(windowList.first!, kAXSizeAttribute as CFString, size);
}
}
}
By the way, I found this code in another discussion which I can't find at the moment, sorry about that
Oh and you need this code to request the accessibility access:
let options: NSDictionary = [kAXTrustedCheckOptionPrompt.takeUnretainedValue() as String : true]
let accessEnabled = AXIsProcessTrustedWithOptions(options)
if !accessEnabled {
print("Access Not Enabled")
}
Hope this helps, even if it's been a year and five days since you posted this question :)

Related

Get Image resolution from an URL in the background thread

I'm trying to get the resolution of an image from URL without actually downloading it. So I have a function for that:
public func resolutionForImage(url: String) -> CGSize? {
guard let url = URL(string: url) else { return nil }
guard let source = CGImageSourceCreateWithURL(url as CFURL, nil) else {
return nil
}
let propertiesOptions = [kCGImageSourceShouldCache: false] as CFDictionary
guard let properties = CGImageSourceCopyPropertiesAtIndex(source, 0, propertiesOptions) as? [CFString: Any] else {
return nil
}
if let width = properties[kCGImagePropertyPixelWidth] as? CGFloat,
let height = properties[kCGImagePropertyPixelHeight] as? CGFloat {
return CGSize(width: width, height: height)
} else {
return nil
}
}
It works fine, but I need to run it in the Background thread, in the main thread in block the UI
And this function is called in an another function in a collectionView cell, so in the end calculateImageHeight output should be in the main thread, could anyone help me manage it, still not in good in the threading
public func calculateImageHeight(ratio: Double = 0.0) -> CGFloat {
var calculatedRatio = ratio
if ratio == 0.0 {
if let size = resolutionForImage(url: imageUrl) {
calculatedRatio = size.height/size.width
}
}
let height = imageViewWidth * calculatedRatio
return height.isFinite ? height : 100
}

How to get a tap position in ARKit?

I am trying to tap the screen and get tap position then I can fire the bullet from the position I tap.
I searched and tried many ways such as below but it doesn't work
guard let pointOfView = sceneView.pointOfView else { return }
let transform = pointOfView.transform
let orientation = SCNVector3(-transform.m31, -transform.m32, -transform.m33)
let scnView = sender.view as! ARSCNView
let holdLocation = sender.location(in: scnView)
let estimatedPlane: ARRaycastQuery.Target = .existingPlaneInfinite
let alignment: ARRaycastQuery.TargetAlignment = .any
guard let query = scnView.raycastQuery(from: holdLocation,
allowing: estimatedPlane,
alignment: alignment)
else { return }
let results = scnView.session.raycast(query)
guard let pointResult = results.first else { return }
let pointTransform = SCNMatrix4(pointResult.worldTransform)
let pointLocation = SCNVector3(pointTransform.m41,
pointTransform.m42,
pointTransform.m43)
let pointPosition = orientation + pointLocation
Can anyone help to tell me how to do it? Highly appreciate it.

How to calculate ETA time with Google Maps API - Swift

I am drawing a route with Google map. I calculate the km distance on the route I drew. I also want to calculate how long it will go. How can I calculate how many minutes the route will take? I calculated the speed in the code below and when I tried to calculate the time using the speed I could not get any output. How can I calculate the duration?
func drowRoute(from source: CLLocationCoordinate2D, to destination: CLLocationCoordinate2D) {
self.mapView.clear()
let origin = "\(source.latitude),\(source.longitude)"
let destinationn = "\(destination.latitude),\(destination.longitude)"
guard let url = URL(string: "https://maps.googleapis.com/maps/api/directions/json?origin=\(origin)&destination=\(destinationn)&mode=driving&key=..") else {
let error = NSError(domain: "LocalDomain", code: 0, userInfo: [NSLocalizedDescriptionKey: "Failed to create object URL"])
print("Error: \(error)")
return
}
let config = URLSessionConfiguration.default
let session = URLSession(configuration: config)
let task = session.dataTask(with: url, completionHandler: {
(data, response, error) in
if error != nil {
print(error!.localizedDescription)
}
else {
do {
if let json : [String:Any] = try JSONSerialization.jsonObject(with: data!, options: .allowFragments) as? [String: Any]{
guard let routes = json["routes"] as? NSArray else {
DispatchQueue.main.async {
}
return
}
if (routes.count > 0) {
let overview_polyline = routes[0] as? NSDictionary
let dictPolyline = overview_polyline?["overview_polyline"] as? NSDictionary
let points = dictPolyline?.object(forKey: "points") as? String
DispatchQueue.main.async {
//
let legs = overview_polyline?["legs"] as! Array<Dictionary<String, AnyObject>>
let distance = legs[0]["distance"] as? NSDictionary
let distanceValue = distance?["value"] as? Int ?? 0
let distanceDouleValue = distance?["value"] as? Double ?? 0.0
let duration = legs[0]["duration"] as? NSDictionary
let totalDurationInSeconds = duration?["value"] as? Int ?? 0
let durationDouleValue = duration?["value"] as? Double ?? 0.0
if(distanceValue != 0) {
self.speed = distanceDouleValue / durationDouleValue
print("speed", self.speed)
}
let miles = Double(distanceValue) / 1609.344
print("\(miles)")
let km = Double(distanceValue) * 0.001609
self.kmLabel.text = ("\(Int(km))" + " " + "KM")
if distanceValue > Int(32186.9){
}else{
self.showPath(polyStr: points!)
let startLocationDictionary = legs[0]["start_location"] as! Dictionary<String, AnyObject>
let originCoordinate = CLLocationCoordinate2DMake(startLocationDictionary["lat"] as! Double, startLocationDictionary["lng"] as! Double)
let endLocationDictionary = legs[legs.count - 1]["end_location"] as! Dictionary<String, AnyObject>
let destinationCoordinate = CLLocationCoordinate2DMake(endLocationDictionary["lat"] as! Double, endLocationDictionary["lng"] as! Double)
let marker1 = GMSMarker()
marker1.position = CLLocationCoordinate2D(latitude:destinationCoordinate.latitude, longitude: destinationCoordinate.longitude)
marker1.icon = UIImage(named: "placeholder")
marker1.map = self.mapView
let marker2 = GMSMarker()
marker2.position = CLLocationCoordinate2D(latitude:originCoordinate.latitude, longitude: originCoordinate.longitude)
marker2.icon = UIImage(named: "location")
marker2.map = self.mapView
}
}
}
else {
print(json)
DispatchQueue.main.async {
// SVProgressHUD.dismiss()
}
}
}
}
catch {
print("error in JSONSerialization")
DispatchQueue.main.async {
// SVProgressHUD.dismiss()
}
}
}
})
task.resume()
}
func showPath(polyStr :String){
// SVProgressHUD.dismiss()
let path = GMSPath(fromEncodedPath: polyStr)
let polyline = GMSPolyline(path: path)
polyline.strokeWidth = 5.0
polyline.strokeColor = UIColor.red
polyline.map = mapView
DispatchQueue.main.async {
let bounds = GMSCoordinateBounds(path: path!)
let update = GMSCameraUpdate.fit(bounds, with: UIEdgeInsets(top: 170, left: 30, bottom: 30, right: 30))
self.mapView.moveCamera(update)
}
}

set the size and position of all windows on the screen in swift

Is it possible in swift to get a list of all apps with a window in the foreground and then set the size and position of these windows.
I get the list of windows properties like this
let type = CGWindowListOption.optionOnScreenOnly
let windowList = CGWindowListCopyWindowInfo(type, kCGNullWindowID) as NSArray? as? [[String: AnyObject]]
for entry in windowList!
{
var owner = entry[kCGWindowOwnerName as String] as! String
var bounds = entry[kCGWindowBounds as String] as? [String: Int]
var pid = entry[kCGWindowOwnerPID as String] as? Int32
print ("\(owner) \(bounds) \(pid) ")
if owner == "Erinnerungen"
{ bounds!["X"] = 0
bounds!["Y"] = 0
print("reset bounds")
let appRef = AXUIElementCreateApplication(pid!); //TopLevel Accessability Object of PID
print(appRef)
var value: AnyObject?
let result = AXUIElementCopyAttributeValue(appRef, kAXWindowsAttribute as CFString, &value)
if result == .success, let windowList = value as? [AXUIElement]
{ // DO ANYTHING
} else
{ print("Result no Success or no valid windowlist returnd")
}
}
}
Now I try to change some of the propierties, but that has no effect.
Also trying to get the AttributeValue for the TopLevel Accessability Object of PID returns AXError (kAXErrorCannotComplete = -25204)
Got it thanks to the help of #Martin R
let type = CGWindowListOption.optionOnScreenOnly
let windowList = CGWindowListCopyWindowInfo(type, kCGNullWindowID) as NSArray? as? [[String: AnyObject]]
for entry in windowList!
{
let owner = entry[kCGWindowOwnerName as String] as! String
var bounds = entry[kCGWindowBounds as String] as? [String: Int]
let pid = entry[kCGWindowOwnerPID as String] as? Int32
if owner == "Terminal"
{
let appRef = AXUIElementCreateApplication(pid!); //TopLevel Accessability Object of PID
var value: AnyObject?
let result = AXUIElementCopyAttributeValue(appRef, kAXWindowsAttribute as CFString, &value)
if let windowList = value as? [AXUIElement]
{ print ("windowList #\(windowList)")
if let window = windowList.first
{
var position : CFTypeRef
var size : CFTypeRef
var newPoint = CGPoint(x: 0, y: 0)
var newSize = CGSize(width: 800, height: 800)
position = AXValueCreate(AXValueType(rawValue: kAXValueCGPointType)!,&newPoint)!;
AXUIElementSetAttributeValue(windowList.first!, kAXPositionAttribute as CFString, position);
size = AXValueCreate(AXValueType(rawValue: kAXValueCGSizeType)!,&newSize)!;
AXUIElementSetAttributeValue(windowList.first!, kAXSizeAttribute as CFString, size);
}
}
}
}

Subscript error when trying to get keyboard frame?

I am trying to get the keyboard frame from keyboard notfication. But I am getting the error "[NSObject:AnyObject] does not have any membership subscript"
I am using the following code.
if keyboardNotification != nil {
let userInfo = keyboardNotification!.userInfo
let animationDuration: NSTimeInterval = (userInfo[UIKeyboardAnimationDurationUserInfoKey] as NSNumber).doubleValue()
let keyboardScreenBeginFrame = (userInfo[UIKeyboardFrameBeginUserInfoKey] as NSValue).CGRectValue()
let keyboardScreenEndFrame = (userInfo[UIKeyboardFrameEndUserInfoKey] as NSValue).CGRectValue()
}
notification.userInfo is [NSObject : AnyObject]?.
So, try this
if let userInfo = keyboardNotification.userInfo {
let animationDuration = (userInfo[UIKeyboardAnimationDurationUserInfoKey] as! NSNumber).doubleValue()
let keyboardScreenBeginFrame = (userInfo[UIKeyboardFrameBeginUserInfoKey] as! NSValue).CGRectValue()
let keyboardScreenEndFrame = (userInfo[UIKeyboardFrameEndUserInfoKey] as! NSValue).CGRectValue()
}