16

I am using this code to get the window title:

tell application "System Events"
    set frontApp to name of first application process whose frontmost is true
end tell

tell application frontApp
    set window_name to name of front window
end tell

However, this fails in some cases. Obviously it fails when there is no open window but that is Ok. However, in some cases, for example for Texmaker, it fails with an error. It also doesn't work for Preview.

What would be a way to get the window title anyway, even for cases like Texmaker?

Albert
  • 65,406
  • 61
  • 242
  • 386

4 Answers4

23

This seems to work always:

global frontApp, frontAppName, windowTitle

set windowTitle to ""
tell application "System Events"
    set frontApp to first application process whose frontmost is true
    set frontAppName to name of frontApp
    tell process frontAppName
        tell (1st window whose value of attribute "AXMain" is true)
            set windowTitle to value of attribute "AXTitle"
        end tell
    end tell
end tell

return {frontAppName, windowTitle}

Got the idea from Getting the currently open file and path from Preview.

Laurel
  • 5,965
  • 14
  • 31
  • 57
Albert
  • 65,406
  • 61
  • 242
  • 386
  • 1
    I ran into some trouble while trying to adapt this code for a related task, and thought I'd point that out here. If you're trying to test this on a particular program, you may need to `tell application processName to activate` first, so that `processName` is in fact frontmost, and the AXMain doesn't error out. – Clayton Hughes Sep 09 '13 at 00:10
  • Hi Albert, after the line `set windowTitle to ....` I added thsi line: `set level to 3` in order to make it "always on top", however it did not do anything. Can you please help me with that - I am was trying to apply this topic - http://www.macscripter.net/viewtopic.php?id=30100 - to here. – Noitidart Sep 03 '16 at 18:31
  • @Noitidart: You should ask that as a separate question. – Albert Sep 03 '16 at 21:58
  • Thanks @Albert I asked it here may you please see - http://stackoverflow.com/q/36960521/1828637 – Noitidart Sep 03 '16 at 23:18
  • This doesn't work when we minimise a particular application. For eg : If i am using chrome browser and if i minimise it then it will keep on reporting frontAppName as chrome. Or putting it in other words, when an application gets minimised the focus remains on that application only. – bhavya_w Sep 12 '16 at 07:24
  • This also doesn't work for apps like iTerm 2 . Recommend defaulting to sakra's answer. I have added a suggestion – Dylanthepiguy May 28 '17 at 22:00
  • Do you have an idea how to make this work with Chrome Apps? I've installed Google Meet, and when I run this script I get "app_mode_loader, missing value". – vault Nov 09 '21 at 11:16
7

Building off of Albert's answer, I'd instead do

global frontApp, frontAppName, windowTitle

set windowTitle to ""
tell application "System Events"
    set frontApp to first application process whose frontmost is true
    set frontAppName to name of frontApp
    set windowTitle to "no window"
    tell process frontAppName
        if exists (1st window whose value of attribute "AXMain" is true) then
            tell (1st window whose value of attribute "AXMain" is true)
                set windowTitle to value of attribute "AXTitle"
            end tell
        end if
    end tell
end tell

return {frontAppName, windowTitle}

This is a hack and I have no experience, but the advantage is that it doesn't crash if there's no window.

user2330514
  • 71
  • 1
  • 2
4

Give the following script a try:

tell application "System Events"
    set window_name to name of first window of (first application process whose frontmost is true)
end tell

I haven't verified that it works for TextMaker, though.

sakra
  • 62,199
  • 16
  • 168
  • 151
  • It only fails for Preview if you open an image. It works for Preview if you open a PDF. – Albert Mar 14 '11 at 00:55
  • This doesn't work when we minimise a particular application. For eg : If i am using chrome browser and if i minimise it then it will keep on reporting frontAppName as chrome. Or putting it in other words, when an application gets minimised the focus remains on that application only. – bhavya_w Sep 12 '16 at 07:25
4

Here's an alternative version in jxa based off of this script. Includes the URL of the window if it's a common browser.

var seApp     = Application("System Events");
var oProcess  = seApp.processes.whose({frontmost: true})[0];
var appName   = oProcess.displayedName();

var url;
var title;

switch(appName) {
  case "Safari":
    url = Application(appName).documents[0].url();
    title = Application(appName).documents[0].name();
    break;
  case "Google Chrome", "Google Chrome Canary", "Chromium":
    url = Application(appName).windows[0].activeTab().url();
    title = Application(appName).windows[0].activeTab().name();
    break;
  default:
    title = oProcess.
      windows().
      find(w => w.attributes.byName("AXMain").value() === true).
      attributes.
      byName("AXTitle").
      value()
}

JSON.stringify({
  appname: appName,
  url: url,
  title: title
});

You can run this on the command line via osascript -l JavaScript ./script.jxa

iloveitaly
  • 2,053
  • 22
  • 21
  • Works wonderful! and I understand more easily this syntax. also, I was able to make it work in node with https://github.com/mikaelbr/node-osascript !! thanks – Nathan Redblur Jun 27 '22 at 22:06