Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
0

Use text from clipboard as filename during Save As.

Explorer ,
Oct 13, 2024 Oct 13, 2024

HI!
I can't save as file with text from clipboard to path. I noticed that one of gpt used approach asking powershell by get-clipboard command. But I have alert: "Clipboard is empty or inaccessible". After this I checked clipboard in powershell with mentioned command and it's returns my text stored in clipboard; Any help, please.

 

#target photoshop

function getClipboardText() {
    var clipText = "";
    try {
        var tempFile = new File("~/Desktop/temp_clipboard.txt");
        app.system("powershell -command Get-Clipboard > \"" + tempFile.fsName + "\"");
        
        // Open the temporary file to read clipboard content
        tempFile.open('r');
        clipText = tempFile.read();
        tempFile.close();
        
        // Remove the temporary file
        tempFile.remove();
    } catch (e) {
        alert("Error accessing clipboard: " + e.message);
    }
    return clipText ? clipText.trim() : ""; // Return trimmed text or empty string
}

if (app.documents.length > 0) {
    var doc = app.activeDocument;
    var clipboardText = getClipboardText();
    
    if (clipboardText !== "") {
        var saveFile = new File("C:\\Desktop\\Art4CNC\\" + clipboardText + ".psb");

        var psbSaveOptions = new PhotoshopSaveOptions();
        psbSaveOptions.alphaChannels = true;
        psbSaveOptions.annotations = true;
        psbSaveOptions.embedColorProfile = true;
        psbSaveOptions.layers = true;
        psbSaveOptions.spotColors = true;

        try {
            doc.saveAs(saveFile, psbSaveOptions, true, Extension.LOWERCASE);
            alert("File saved as " + clipboardText + ".psb in C:\\Desktop\\Art4CNC\\");
        } catch (e) {
            alert("Error saving file: " + e.message);
        }
    } else {
        alert("Clipboard is empty or inaccessible.");
    }
} else {
    alert("No open documents to save.");
}

 

 

 

 

[scripting topic added by moderator]

 

 

 

TOPICS
Actions and scripting , Windows
670
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Explorer , Nov 30, 2024 Nov 30, 2024

Thank you guys,
Ended up with this one, works fine: it takes clipboard and name with it an open document without any interruption.
Updated: it's .jsx script that creates .bat file and Clipboard file and then delete them. 

 

//  The script will seamlessly save open document as ClipboardText.psb in the same directory;
//  known issues: cyrillic characters will result in underscore.
 
