Save As dialog box with multiple file choice

Luigi Castelli's icon

Hello there,

I am using the function saveasdialog_extended() to give the user the choice to save two different types of image files. (PNG or JPEG)
Here is the relevant code snippet:

void myobj_do_image_save(t_myobj *x, t_symbol *msg, long argc, t_atom *argv)
{
    t_symbol *sym;
    char name[MAX_FILENAME_CHARS];
    t_filepath path = 0;
    t_fourcc type = 0;
    t_fourcc typelist[2] = { FOUR_CHAR_CODE('PNG '), FOUR_CHAR_CODE('JPEG') };
    short typecount = 2;
    t_max_err err;
  
    if (argc && argv) {
        if (atom_gettype(argv) == A_SYM) {
            sym = atom_getsym(argv);
            strncpy_zero(name, sym->s_name, MAX_FILENAME_CHARS);
        } else {
            strncpy_zero(name, “untitled.png”, MAX_FILENAME_CHARS);
        }
    } else {
        strncpy_zero(name, “untitled.png”, MAX_FILENAME_CHARS);
    }
    err = saveasdialog_extended(name, &path, &type, typelist, typecount);
    if (err == MAX_ERR_NONE) {
        post("name: %s, path: %d, type: %d”, name, path, type);
    } else {
        post(“user cancelled…”);
    }
}

I am using the latest Max SDK 7.3.3 and I am noticing some unexpected behavior in the function saveasdialog_extended() as prototyped in ext_proto.h.
In my code the ‘type’ argument doesn’t seem to return the type of file chosen by the user.
I get the file name and the path ID correctly, but - no matter what I try - the function always returns a zero for the type argument.
Can someone double check that?
How is it possible to distinguish what file type has the user selected in the dialog box?

Any help is highly appreciated.
Thank you.

- Luigi

Timothy Place's icon

Hi Luigi,

Below is the code used for writing Dictionaries in the dict object as either YAML or JSON.

char             filename[MAX_PATH_CHARS];
    short             path = 0;
    t_fourcc         types[3] = {'DICT', 'JSON', 'YAML'};
    t_fourcc        outtype = 0;
    t_max_err        err;

    filename[0] = 0;

    if (!userpath || !userpath->s_name[0]) {
        saveas_promptset("Export as file...");

        err = saveasdialog_extended(filename, &path, &outtype, types, 3);
        if (err)        // User Cancelled
            return err;
    }
    else {
        strncpy_zero(filename, userpath->s_name, MAX_PATH_CHARS);
    }

    dictobj_enforce_filesuffix(filename, MAX_PATH_CHARS, &outtype);

    if (outtype == 'YAML')
        err = dictionary_write_yaml(x->m_dictionary, filename, path);
    else
        err = dictionary_write(x->m_dictionary, filename, path);

Luigi Castelli's icon

Hi Tim,

thanks for your help. I am finally getting somewhere...
Could you please post the code for the dictobj_enforce_filesuffix() function.
It doesn't seem to be declared/defined anywhere in the SDK.

Thanks!

- Luigi

Timothy Place's icon

It's not very exciting :-) but here it is:

void dictobj_enforce_filesuffix(char *filename, long maxlen, t_fourcc *type)
{
    char *c;
    char has_json_extension = false;
    char has_yaml_extension = false;

    c = strrchr(filename, '.');
    if (c) {
        if (!strcmp(c, ".json"))
            has_json_extension = true;
        else if (!strcmp(c, ".yaml"))
            has_yaml_extension = true;
        else if (!strcmp(c, ".yml"))
            has_yaml_extension = true;
    }
    if (*type == 'JSON' && !has_json_extension)
        strncat_zero(filename, ".json", maxlen);
    else if (*type == 'YAML' && !has_yaml_extension)
        strncat_zero(filename, ".yaml", maxlen);
    else if (*type == 0) {
        if (!has_yaml_extension && !has_json_extension) {
            *type = 'JSON';
            strncat_zero(filename, ".json", maxlen);
        }
        else if (has_yaml_extension)
            *type = 'YAML';
        else if (has_json_extension)
            *type = 'JSON';
    }
}
martin mueller's icon

Hello Luigi! Did you implemented the code and does it work as expected? I also tried it but had no luck :-( Maybe you can post your code snippet? Thank you so much for your help!