Home > front end >  How to open JPG image in application written in C on MacOS
How to open JPG image in application written in C on MacOS

Time:01-10

I want to write a simple image browser in C and then make a bundle for the MacOs.

The problem is that I am encountering a warning when I am trying to open a jpg file from a context menu in Finder.

enter image description here

I compiled my code, I prepared a iViewGtk.app bundle with Info.plist file.

The Info.plist is as follows:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
    <dict>
        <key>CFBundleExecutable</key>
        <string>iViewGtk</string>
        <key>CFBundleName</key>
        <string>iViewGtk</string>
        <key>CFBundleGetInfoString</key>
        <string>5.0</string>
        <key>CFBundleTypeName</key>
        <string>3r23rfewfwefwef343f</string>
        <key>CFBundleTypeOSTypes</key>
        <array>
            <string>****</string>
        </array>
        <key>CFBundleTypeMIMETypes</key>
        <string>image/jpeg</string>
        <key>CFBundleIconFile</key>
        <string>icon.icns</string>
        <key>CFBundleIdentifier</key>
        <string>net.bean.gtk.iview</string>
        <key>CFBundlePackageType</key>
        <string>APPL</string>
        <key>LSMinimumSystemVersion</key>
        <string>10.14</string>
        <key>NSPrincipalClass</key>
        <string>NSApplication</string>
        <key>CFBundleShortVersionString</key>
        <string>5.0</string>
        <key>CFBundleDocumentTypes</key>
        <array>
            <dict>
                <key>CFBundleTypeExtensions</key>
                <array>
                    <string>bmp</string>
                    <string>cur</string>
                    <string>gif</string>
                    <string>icns</string>
                    <string>ico</string>
                    <string>jpeg</string>
                    <string>jpg</string>
                    <string>jpe</string>
                    <string>jfi</string>
                    <string>jfif</string>
                    <string>pbm</string>
                    <string>pgm</string>
                    <string>png</string>
                    <string>ppm</string>
                    <string>svg</string>
                    <string>svgz</string>
                    <string>tif</string>
                    <string>tiff</string>
                    <string>wbmp</string>
                    <string>webp</string>
                    <string>xbm</string>
                    <string>xpm</string>
                    <string>ani</string>
                    <string>apng</string>
                    <string>avif</string>
                    <string>avifs</string>
                    <string>bw</string>
                    <string>exr</string>
                    <string>hdr</string>
                    <string>heic</string>
                    <string>heif</string>
                    <string>jxl</string>
                    <string>kra</string>
                    <string>ora</string>
                    <string>pcx</string>
                    <string>pic</string>
                    <string>psd</string>
                    <string>ras</string>
                    <string>rgb</string>
                    <string>rgba</string>
                    <string>sgi</string>
                    <string>tga</string>
                    <string>xcf</string>
                </array>
                <key>CFBundleTypeRole</key>
                <string>Viewer</string>
            </dict>
        </array>
        <key>NSSupportsAutomaticGraphicsSwitching</key>
        <true />
        <key>NSHighResolutionCapable</key>
        <true />
    </dict>
</plist>

Actually the Info.plist file is a copy of Info.plist file from a working application qView which can be installed using brew install qview.

In addition to that I am able to click on the bundle and open the application by clicking it.

I copied the app into Application folder and it seems that Finder recognizes it as application which can open image files. It is not grayed out:

enter image description here

Since I don't know how the parameter from Finder is provided to my app, I didn't implement the part which opens and displays the image. I think, that it shouldn't be a problem for MacOS, should be? The main.c looks as follows:

int main(int argc, char **argv)
{
    struct ApplicationContext *appContext = malloc(sizeof(appContext));
    GtkApplication *app;
    int status;

    app = gtk_application_new("net.bean.app", G_APPLICATION_HANDLES_OPEN);
    g_signal_connect(app, "activate", G_CALLBACK(activate), appContext);
    g_signal_connect(app, "open", G_CALLBACK(app_open), appContext);
    status = g_application_run(G_APPLICATION(app), argc, argv);
    g_object_unref(app);

    return status;
}

Could someone tell me why MacOS displays the warning?

Is this somehow related to some MacOs security policies which are forbidding to open a file to an unknown app?

Thank you.

CodePudding user response:

It's not sufficient to have a .plist file, there is also handling code necessary.

By the way, this is also the case for a regular Cocoa Objective-C applications. If none of the corresponding optional NSApplicationDelegate methods (such as application:openURLs: and friends) is implemented there, the dialog shown in the question appears.

Neither from the sample code provided nor from the tags used can one conclude whether the question refers to GTK3 or GTK4 - therefore this answer covers both cases.

GTK4

GTK4 already supports macOS as a backend. Looking for a corresponding NSApplicationDelegate delegate method in gtk open file test

So again no error dialog is shown and the file path is successfully passed to the GTK application.

  • Related