Search code examples
javascriptapplescriptjavascript-automation

JXA: display dialog with custom icon


With AppleScript, we can easily display a dialog with a custom icon:

display dialog "Test" with icon POSIX file "{{path_to_our_icon}}"

How can we do the same with JXA (JavaScript for Automation)? The official documentation doesn’t seem to cover this. It only tells us how to use one of the predefined icons.


Solution

  • Where the SDEF dictionary specifies an argument of type file you need a full path string wrapped in the Path() constructor.

    (For more on Path() see under 'Paths' in the JavaScript for Automation Release Notes)

    (function () {
        'use strict';
    
        var a = Application.currentApplication(),
            sa = (a.includeStandardAdditions = true, a);
    
        sa.displayDialog('Test', {
            defaultAnswer: 'Next question ?',
            buttons: ['OK', 'Cancel'],
            defaultButton: 'OK',
            cancelButton: 'Cancel',
            withTitle: 'Test dialog',
            withIcon: Path('/System/Library/Frameworks/Automator.framework/Versions/A/Resources/Automator.icns')
        });
    })();