Copy link to clipboard
Copied
Hey Photoshop wizards,
I am writing a script to assist in saving .psd files:
// Define the folder path
var folderPath = "C:/XXX/";
// Get the current document's name
var currentDocumentName = app.activeDocument.name;
// Check if the current document's name exists in the folder
var fileExists = false;
// Initialize variables for finding the largest number
var largestNumber = 0;
// Open a folder object
var folder = new Folder(folderPath);
// Get a list of all files in the folder
var fileList = folder.getFiles();
// Loop through the files to check if the current document's name exists
for (var i = 0; i < fileList.length; i++) {
var file = fileList[i];
// Check if the file is a PSD with the same name
if (file instanceof File && file.name === currentDocumentName) {
fileExists = true;
break;
}
// Check if the file is a PSD with the name pattern "Untitled-X.psd"
if (file instanceof File && file.name.match(/^Untitled-(\d+)\.psd$/)) {
var match = file.name.match(/^Untitled-(\d+)\.psd$/);
var number = parseInt(match[1], 10);
// Update the largest number if necessary
if (number > largestNumber) {
largestNumber = number;
}
}
}
// If the file exists in the folder, save the current document
if (fileExists) {
app.activeDocument.save();
alert("The current document has been saved.");
} else {
// Increment the largest number to get the next available number
var nextNumber = largestNumber + 1;
// Define the new filename
var newFileName = "Untitled-" + nextNumber + ".psd";
// Save the current document with the new filename
var saveFile = new File(folderPath + newFileName);
app.activeDocument.saveAs(saveFile);
// Alert the user with the saved filename
alert("Saved as: " + newFileName);
}
TL;DR - it replaces Ctrl + S, but it renames the base file name Untitled-1.psd to Untitled-2.psd, Untitled-3.psd, -4 and so on without the need to edit the digit each time you save a new document in same folder.
The problem is that if I need to change my default save location(here it is folerPath variable - "C:\XXX"), I need to edit the script's save path as well.
It would be great if it just copied the save path from Photoshop's preferences, but I couldn't find any file with the config. I assume it is somewhere in 'C:\Users\X\AppData\Roaming\Adobe\Adobe Photoshop 2022\", because when I deleted that folder and started Photoshop again, the default save path was reset to default(Documents folder) - but I couldn't find it using Notepad++ and it's search feature.
I see your point now, great idea!
Here's the code for the main script:
if (app && app.documents.length > 0) {
main();
}
else {
alert("Please open a document in Photoshop to use this script.");
}
function main() {
var savedPathGlobal = "";
function saveFolderPathToFile() {
// Get the directory of the currently executing script
var scriptFile = File($.fileName);
var scriptFolder = scriptFile.parent;
// Create a reference to the text file
var infoFile = new File(scri
...
Copy link to clipboard
Copied
Wouldn’t you want to use the active file’s path instead?
Copy link to clipboard
Copied
The problem is there is no path in the first place. If you start this script and try to get active file's save path, for example, with this kind of command:
alert("get path of activeDoc: "+(activeDocument.path.fsName));
it wouldn't work with unsaved before documents because they don't exist yet, thus there is no path to where is the file stored. And for already saved documents, there is no need for this script because that will be just Ctrl + S.
Of course you could check current opened documents and see their save paths, but what if they are from different folders, or, there are none open?
I am open to changing my code in any way to get this to work, the main problem is figuring out the user's preferenced save path without entering it manually
Copy link to clipboard
Copied
it wouldn't work with unsaved the main problem is figuring out the user's preferenced save path without entering it manually
By @definitelynotanalligator
I wasn't aware that there was a user preference save location.
Copy link to clipboard
Copied
I don’t think that location is written as a Preferences file and even if it would that would probably happen on quitting and during a session it would only be stored in RAM/scratch.
Copy link to clipboard
Copied
it wouldn't work with unsaved the main problem is figuring out the user's preferenced save path without entering it manually
By @definitelynotanalligator
I wasn't aware that there was a user preference save location.
By @Stephen_A_Marsh
I don't know the exact name for this feature, but here is what I mean by user preference save location:
First picture - my preferred save location. When I saved my .psd file there once, every other .psd file I try to save - it opens this folder where I can save my desired file:
But let's say I want to change the default save folder to a different one and put this file in an other folder and make it the default one. I open the Save As window via Ctrl + S, and navigate to it, then save:
After saving it, the next .psd file I save(not the same document, different one), the Save As window will have the last save folder location opened.
(It applies to both Save As and Save options(aka Ctrl+Alt+S and Ctrl+S)).
Here I tried to save another file, and this path was opened by default:
Note that I did not do anything but just save a file in my 'user preferred location', it memorizes my choice of where I want the files to be saved when I save one of my files there.
By the way, the exact same thing applies to Export as well - when I export a picture as, for example, .png, it memorizes what folder do I put the file in and opens it as the 'default' page in future exports.
Copy link to clipboard
Copied
You can have the script check for the existence of a custom preference file that contains the preferred save path.
If the pref file exists, move on and save.
If the pref file doesn't exist prompt the user to set the save path for any files that don't have a backing path (new unsaved files). Existing files would use their backing path or the one from the pref file.
Copy link to clipboard
Copied
While this would bring more user-friendly interaction with the script, it doesn't solve the problem - you still have to manually copy and paste the path you want your files to be saved within.
Since it's just me who will use this script, it's not much difference for me to just edit the script's folderPath variable or to have it in custom preference file, where I still have to copy it to each time I change a folder(I don't quite do that often, but it would be awesome if it was automated - and here we are, back to the title of this topic - need to find the default user's save path preference).
I did find:
C:\Users\USERNAME\AppData\Roaming\Adobe\Adobe Photoshop 2020\Adobe Photoshop 2020 Settings\export-as-settings.json
Where one can find the LAST_EXPORTED_LOCATION
By @Stephen_A_Marsh
I looked for that file in PS 2022 and PS beta, but, unfortunately, didn't find it.
Copy link to clipboard
Copied
it doesn't solve the problem - you still have to manually copy and paste the path you want your files to be saved within.
By @definitelynotanalligator
Not if using a preference file or session variable. All you need to do is select the folder via a folder selection GUI step. The selected directory path is then set without needing to manually update the script.
Copy link to clipboard
Copied
I see your point now, great idea!
Here's the code for the main script:
if (app && app.documents.length > 0) {
main();
}
else {
alert("Please open a document in Photoshop to use this script.");
}
function main() {
var savedPathGlobal = "";
function saveFolderPathToFile() {
// Get the directory of the currently executing script
var scriptFile = File($.fileName);
var scriptFolder = scriptFile.parent;
// Create a reference to the text file
var infoFile = new File(scriptFolder.fullName + "/selectedFolderPath.txt");
// Check if the text file doesn't exist or is empty
if (!infoFile.exists || infoFile.length == 0) {
// Select a folder to save files
var saveFolder = Folder.selectDialog("Select a folder to save your file");
if (saveFolder) {
// Get the selected folder's path
var savedPath = saveFolder.fsName;
// Open the text file for writing
infoFile.open("w");
infoFile.encoding = "UTF-8"; // Set the encoding to handle non-ASCII characters
infoFile.writeln(savedPath);
infoFile.close();
savedPathGlobal = savedPath + "\\";
// Display an alert with the selected folder path
alert("Selected folder path:\n" + savedPath + "\nPath saved to:\n" + infoFile.fsName);
} else {
alert("Folder selection was canceled.");
}
} else {
// If the text file exists and is not empty, use the stored path
infoFile.open("r"); // Open the text file for reading
savedPathGlobal = infoFile.readln() + "\\"; // Read the saved path
infoFile.close(); // Close the text file
//alert("Using the previously saved folder path:\n" + savedPathGlobal);
}
}
// Call the function to run the script
saveFolderPathToFile();
// Define the folder path
var folderPath = savedPathGlobal;
// Get the current document's name
var currentDocumentName = app.activeDocument.name;
// Check if the current document's name exists in the folder
var fileExists = false;
// Initialize variables for finding the largest number
var largestNumber = 0;
// Open a folder object
var folder = new Folder(folderPath);
// Get a list of all files in the folder
var fileList = folder.getFiles();
// Loop through the files to check if the current document's name exists
for (var i = 0; i < fileList.length; i++) {
var file = fileList[i];
// Check if the file is a PSD with the same name
if (file instanceof File && file.name === currentDocumentName) {
fileExists = true;
break;
}
// Check if the file is a PSD with the name pattern "Untitled-X.psd"
if (file instanceof File && file.name.match(/^Untitled-(\d+)\.psd$/)) {
var match = file.name.match(/^Untitled-(\d+)\.psd$/);
var number = parseInt(match[1], 10);
// Update the largest number if necessary
if (number > largestNumber) {
largestNumber = number;
}
}
}
// If the file exists in the folder, save the current document
if (fileExists) {
app.activeDocument.save();
//alert("The current document has been saved.");
} else {
// Increment the largest number to get the next available number
var nextNumber = largestNumber + 1;
// Define the new filename
var newFileName = "Untitled-" + nextNumber + ".psd";
// Save the current document with the new filename
var saveFile = new File(folderPath + newFileName);
app.activeDocument.saveAs(saveFile);
// Alert the user with the saved filename
//alert("Saved as: " + newFileName);
}
}
And for the second one, which allows you to change the default save folder if needed:
function saveFolderPathToFile() {
// Get the directory of the currently executing script
var scriptFile = File($.fileName);
var scriptFolder = scriptFile.parent;
// Create a reference to the text file
var infoFile = new File(scriptFolder.fullName + "/selectedFolderPath.txt");
// Check if the text file doesn't exist or is empty
// Select a folder to save files
var saveFolder = Folder.selectDialog("Select the default save location folder");
if (saveFolder) {
// Get the selected folder's path
var savedPath = saveFolder.fsName;
// Open the text file for writing
infoFile.open("w");
infoFile.encoding = "UTF-8"; // Set the encoding to handle non-ASCII characters
infoFile.writeln(savedPath + "\\");
infoFile.close();
savedPathGlobal = savedPath;
// Display an alert with the selected folder path
alert("Selected folder path:\n" + savedPath + "\nPath saved to:\n" + infoFile.fsName);
} else {
alert("Folder selection was canceled.");
}
}
// Call the function to run the script
saveFolderPathToFile();
If anyone like me wants to use this, here's the tutorial on installing it:
1. Copy and paste the first code, put it in a .txt and rename it's extension to .jsx. Do the same to the second code.
For example, you can use names like FileSave.jsx and FileSaveFolder_Change.jsx, but feel free to choose different ones.
2. Open your photoshop folder -> Presets -> Scripts, put both scripts in this folder
3. Start(or restart if it is opened) Photoshop
4. Open a document
5. In top left corner there is File. Press it, find scripts, and there will be the two scripts you created
If you need to save files as Untitled-1, Untitled-2, -3 and so on - use the first script.
Whenever you need to change these files' save location, use the second script.
Feel free to bind them to any hotkeys!
Copy link to clipboard
Copied
@Stephen_A_Marsh wrote:it wouldn't work with unsaved the main problem is figuring out the user's preferenced save path without entering it manually
By @definitelynotanalligator
I wasn't aware that there was a user preference save location.
I don't know this feature's name, but here is what I mean by 'user preference save location':
Let's say I want to save my images in this folder here. I open a new document in Photoshop, do something in it, then press Save in photoshop to save the .psd file I've been working in:
Photoshop opened this window, because I saved .psd files here before.
Now, for example, let's change our default save folder to the other one:
I navigated to NewLocationExample folder, then I save the document there.
After that, I create another new document. Then I do something in it, and press Save again:
I did not navigate to that folder - when I pressed save, it was opened by 'default'.
This 'default' path is what I meant by 'user preference save location'.
And this principle applies to Export option as well - if I export .png in some folder, it memorizes the location, and the future exports are opened in the memorized location.
Copy link to clipboard
Copied
For some reason my replies disappear? Maybe it's due to the images I apply.
I will try to describe it with words only.
it wouldn't work with unsaved the main problem is figuring out the user's preferenced save path without entering it manually
By @definitelynotanalligator
I wasn't aware that there was a user preference save location.
By @Stephen_A_Marsh
I don't know this feature's name, but here is what I mean by 'user preference save location':
Let's say I want to save my images in this folder here - F:\Photoshop3. I open a new document in Photoshop, do something in it, then press Save in photoshop to save the .psd file I've been working in.
Photoshop opened 'Save as' window with path F:\Photoshop3, because I saved .psd files there before.
Now, for example, let's change our default save folder to the other one:
I navigated to C:\Users\X\Desktop\NewLocationExample folder, then I saved the document there.
After that, I create another new document. Then I do something in it, and press Save again:
I did not navigate to NewLocationExample folder - when I pressed save, in the popped out 'Save as' window, the NewLocationExample was opened by 'default'.
This 'default' path is what I meant by 'user preference save location'.
And this principle applies to Export option as well - if I export .png in some folder, it memorizes the location, and the future exports are opened in the memorized location.
Copy link to clipboard
Copied
This target location seems to persist even after restarting Photoshop so it appears to be stored somewhere but I can‘t think of a way to access that information so far.
Copy link to clipboard
Copied
On the Mac its stored as NSLastRootDirectory. Accessing this is left as an exercise for the reader.
Copy link to clipboard
Copied
Copy link to clipboard
Copied
At least in version 2020 in Windows, the Save As location and Export As location don't appear to be the same?
I did find:
C:\Users\USERNAME\AppData\Roaming\Adobe\Adobe Photoshop 2020\Adobe Photoshop 2020 Settings\export-as-settings.json
Where one can find the LAST_EXPORTED_LOCATION
However, this is independent of the Save As location.
Perhaps someone can find the required file... But it may be hiding in a binary preference file.