Getting provided External Editor from Max Configuration using c-api

Shakeeb Alireza's icon

Is there a way to obtain the configured External Editor from Max configuration via the c-api? If not is there a way to obtain it from the environment variable "EDITOR" via the c-api? I've tried the latter using std::getenv without success..

Thanks!

11OLSEN's icon

I haven't played with it but i think "ext_preferences.h" provides some methods to query Max preferences..

Shakeeb Alireza's icon

@11OLSEN

Thanks very much for the tip.

I'll check this ext_preferences.h api out and report back here if it ends up working for me.

Shakeeb Alireza's icon

OK, a quick test shows that ext_preferences.h works as advertised. You have to include the header in your external and then do something like this:

t_symbol* editor = preferences_getsym("externaleditor");

You can get the actual preference key by hovering over the preference entry and selecting 'copy attribute'.

So far so good..

Now for the problem: the full path is seemingly not stored in the field. So if you enter /usr/local/bin/subl it will only store the subl part which is not so useful if you want to use system() or std::system() which requires the full path of the executable.

Now, I have to check if there's something ext_path.h which can help with the above...

11OLSEN's icon

Maybe it's in the search path. Try absolutepath on it.

Shakeeb Alireza's icon

@11OLSEN

Maybe it's in the search path. Try absolutepath on it.

Interesting result: it picked up the EDITOR environment variable this time:

if (const char* editor = std::getenv("EDITOR")) {
    post("editor from env: %s", editor);
    x->editor = gensym(editor);
} else {
    x->editor = gensym("");
}

I couldn't get the full executable path of the preferences_getsym even if I used something like the following:

t_symbol* ck_locatefile(t_symbol* name)
{
    char filepath[MAX_PATH_CHARS];
    char abspath[MAX_PATH_CHARS];
    char conform_path[MAX_PATH_CHARS];
    short path, res;
    t_fourcc outtype;
    t_fourcc filetypelist;
    t_max_err err;

    strncpy_zero(filepath, name->s_name, MAX_FILENAME_CHARS); 
    res = locatefile_extended(filepath, &path, &outtype, &filetypelist, 1);
    if (res != 0)
        error("ck_locatefile > locatefile_extended failed");
        return;
    err = path_toabsolutesystempath(path, filepath, abspath);
    if (err != MAX_ERR_NONE)
        error("ck_locatefile > path_toabsolutesystempath failed");
        return;
    path_nameconform(abspath, conform_path, PATH_STYLE_MAX, PATH_TYPE_BOOT);
    post("abspath: %s", abspath);
    post("conform_path: %s", conform_path);
    return gensym(conform_path);
}

Well 1 out of 2 worked so not such a bad outcome... still. how to excess the fullpath of the preference editor remains a mystery.

Source Audio's icon

max does not store absolute path to external editor into preferences file

or any other place.

It must be searching for it when editor needs to start,

not on max app start.

App can be located on any hard drive or partition outside of search path.

Tested to be so on Mac and max 8 >.

11OLSEN's icon

On Windows there's the full path

Source Audio's icon

then one can read prefs file and extract it.

but not on mac ...

what I tested - assigned TEXTY.app in prefs.

opec coll editor - it works.

renamed TEXTY.app -to TEXTY1.app in same path - can't find it.

copied TEXTY.app to different HD, also out of max search path,

it finds and starts app.

All while max is running.

11OLSEN's icon

On Mac, Applications seem to be registered in some way as you can start them without actually knowing the path open -a <application name>in terminal

Shakeeb Alireza's icon

@SOURCEAUDIO @110LSEN

Thanks for the feedback, help, and illuminating differences between macOS and Windows.

It looks like open -a 'Sublime Text' does work on macOS and the issue is moot in any case with Windows since the full path is stored. So the issue looks like it's solved!

Thanks!