//usr/bin/env SRC=$0 DST=$TMPDIR/${app=`basename ${0%.mm}`}.app/Contents/MacOS/$app ARGS="$*" /bin/bash -c ' \
    mkdir -p $(dirname $DST) && \
    plutil -create xml1 ${plist=`dirname $DST`/../Info.plist} && \
        plutil -insert CFBundleIdentifier -string com.yourcompany.$app $plist && \
    clang++ $SRC -o $DST -fmodules -fcxx-modules && \
    echo $DST && exec $DST $ARGS '; exit


@import AppKit;
@import UniformTypeIdentifiers;

int main()
{
    @autoreleasepool {
        [NSApplication sharedApplication];
        [NSApp setActivationPolicy:NSApplicationActivationPolicyRegular];

        id appMenu = [[NSMenu new] autorelease];
        [appMenu addItem:[[[NSMenuItem alloc] initWithTitle:@"Quit"
            action:@selector(terminate:) keyEquivalent:@"q"] autorelease]];
        id appMenuItem = [[NSMenuItem new] autorelease];
        [appMenuItem setSubmenu:appMenu];
        NSApp.mainMenu = [[NSMenu new] autorelease];
        [NSApp.mainMenu addItem:appMenuItem];


        [NSApp activateIgnoringOtherApps:YES];

        NSSavePanel *savePanel = [NSSavePanel savePanel];
        // File type is multi-part extension
#if 1
        savePanel.allowedFileTypes = @[@"tar.gz"];
#else
        // Crashes because typeWithFilenameExtension:@"tar.gz" returns nil
        // Documentation says: The corresponding type if the system recognizes
        // the filename extension; otherwise, the initializer returns a dynamic
        // type whose declared and publicType properties are both set to NO.
        savePanel.allowedContentTypes = @[[UTType typeWithFilenameExtension:@"tar.gz"]];
#endif

        // Without setting this, the default file name ends up
        // as 'Untitled.tar.gz.tar.gz'.
        // We must also set this without any extension, otherwise
        // we end up with the same problem.
        savePanel.nameFieldStringValue = @"foo";

        // Without setting this the issues below become even worse,
        // as the user doesn't see the final file name
        savePanel.extensionHidden = NO;

#if 1
        savePanel.allowsOtherFileTypes = YES;

        // Clicking "Save" results in:
        // "You have used the extension “.gz” at the end of the name. The standard extension is “.tar.gz”.""
        // Clicking "use gz" results in foo.tar.gz
        // Clicking "use tar.gz" results in foo.tar.tar.gz
#else
        // Clicking "Save" results in:
        // "You cannot save this document with extension “.gz” at the end of the name. The required extension is “.tar.gz”."
        // Clicking "use tar.gz" results in foo.tar.tar.gz
        // Clicking "use both" results in foo.tar.gz.tar.gz
#endif
        [savePanel runModal];

        NSLog(@"\nResulting URL: %@", savePanel.URL);

        return 0;
    }
}
