# File Selector Persistent Entry



## derstefmitf (Mar 16, 2018)

Hi,

how can I make sure that a file selector remembers the last file I selected when I reload the NKI/script. I checked the KSP manual and found a command, but it does not work:


```
declare read @file_path
declare @basepath
preset.basepath := get_folder(GET_FOLDER_PATCH_DIR)
declare ui_file_selector browser
preset.browser->basepath:= preset.basepath & "/Data"
set_bounds(preset.browser, 35, 130, 500, 320)
preset.browser->file_type:= NI_FILE_TYPE_ARRAY
set_control_par_str(get_ui_id(preset.browser), CONTROL_PAR_FILEPATH, preset.file_path)
```

and in the ui callback I set the variable file_path to the correct file:

```
preset.file_path := fs_get_filename(get_ui_id(preset.browser), 2)
```

Unfortunately, it does not work for me. 

Thanks for your help.


----------



## EvilDragon (Mar 16, 2018)

Yeah, that should work...


----------



## derstefmitf (Mar 19, 2018)

Hey, I think I found the problem. I am working on an unencoded library and I set the basepath to the patch directory. In this case the script does not work. When I set the basepath to GET_FOLDER_LIBRARY_DIR and copy some .nka files to this folder, I can load them, save the NKI and after opening the instrument again the file path is set correctly and the last chosen .nka file is selected. Is this a bug?


----------



## EvilDragon (Mar 19, 2018)

I don't think it's a bug. Unencoded libraries work differently because of how $GET_FOLDER_LIBRARY_DIR is set to behave in this case (different folder).

One thing I'm not sure about is how is your library folder structure set up? If you have NKIs in their own subfolder, and Data folder outside of it (which would be a sane thing to have, NKR in the library root folder also makes it so that Data folder is there, too), then you're missing ".." to go back one folder level. Here:


```
preset.browser->basepath:= preset.basepath & "../Data"
```


----------



## derstefmitf (Mar 20, 2018)

Yeah, but I do not want to use $GET_FOLDER_LIBRARY_DIR. This was simply to check if this works, because $GET_FOLDER_PATCH_DIR does not work. Well it works, but it simply does not keep the last chosen entry. 

I do not have an instruments subfolder, otherwise my script would not work. The product will have only one NKI, so no problem here. The release version will have a Data folder, one NKI, documentation folder and a samples folder with the nkr in it.


----------



## EvilDragon (Mar 20, 2018)

Dunno. I remember it did work back when I used $GET_FOLDER_PATCH_DIR...


----------



## derstefmitf (Mar 20, 2018)

EvilDragon said:


> Dunno. I remember it did work back when I used $GET_FOLDER_PATCH_DIR...


Good to know. I already informed NI, maybe it broke when something got fixed :D


----------



## derstefmitf (Mar 20, 2018)

With the help from a good friend, I found the dumb little mistake. Obviously, it has to be

```
preset.basepath := get_folder(GET_FOLDER_PATCH_DIR) & "Data/"
```
 and not

```
preset.basepath := get_folder(GET_FOLDER_PATCH_DIR) & "/Data"
```

So simply moving the slash fixed it :D


----------



## EvilDragon (Mar 20, 2018)

Or you could make it doubly sure and use "/Data/" (double slashes would work, in case of preceeding one). So basically what you got there would merge the filepath right after basepath, so you'd end up with /Datanameofthepreset.nka, which is obviously not what you want. Ehehe.


----------



## derstefmitf (Mar 20, 2018)

No, because the file path gets set by the variable preset.file_path, but I think KONTAKT just has a problem when the basepath is not set coherently to the filepath.


----------



## EvilDragon (Mar 20, 2018)

Yes, that variable actually contains "nameofthepreset.nka", without any slashes. So if you didn't have a slash after folder name "Data", it wouldn't correctly parse the folder(s) up to the file.


----------



## nerine (Jul 31, 2018)

Hello!
I have also created a file selector for the .nka files. The nka files are listed in different subfolders in the Data folder depending on their style. The selector seems to work fine and it remembers the last file i selected when i reload a NKI.

If i compress the instrument's folder (that contains the nki, samples and all the data) and then transfer it to another location and try to extract it, it does not remember the last nka i had selected before compressing the folder. After the compression it remembers the base path but not the file path.

The script has the same logic as derstefmitf's script.

_In the init callback the base path uses: @basepath := get_folder($GET_FOLDER_PATCH_DIR) & "Data/"
and in the ui_control($file_browser): @file_path := fs_get_filename(get_ui_id($file_browser), 2)_

Did anyone have this issue? Any ideas would be appreciated.
Thanks in advance.


----------



## EvilDragon (Jul 31, 2018)

When you move a library to a new computer, chances are that the path to the library won't be the same as on your development computer.

The trick is to have a variable that will store the first time the patch is opened, make it persistent, then set the file path if first_time is equal to 1. It works something like this:


```
on init
    declare first_time := 1
    declare @base_path
    declare @file_path

    <declare your file browser and its properties, etc.>

    make_persistent(file_path)
    read_persistent_var(file_path)

    { these two lines are supposed to be COMMENTED when developing! }
    make_persistent(first_time)
    read_persistent_var(first_time)

    if first_time = 1
        file_path := <whatever path you want to set it to by default when the instrument opens for the first time for the user>
    end if

    <set $CONTROL_PAR_FILEPATH for file browser>
end on

on ui_control (Browser)
    if first_time = 1
        first_time := 0
    end if

    { this line must be UNCOMMENTED when developing! }
    { first_time := 1 }

    file_path := fs_get_filename(get_ui_id(Browser), 2)

    < etc. >
end on
```

The important part is to make sure that first_time is NOT made persistent when you develop the library. It's supposed to be a "fresh" variable. You make it persistent only when your NKI files are all done and ready to ship, then you compile the code one last time with those two lines uncommented (and in UICB of the browser that one line MUST be commented for the release!), and repack the NKR.


----------



## nerine (Aug 1, 2018)

It works now! Thanks so much


----------



## derstefmitf (Aug 3, 2018)

Great, I just used this trick. Thanks again


----------



## derstefmitf (May 9, 2019)

Does anyone experience an issue with this workaround when using K6.1.0?


----------



## EvilDragon (May 12, 2019)

Yeah there's a known issue with K6.1 and file selector filepaths not being recalled, it's being looked at.


(BTW, the code to handle this is not exactly a workaround, it's how it's supposed to be done )


----------



## derstefmitf (May 13, 2019)

EvilDragon said:


> Yeah there's a known issue with K6.1 and file selector filepaths not being recalled, it's being looked at.
> 
> 
> (BTW, the code to handle this is not exactly a workaround, it's how it's supposed to be done )


Thanks for the quick reply. Good to know that NI is working on it.


----------

