Is it possible to store/reuse a userfolder object?


#1

Scenario: The user picks a folder with several resources. The user makes choices in a dialog that determine how the content is drawn.

if the user wants to run the plugin again with the same content from the file system but different settings, the plugin needs to remember the folder the user picked.

I’m kinda assuming that access to the file system is one a one-time basis. Now I’m wondering if I could store the userFolder object in pluginData and re-use it when the plugin runs again so the user doesn’t have to choose a folder of resources every time?


#2

You can save folder references to a variable for a single session per project but if you reload your plugin or restart XD your user will have to choose a folder again. It doesn’t matter if you’ve saved the folder location.

Define a variable in your plugin and set it to the folder.

var exportFolder;

async function browseForExportFolder() {

  try {
	 if (exportFolder==null) exportFolder = await fileSystem.getFolder();
  }
  catch (error) {
	console.log(error.stack);
  }

  return exportFolder;
}

module.exports = {
  commands: {
	selectFolder: browseForExportFolder
  }
};

#3

sweet, works!

In my mental model, I always figured that all variables are re-set when a plugin runs, but now I understand that a plugin is ‘constant’ in a way.


#4

When a plugin is loaded (or reloaded) the code in main.js is run. It’s sort of the same as the browser running window level JavaScript.

So if your main.js contains the following:

// code below is run on startup or reload of plugin
const {Artboard, BooleanGroup, Matrix} = require("scenegraph");
var foo = "Hello";
console.log(foo);

function showMainDialog() {
    console.log("world")
}

module.exports = {
  commands: {
	showMainDialog:showMainDialog, 
  }
};

When you reload your plugin (or open XD) the console will show:

Hello

and when you open your plugin the console will add:

world

The second time you open your plugin the console will add world again but not Hello.