How to write package that interacts with local files

I’m trying to write my first Atom extension, and I want to display one of a set of images at semi-random. Is this feasible? What’s the best way of getting a list of files within a folder associated with a package, or even just the absolute filepath of your own package? I’m not sure how to find this information in the documentation.

Atom is built on Electron, which is itself built on NodeJS. In this case, you want to look at the NodeJS APIs. Specifically, the fs module. The API is cross platform, and closely modeled on POSIX (so on how file IO works on Linux / macOS). You would be most interested in readdir (or it’s synchronous variant readdirSync). This will tell you the files in a directory.

Image display itself has builtin support in HTML. You can probably just use an image tag with the src property set to the file path of the image. I can’t help much here though, GUI / displaying things is not something I’ve looked into much.

For getting your packages path, use __filename or __dirname. They resolve to the filename (full path) and directory of the file they are in. From there, you can construct a path to your resources folder.

For a fs module example, try the following in your init.js file (or your package).

const fs = require("fs");

console.log(fs.readdirSync(__dirname));

The difference between readdir and readdirSync is when the results are available. The first takes a callback function as a parameter, and will call that function with the list of directory contents when they are ready. The second will block execution until the results are ready, and return them “immediately”. The second is convenient, but can cause issues like UI freezes if the call takes a long time to complete.

1 Like

Install process-palette.
You can then pass a host of Atom variables as args to say a python script. Read section “Variables” in above link. Often process-palette can be used - command running external python (or other) script - to add functionality rather than writing another package.