#!/usr/bin/osascript -l JavaScript

// adapted from:
// https://gist.github.com/EvanLovely/cb01eafb0d61515c835ecd56f6ac199a

// new to jxa?
// - https://apple-dev.groups.io/g/jxa/wiki/3202
// - interactive repl: `osascript -il JavaScript`
// - API reference: Script Editor -> File -> Open Dictionary

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

// as of 05/01/21 incognio & url are not actively used in AW
// variables must be set to `undefined` since this script is re-run via osascript
// and the previously set values will be cached otherwise
var url = undefined, incognito = undefined, title = undefined;

// it's not possible to get the URL from firefox
// https://stackoverflow.com/questions/17846948/does-firefox-offer-applescript-support-to-get-url-of-windows

switch(appName) {
  case "Safari":
    // incognito is not available via safari applescript
    url = Application(appName).documents[0].url();
    title = Application(appName).documents[0].name();
    break;
  case "Google Chrome":
  case "Google Chrome Canary":
  case "Chromium":
  case "Brave Browser":
    const activeWindow = Application(appName).windows[0];
    const activeTab = activeWindow.activeTab();

    url = activeTab.url();
    title = activeTab.name();
    incognito = activeWindow.mode() === 'incognito';
    break;
  case "Firefox":
  case "Firefox Developer Edition":
    title = Application(appName).windows[0].name();
    break;
  default:
    mainWindow = oProcess.
      windows().
      find(w => w.attributes.byName("AXMain").value() === true)

    // in some cases, the primary window of an application may not be found
    // this occurs rarely and seems to be triggered by switching to a different application
    if(mainWindow) {
      title = mainWindow.
        attributes.
        byName("AXTitle").
        value()
    }
}

// key names must match expected names in lib.py
JSON.stringify({
  app: appName,
  url,
  title,
  incognito
});
