27

Is there a way to find the application name of the current active window at a given time on Mac OS X using Python?

1

4 Answers 4

30

This should work:

#!/usr/bin/python

from AppKit import NSWorkspace
activeAppName = NSWorkspace.sharedWorkspace().activeApplication()['NSApplicationName']
print activeAppName

Only works on Leopard, or on Tiger if you have PyObjC installed and happen to point at the right python binary in line one (not the case if you've installed universal MacPython, which you'd probably want to do on Tiger). But Peter's answer with the Carbon way of doing this will probably be quite a bit faster, since importing anything from AppKit in Python takes a while, or more accurately, importing something from AppKit for the first time in a Python process takes a while.

If you need this inside a PyObjC app, what I describe will work great and fast, since you only experience the lag of importing AppKit once. If you need this to work as a command-line tool, you'll notice the performance hit. If that's relevant to you, you're probably better off building a 10 line Foundation command line tool in Xcode using Peter's code as a starting point.

21

The method in the accepted answer was deprecated in OS X 10.7+. The current recommended version would be the following:

from AppKit import NSWorkspace
active_app_name = NSWorkspace.sharedWorkspace().frontmostApplication().localizedName()
print(active_app_name)
8
  • 1
    This really helps! How can we get the active window title?
    – Jake W
    Jan 16, 2015 at 7:10
  • I know it's possible to do it using AppleScript (calling it through osascript in Python), but up to my knowledge, there's no straightforward way to do it in Python. Here's a link to check the information you can retrieve using Cocoa: developer.apple.com/library/mac/documentation/Cocoa/Reference/…
    – user2317421
    Jan 17, 2015 at 13:49
  • Originally we used AppleScript to get the current window title, which worked perfectly. But it didn't work if we signed the app. And then I found a native API in Quartz to do it, and that resolved our problem.
    – Jake W
    Jan 18, 2015 at 1:48
  • 1
    For some reason this always gave me the name of my terminal. The accepted answer got it right. I'm running OS X 10.10.
    – erb
    Mar 5, 2015 at 9:44
  • 1
    @Yehosef As suggested by reviewers, I added the answer to this question: stackoverflow.com/questions/28815863/…, which is more relevant. Basically the way to get app name is the same, the native part I found is for getting window title using Quartz.
    – Jake W
    May 21, 2016 at 22:58
7

First off, do you want the window or the application name? This isn't Windows—an application process on Mac OS X can have multiple windows. (Furthermore, this has also been true of Windows for a few years now, although I have no idea what the API looks like for that.)

Second, Carbon or Cocoa?

To get the active window in Cocoa:

window = NSApp.mainWindow()

To get the name of your process in Cocoa:

appName = NSProcessInfo.processInfo().processName()

Edit: Oh, I think I know what you want. The name of the frontmost process, right?

I don't think there's a way to do it in Cocoa, but here's how to do it in Carbon in C:

ProcessSerialNumber psn = { 0L, 0L };
OSStatus err = GetFrontProcess(&psn);
/*error check*/

CFStringRef processName = NULL;
err = CopyProcessName(&psn, &processName);
/*error check*/

Remember to CFRelease(processName) when you're done with it.

I'm not sure what that will look like in Python, or if it's even possible. Python doesn't have pointers, which makes that tricky.

I know PyObjC would translate the latter argument to CopyProcessName into err, processName = CopyProcessName(…), but the Carbon bindings don't rely on PyObjC (they're part of core Python 2), and I'm not sure what you do about the PSN either way.

3
  • What do we need to import to get NSApp and NSProcessInto? Jan 26, 2014 at 21:59
  • @GreenAsJade: Going by Dirk's answer, presumably Foundation and AppKit. Jan 26, 2014 at 22:34
  • Since the original question was about Python, note that CFRelease is not necessary with PyObjC. PyObjC handles calling that for you, AFAIK.
    – Dale
    Jun 30, 2019 at 21:07
2

I needed the current frontmost application in a Python script that arranges the windows nicely on my screen (see move_window).

Of course, the complete credit goes to Peter! But here is the complete program:

#include <Carbon/Carbon.h>

int main(int, char) {
    ProcessSerialNumber psn = { 0L, 0L };
    OSStatus err = GetFrontProcess(&psn);

    CFStringRef processName = NULL;
    err = CopyProcessName(&psn, &processName);
    printf("%s\n", CFStringGetCStringPtr(processName, NULL));
    CFRelease(processName);
}

Build with gcc -framework Carbon filename.c

3
  • 2
    How is this an answer for python? Jan 26, 2014 at 21:56
  • Also not for Python, but for the new Yosemite JavaScript automation you can use $.NSWorkspace.sharedWorkspace.activeApplication.objectForKey('NSApplicationName').UTF8String;. You need to import this first though: ObjC.import('AppKit').
    – SirVer
    Feb 15, 2015 at 8:49
  • 1
    As of now, 'CopyProcessName' has been explicitly marked deprecated.
    – Isilmë O.
    Apr 19, 2018 at 17:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.