jpatcher_load_fromdictionary error: "patcher file your.maxpat missing patcher"

lx's icon

Hey,

I keep getting this error when trying to use jpatcher_load_fromdictionary.
The patch is of course in the search path. locatefile_extended() states that the file is found. The pathID is valid.

char filename[MAX_FILENAME_CHARS];
    strncpy_zero(filename,"your.maxpat", MAX_FILENAME_CHARS);

    t_uint32  type_searched_for = 'JSON';
    t_uint32  type_found;

    short path_id = 0;
    int res = locatefile_extended(filename,&path_id, &type_found ,&type_searched_for, 1);

    if(res == 0 && path_id != 0)    {post("filename found");}
    else if(res != 0)               {post("filename not found"); return;}
    /*––––––––––––––––––––––––––––––––––––––––––*/

    t_atom av[9];
    atom_setlong(&av[0], 99); //#1
    atom_setsym(&av[1], gensym("aaa")); //#2
    atom_setlong(&av[2], 0); //#3
    atom_setlong(&av[3], 0);
    atom_setlong(&av[4], 0);
    atom_setlong(&av[5], 0);
    atom_setlong(&av[6], 0);
    atom_setlong(&av[7], 0);
    atom_setlong(&av[8], 0);

    t_object* opened_patch = NULL;

    t_dictionary *rd;
    rd = dictionary_sprintf("@rect 0.0 0.0 0.0 0.0 @statusbarvisible 0 @toolbarvisible 0");
    /*the actual method call*/
    opened_patch = jpatcher_load_fromdictionary("your.maxpat", path_id, rd, 9, av);
    //error is posted -> "patcher file your.maxpat missing patcher"
    if(opened_patch != NULL)        {post("opened_patch found adress is %i", opened_patch);}
    else if(opened_patch == NULL)   {post("opened_patch not found : %i", opened_patch);return;}
    //also states opened_patch not found

Am I doing s.th wrong or is jpatcher_load_fromdictionary itself causing the problem?
Getting this fixed would be an immense help.

Greetings,
Alex

lx's icon

Solved.
If anyone wants to open a top-level-patcher with arguments and attributes from code,
the rough sketch to solve this could look like this:

As a maxpatch is a textfile in the json-format one can do operations
specific to textfiles(replace occurences of (#1,#2)) with your arguments
and traverse through the json-file to set specific attributes with the help of a json parser.
The edited data then must written to the hd(if you need a pointer to the created patch later). (Otherwise it might be possible to open it from buffer)
After that you can call stringload("your_patcher") which seems to be the only correct working function to open a top-level patcher from code at this time returning a pointer to that opened patcher.
Since the size of maxpatches are quite small(...of course it depends on your patch)
the process of writing to hd is no speed issue. 90% is taken for drawing the stuff on screen while 10% are used for preparation. On a MBP(2,7 GHz Intel Core i7) opening 100 small patches(40-80 kb) takes about 5-8 seconds. (max v.6.0.8)