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.
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!