Ideas for apps that involves NSWindows

I’ve read the first 14 chapters and have a few ideas for apps, but I think they might be impossible or least require some private APIs that I haven’t find. The basic functionality I need for the apps is the ability to find the windows associated to an application (or all currently running applications). I can find the list of all windows from NSApplication.windows. I can find a list of all running applications (which are of type NSRunningApplication) but there’s no way to convert an NSRunningApplication into an NSApplication. Is there another way (terminal command or some private API) to access the window object or the application object?

Hi,

Getting information about windows in other apps is tricky. You might have to do it with AppleScript.

Finding about your own app and its windows is not so difficult. Try this:

func listWindows() {
  let app = NSApplication.shared   // or use the `NSApp` shorthand
  print(app)

  let windows = app.orderedWindows
  for win in windows {
    let winType = win == app.keyWindow ? "- key" : ""
    print(win.title, winType)
  }
}

If you could give me some more info about what you want to achieve maybe I can offer some more suggestions.

Sarah

1 Like

For the two apps I’m thinking about, I would want the other apps windows:

  • Window Switcher: Similar to App Switcher but will list other windows (I use a lot of windows)
  • App focus highlight: I use split screen a lot and it’s difficult to know which app is currently in focus. So I would want to know which app is the current (NSWorkspace.current), but finding the current window is difficult.

For AppleScript, is there a way to integrate it into an app? I have no experience with AppleScript, but I think if there is, it would be similar to Chapter 12, 13, 14.

Yes, there are two ways to integrate AppleScript.

One is using NSAppleScript, but that seems slow and a bit buggy.
The other ways is to use the osascript Terminal command.
You can so something like this to get a list of all running apps:

osascript -e 'tell application "System Events" to get name of (processes where background only is false)'
1 Like