Get Open Windows in Objective C - objective-c

How would I get the names of the open windows on Mac OSX in Objective C? Basically I just want to check if a window with a specific name (ie. "Chrome" to detect if Chrome is open) is open.
The app in question will NEVER run without a window open (unlike Chrome in my example), so detecting running processes may not be necessary.

For the case you describe, looking through the window list is not a good approach. For example, Chrome does not have a window named "Chrome" AFAIK — the names of its windows depend on the currently open tab. If you want to detect an application, use NSWorkSpace's launchedApplications method.
To actually detect windows, use the Quartz Window Services API.

Related

Mac development, objective-c: can I detect when user switch tasks like closing a window, activating to another window?

I am developing a desktop application that capture computer activities on mac osx using objective-c. I know it's possible to capture it when user presses on the keyboard and mouse position. But I don't know how to detect when user switches tasks on computer, like closing a window (of other applications), activating another window (of other applications)?
Does anyone have any experience in that?
Yes, via the Accessibility system. For example the NSAccessibilityMainWindowChangedNotification.

Get list of opened windows and minimised windows

I'm using CGWindowListCopyWindowInfo to obtain list of windows opened in OSX system. I would like to get all opened windows + minimised window (windows which appears in the bottom of the dock). I've already tried all possible options of this function (https://developer.apple.com/reference/coregraphics/1666230-quartz_window_services/1805250-window_list_option_constants?language=objc)
Using it with option kCGWindowListOptionOnScreenOnly returns obviously proper list but without minimised windows. Without it, I get minimised but also all different windows which are not minimised (e.g. splash screens of other applications).
Second attempt was to use properties of window returned by CGWindowListCreateDescriptionFromArray(https://developer.apple.com/reference/coregraphics/1455215-cgwindowlistcreatedescriptionfro?language=objc) but I didn't find the way also.
Thanks.
Update:
Also using accessibility API isn't a solution here since the user has to explicitly allow particular application to control the computer in system preferences.

Porting CLI/GUI Windows program to OS X

I have a Windows program that has a GUI which also uses a command line interface (a cmd Window) as a debugging console. Basically, when it is double clicked, it launches a command line window and then the program creates all the GUI windows. Then you'd have two Windows: the main GUI and a debugging console.
I'm trying to port this pogram to OS X. Because OS X (and all Unix OSs for that matter) doesn't automatically launch a command line window when you run a command line application. So, I obviously need another way to port this application.
My initial thought was simply to import the source code into a XCode project, redirect standard input and output and then port the GUI. The GUI and console would run side by side just like in Windows. I don't think this is the most optimal solution since that would mean I'd essentially have to write a terminal emulator.
My other thought would be to port the application as a command line application which creates its GUI just like in Windows. The application would then have to be run from Terminal.app which could handle all the I/O. Unfortunately, I don't think you can use the Cocoa framework without using a NSApplication loop.
Any ideas how I could approach this?
You can of course create a run loop from a terminal-launched app. But that generally isn't what you want to do.
It sounds like on Windows, the CLI is just being used as a shortcut to creating a real debugging console window. So the simplest answer is to create a debugging console window. It's pretty easy to create a window which contains just a multi-line text or list view. (If you want something fancier, consider borrowing code from iTerm2 or other open source projects instead of trying to build a complete terminal.) If your debug information is being printed with some fancy macros, just change the macros to log to your list view.
If you're directly doing something like fprintf or syslog to do your logging, it might be simpler to create a wrapper app that launches the main app, and the wrapper creates the debugging console window and displays the main app's stdout and/or stderr. (This might be as simple as using popen.)

possible to modifer the properties of windows in Objective C?

These are windows besides those that belong to the application. For example, how could I change the title of an open application, for example, TextEdit
You could use mach_inject and mach_override to load code into the target application and then simply use the Objective-C API's to access the window.
You could also try using applescript, but no idea if or how that is done.

Finding out if a Window is Resizable

Is there a way to determine whether the active window (not of my process, but a different application) is resizable?
Edit: I would prefer to do this without applescript as it then depends on the application being scriptable.
Use Accessibility. Once you find the window you want to examine, test whether it has the kAXGrowAreaAttribute attribute (the value for which would be the grow area itself, a.k.a. the “size box” or [the Windows term] “resize handle”). A window that has one is resizable; a window that doesn't have one is not resizable.
The user will need to have access for assistive devices turned on, but making that happen is easier than scripting unscriptable applications.
Edit from the year 2011: Lion killed off size boxes, so now you'll need to test whether the window's size attribute can be changed.
Yes, you can check if the "frontmost window" of the target application is resizable! You can perform the scripting request via applescript, scripting bridge or a third party framework!
However, in any case, it's needed that the target application is scriptable and you can access to the "resizable" property (of the "frontmost window" object) via a scripting request!
Depends on how you're getting access to the window. There's a property in AppleScript on Window objects, resizable, that indicates this.