Skip to main content
Inspiring
September 8, 2023
Answered

Finding Default Save Path for Photoshop .PSD Documents via JavaScript

  • September 8, 2023
  • 2 replies
  • 1933 views

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.


This topic has been closed for replies.
Correct answer definitelynotanalligator
quote

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!

2 replies

c.pfaffenbichler
Community Expert
Community Expert
September 8, 2023

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. 

Legend
September 8, 2023

On the Mac its stored as NSLastRootDirectory. Accessing this is left as an exercise for the reader.

Stephen Marsh
Community Expert
Community Expert
September 9, 2023

@Lumigraphics – thank you for the easter egg hunt!  :]

 

c.pfaffenbichler
Community Expert
Community Expert
September 8, 2023

Wouldn’t you want to use the active file’s path instead? 

Inspiring
September 8, 2023

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

Stephen Marsh
Community Expert
Community Expert
September 8, 2023

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.