KM Action :: Evaluate JavaScript for Automation vs Script Editor

This is the first time that I've noticed a piece of code which can be evaluated successfully by Script Editor, (with the language selector at top left set to JavaScript), but which harvests an error message from the Keyboard Maestro Evaluate JavaScript for Automation action.

ObjC.unwrap(
    $.NSBundle.bundleWithIdentifier("com.apple.Safari")
    .objectForInfoDictionaryKey("CFBundleDisplayName")
);

Has anyone else seen this, or detected, for example, framework imports made by Script Editor by default (for JavaScript), which are not available to the Keyboard Maestro action (or perhaps, not available to the underlying osascript command line ?)

JavaScript for Automation evaluation glitch ?.kmmacros (1.8 KB)

And in case anyone is curious, we hit the same issue with the equivalent AppleScript:

The following can be evaluated by Script Editor,
but not, it seems, by the Keyboard Maestro Evaluate AppleScript action

use framework "Foundation"

tell current application
    ((its (NSBundle's bundleWithIdentifier:"com.apple.Safari"))'s ¬
        objectForInfoDictionaryKey:"CFBundleDisplayName") as text
end tell

Have you tried it in Terminal? Looks like an osascript issue:

bash-3.2$ osascript -e "use framework \"Foundation\"
tell current application
((its (NSBundle's bundleWithIdentifier:\"com.apple.Safari\"))'s objectForInfoDictionaryKey:\"CFBundleDisplayName\") as text                                                             
end tell"
114:162: execution error: missing value doesn’t understand the “objectForInfoDictionaryKey_” message. (-1708)
bash-3.2$ 

Not that that really helps, but at least you can take KM out of the equation!

1 Like

That's right – it does seem to turn on some difference between the osascript evaluation context and that of Script Editor.

It turns out that the same thing is seen at the command line, and in applications like Code Runner and Visual Studio Code which, like Keyboard Maestro, pass source code to osascript.


But it's more of a curiosity than a practical problem – in this case for example, we can change the route to:

Bundle ID -> Application installation path -> Display Name

osascript diverges from Script Editor (and Script Debugger FWIW) only in its difficulty with the direct route:

Bundle ID -> Display Name


For the indirect route, Keyboard Maestro's Evaluate Actions (and osascript generally) turn out to have no problem with code like:

use framework "Foundation"
use framework "AppKit"

-- Application Display Name from Application Bundle
on run
    set fpApp to appPathFromID("com.apple.Safari")

    if missing value is not fpApp then
        displayNameFromAppPath(fpApp)
    else
        missing value
    end if
end run


-- appPathFromID :: bundle ID String -> Either (missing value) FilePath
on appPathFromID(bundleID)
    tell current application
        set maybeURL to its (NSWorkspace's sharedWorkspace's ¬
            URLForApplicationWithBundleIdentifier:(bundleID))
    end tell
    
    if missing value is not maybeURL then
        POSIX path of (maybeURL as text)
    else
        missing value
    end if
end appPathFromID


-- displayNameFromAppPath :: 
on displayNameFromAppPath(fpApp)
    tell current application
        set maybeBundle to its (NSBundle's bundleWithPath:fpApp)
    end tell
    
    if missing value is not maybeBundle then
        (maybeBundle's objectForInfoDictionaryKey:"CFBundleDisplayName") as text
    else
        missing value
    end if
end displayNameFromAppPath
1 Like