Path of front document in named application

Returns the (posix) path of any front document in an application with a matching name (case sensitive).

Intended to work for applications like Highlights.app which have no osascript dictionary.

( Requires OS X Yosemite – the script (source code below) is written in Javascript for Applications)

Path of front document in named application.zip (7.3 KB)

NB the application can only be found by the plugin if its name is spelled correctly ( case sensitive )

(function (strAppName) {
    var appSE = Application("System Events"),
        appNamed = null,
        lstWins = null,
        strPath = '',
        lngPrefix, strURL;

    appNamed = appSE.applicationProcesses[strAppName];
    // IS THERE AN OPEN WINDOW IN AN APPLICATION OF THIS NAME ?
    try {
        lstWins = appNamed.windows();
    } catch (f) {
        return 'No open documents found in application "' + strAppName + '"';
    }

    if (lstWins) {
        // DOES THE WINDOW CONTAIN A SAVED DOCUMENT ?
        try {
            strURL = lstWins[0].attributes["AXDocument"].value();
        } catch (g) {
            return 'No open documents found in' + strAppName;
        }
    }

    // drop 'file://' and any hostname prefix, and decode
    return strURL ?
        (strPath = decodeURI(strURL).slice(7)).slice(
            strPath.indexOf("/")
        ) : ""

})("$KMPARAM_Name_of_application")
3 Likes

@ComplexPoint This has come in so handy! Thank you!

2 Likes