// Function to execute a command
function executeCommand(command) {
    var fileObj = new File(command);
    fileObj.ex
...
Translate
Adobe
Community Expert ,
Oct 13, 2024 Oct 13, 2024

You can find cross-platform ExtendScript code for text to clipboard in these topics:

 

 
 I'm not sure about a text or variable from the clipboard though, how does the text get in the clipboard in the first place and could you use a variable instead to capture the text?
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Oct 14, 2024 Oct 14, 2024

Adobe has zero support for Powershell code. There is a workaround that I use involving writing a temp file and executing it with Windows Scripting Host but its a kludge at best.

You can create a temp document or add a text layer to your existing document, paste, copy the textitem, and delete your temp file/layer.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Nov 30, 2024 Nov 30, 2024

Seems like Photoshop can't name layer with clipboard text via script: it's returns error "clipboard is empty of inaccessible", despite I can name layer with text from clipboard manually via Ctrl+V while editing layer name.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 30, 2024 Nov 30, 2024

This is why I previously asked how the text gets in the clipboard – what are the workflow steps? Could the workflow be adjusted to work within the constraints of scripting?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Nov 30, 2024 Nov 30, 2024

-I copied subject in my Thunderbird mail client. Unfortunatelly I can't see if any workaround can be used here.. Sometimes I should copy filename from another file instead of mail subject.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Nov 30, 2024 Nov 30, 2024

The action is next: I open read-only .psb template, then manually copy subject from mail and paste it as filename at Save As  step in the action. It is interruption in my pipeline I'd like to avoid. 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 30, 2024 Nov 30, 2024

Yes, there are limitations, and you have to work with them unless you will use a macro or some other software instead of scripting.

 

For example, if the copied text was saved as a text file, or if the email was saved to a file, then a script can get the name of the file object or read the text file contents. So perhaps your PowerShell code can take the clipboard contents and make a new folder or text file with the clipboard contents as a filename or contents, then a Photoshop script can get the name.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Nov 30, 2024 Nov 30, 2024

Macro are the Visual Basic scipts, am I correct?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
People's Champ ,
Nov 30, 2024 Nov 30, 2024

Write your command in a bat file and run it instead of app.system() like here .

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Nov 30, 2024 Nov 30, 2024

Thank you guys,
Ended up with this one, works fine: it takes clipboard and name with it an open document without any interruption.
Updated: it's .jsx script that creates .bat file and Clipboard file and then delete them. 

 

//  The script will seamlessly save open document as ClipboardText.psb in the same directory;
//  known issues: cyrillic characters will result in underscore.
 
// Function to execute a command
function executeCommand(command) {
    var fileObj = new File(command);
    fileObj.execute();
}

try {
    // Get the directory of the current document
    var doc = app.activeDocument;
    var docPath = doc.path;

    // Define the batch file path and clipboard file path
    var tempBatchFile = new File(docPath + "/TempClipboard.bat");
    var clipboardFilePath = docPath + "/Clipboard.txt";

    // Create the batch script
    tempBatchFile.open("w");
    tempBatchFile.writeln("@echo off");
    tempBatchFile.writeln(":: Get the directory where the batch file is located");
    tempBatchFile.writeln("set \"batchDir=%~dp0\"");
    tempBatchFile.writeln(":: Save clipboard content to Clipboard.txt in the same directory");
    tempBatchFile.writeln("powershell -command \"& {Get-Clipboard | Out-File -FilePath '%batchDir%Clipboard.txt' -Encoding UTF8}\"");
    tempBatchFile.close();

    // Execute the Batch script to create Clipboard.txt
    executeCommand(tempBatchFile.fsName);

    // Wait for the batch file to complete
    $.sleep(1000);

    // Remove the temporary batch file
    tempBatchFile.remove();

    // Read the text content from Clipboard.txt
    var inputFile = new File(clipboardFilePath);
    inputFile.open("r"); // Open the file in read mode
    // inputFile.open("r",'e', 'TEXT', '????');
    var fileName = inputFile.read(); // Read the file content as is
    inputFile.close();
    inputFile.remove(); // Optionally delete the clipboard file

    // Trim leading and trailing spaces
    fileName = fileName.replace(/^\s+|\s+$/g, ""); // Remove leading and trailing whitespace

    // Sanitize filename: Replace invalid characters, replace spaces, and limit characters
    fileName = fileName.replace(/[\/\\:*?"<>|]/g, "_"); // Replace invalid characters
    fileName = fileName.replace(/\s+/g, "_"); // Replace spaces with underscores
    fileName = fileName.substring(0, 60); // Limit characters to 60

    // Create the save file path with .psb extension
    var saveFile = new File(docPath + "/" + fileName + ".psb");

    // Function to save as PSB with specified options
    function saveAsPSB(thePath, asCopy, theDialog) {
        var s2t = function (s) {
            return app.stringIDToTypeID(s);
        };
        var descriptor = new ActionDescriptor();
        var descriptor2 = new ActionDescriptor();
        descriptor.putObject(s2t("as"), s2t("largeDocumentFormat"), descriptor2); // Specify PSB format
        descriptor.putPath(s2t("in"), thePath); // Specify save path
        descriptor.putBoolean(s2t("copy"), asCopy); // Save as a copy if true
        descriptor.putBoolean(s2t("lowerCase"), true); // Use lowercase extension
        descriptor.putBoolean(s2t("layers"), true); // Save with layers
        descriptor.putBoolean(s2t("alphaChannels"), true); // Save with alpha channels
        descriptor.putBoolean(s2t("maximizeCompatibility"), false); // Don't maximize compatibility
        descriptor.putBoolean(s2t("embedProfile"), true); // Embed color profile

        executeAction(s2t("save"), descriptor, theDialog); // Execute the save action
    }

    // Save the document strictly as PSB
    saveAsPSB(saveFile, false, DialogModes.NO);

} catch (e) {
    // Minimal error handling for unexpected issues
    alert("Error: " + e.message);
}

 

 

 

 

 

 

 

 

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Nov 30, 2024 Nov 30, 2024

.bat file:

@ECho off
:: Get the directory where the batch file is located
set "batchDir=%~dp0"

:: Save clipboard content to Clipboard.txt in the same directory
powershell -command "& {Get-Clipboard}" > "%batchDir%Clipboard.txt"
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Nov 30, 2024 Nov 30, 2024

But still with minor issue: I need .psb file. Current open document is also .psb file. 
But script won't save it as psb. It's saves as psd even when file size is over 2gb.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 30, 2024 Nov 30, 2024
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Nov 30, 2024 Nov 30, 2024

Thank you, it works now!
I've updated posted script with it.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
People's Champ ,
Dec 01, 2024 Dec 01, 2024
quote

I've updated posted script with it.

By dendenis82

 

There  are a few more parameters that can be passed in the descriptor. If they are missing, they take some default values. You may want to control some of them.

 

UPD. Also, the bat (cmd) file does not need to be stored separately You can create it in a temporary folder using your script, then run it and then delete it.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Dec 02, 2024 Dec 02, 2024
LATEST

Oh, it's just amazing what scripts can do! 
I've updated the script to single .jsx version.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines