Quick Tip: findFile

JoeStrout - Nov 21 '23 - - Dev Community

Mini Micro version 1.2 includes a new global function, findFile, which is really handy. If you're not already using it, you're missing out! And if you are already enjoying it, skim to the end, where you may learn something about it you didn't already know.

Screen shot of findFile

If you just type findFile at the prompt, it pops up a three-column file dialog as shown above. You can use the arrow keys to navigate anywhere in the file hierarchy, including the user disks (/usr and /usr2, if mounted) as well as the system disk (/sys).

The leftmost column shows the directory you're currently in, along with its context (any files or volumes at the same level). The center column lists all the files and subdirectories thereof. And the column on the right shows a preview of the file, whenever possible:

  • MiniScript source files show the first dozen lines or so of the code. You only get to see about 14 characters of each line, but this is often enough to be sure you've got the right script, or check the imports or constants at the top of the file.

  • Text files (ending in .txt, .grfon, .json, etc.) similarly show the first part of the first dozen lines or so.

  • Images show the actual picture (as in the screen shot above). These are scaled up or down as necessary to fit.

  • Sounds show the sound duration, but more importantly, the sound is actually played when selected! To play it again, just switch to another file and back.

When you've found the file you want, click the Select button, or just press Return/Enter. The dialog will disappear, and the path to the selected file will be returned.

Screen shot showing return value of a findFile call

You can then select and copy that path from the text display, edit your program, and paste it into your code. Or, when tinkering around on the command line, you can use this directly anywhere you need a file path. For example:

Screen shot showing loading of fish sprite via findFile

Note the use of findFile in the second line!

You can also use findFile when you don't actually care about the return value, but you just want to explore. It's a great way to poke around on the /sys disk, for example, and remind yourself of all the great resources and libraries that are available there.

Finally, it may not be obvious that you can call findFile in your own programs! Any time you need the user to specify a file to load or operate on, consider using findFile to give them a nice UI to do so, rather than having them type a file name or path from memory. And if you need to customize the behavior — for example, to change the title or colors, or to provide a custom preview for your custom file type — you can import "textUtil", and then subclass textUtil.FileDialog, which is the workhorse behind the findFile function.

Have you been using findFile? Do you have ideas of how you might apply it (or the underlying FileDialog) in the future? Share your thoughts in the comments below!

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .