I decided to finish off my channel swizzler by creating a working files and Unity asset folder connection, so that the file in the asset folder is never actually edited, and there’s no confusion with which is which. It also means Unity will never import workfiles which will happen if they are stored in the project directory.
Because my hypthothetical users might store their project folder on different drives, locations etc, I decided to control the path through an environment variable. Editing things on the user’s pc like this isn’t always the best course of action, but works well for situations when you can’t bank on getting the user’s documents or another folder that windows knows.
To get photoshop to use this, I used $.getenv (which I thought was broken for a while, until realizing I’d made the variable with photoshop open – oops!), and then replaced all the backslashes with forward ones. This is because backslash is an escape character, used for declaring newlines. Before I did this, the string was broken, and photoshop kept defaulting to its own path instead.
I then wanted to match the folder within workfiles to the folder within assets/textures. This meant I could manage the Unity folder structure via my saved workfile and didn’t need to resave/move/copy anything to get a nice organised folder. To get the last element in the path.split, I had to use string.pop(), which returns the last element of the list while removing it from said list. (or should I say array? We’re not in Python anymore Toto!) This worked nicley for this purpose, but I would like to find the equivalent of Python’s list[-1] as its really handy for this sort of thing.
var myfolderPath = $.getenv('PROJECT_PATH').replace("\\", "/") + '/' + 'Assets/Textures' + '/' + String(currentDoc.path).split("/").pop(); var filePath = new File(myfolderPath + '/' + currentDoc.name.replace(/\.[^\.]+$/, '.psd'));