Inspiring
October 13, 2024
Answered
Use text from clipboard as filename during Save As.
- October 13, 2024
- 4 replies
- 1866 views
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]
