windowid.swift 604 B

123456789101112131415161718192021
  1. #!/usr/bin/swift
  2. import Foundation
  3. import Cocoa
  4. import CoreGraphics.CGWindow
  5. let windows : NSArray = CGWindowListCopyWindowInfo(CGWindowListOption.excludeDesktopElements, kCGNullWindowID)! as NSArray
  6. let search_for_app = CommandLine.arguments[1]
  7. let search_for_win = CommandLine.arguments[2]
  8. for window in windows {
  9. let window = window as! NSDictionary
  10. let app_name = window[kCGWindowOwnerName] as! String
  11. let window_name = window[kCGWindowName] as? String
  12. if app_name == search_for_app && window_name == search_for_win {
  13. print("\(window[kCGWindowNumber]!)")
  14. }
  15. }