★ Finding your resource files
posted Thu 28 Aug 2014 by Michael Galloy under IDLWhen writing even small applications, it is often necessary to distribute resource files along with your code. For example, images and icons are frequently needed by GUI applications. Custom color table files or fonts might be needed by applications that create visualizations. Defaults might be stored in other data files. But how do you find these files, when the user could have installed your application anywhere on their system?
The answer is to place these files in a directory that you know the location of relative to your source code. Then use MG_SRC_ROOT [1] (or one of the other alternatives to it) to determine the location of your source code. Finally, use FILEPATH
to specify the location of the resource. For example, in MG_LOADCT
, I do the following to find the Brewer color tables, which are in the same directory as MG_LOADCT
:
ctfilename = filepath('brewer.tbl', root=mg_src_root())
If I had placed the color tables in a resources subdirectory which was parallel to the code directory my source code lives in, I could just use the SUBDIR
keyword:
ctfilename = filepath('brewer.tbl', subdir=['..', 'resources'], $
root=mg_src_root())
MG_SRC_ROOT
is one of my most used routines[2]. Get all the source code for mglib on GitHub.