title: Moving windows programmatically on MacOS in Swift
body: <p>I'm trying to move a window programmatically on my desktop. For this I've tried:</p>
  
  <pre><code>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))
          }
      }
  }
  </code></pre>
  
  <p>but when I'm trying to get <code>NSWindow</code> by using <code>windowNumber</code>:</p>
  
  <pre><code>NSApp.window(withWindowNumber: Int(windowNumber))
  </code></pre>
  
  <p>I get <code>nil</code>.</p>
  
  <p>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</p>
  
  <pre><code>let pid = window["kCGWindowOwnerPID"] as? Int32 {
  let app = NSRunningApplication(processIdentifier: pid)
  app?.activate(options: .activateIgnoringOtherApps)
  </code></pre>
  
  <p>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?</p>