Copy link to clipboard
Copied
Hey there,
sadly i´m still new in the world of PS scripting but i´ve already fount so much useful scripts, tips and ideas here!
Currently i´m looking für a script (which sadly exceeds my knowledge) to rename my files and save them into a new "processec" folder at the same place.
The files i´m going to work on are named like: §BI00337684§_HPK1155.JPG or §AN00550282§_CHJ3942.JPG or §AD00093642§_IMG0023.JPG
It would be so cool and so much work saving if a script can delet everything after the "_" and the "_" itself.
Maybe somebody could help me...
Thanks in advance, Chris from austria!!
Copy link to clipboard
Copied
So you have a JPEG file open that has been edited.
What happens to this file file when the script renames etc?
Does the original file stay open with the renamed file saved as a copy in the backgroun?
Or does the edited file save as with the new name and remain open, leaving the original as per the last save state?
Can you list step by step what should happen?
Copy link to clipboard
Copied
Hey Stephen,
i´m going to use this script in an PS action.
1. create a copy of the file
2. save it (without the _abc1234) into a "processed" folder
3. remain open the processed file and leave the original as it was
4. PS action with some standard stuff
5. now i will work on each file individually and save it
OH, i made a mistake... my files look like "1ABCD_§BI00337684§_HPK1155.JPG" sorry...
Thanks!!
Copy link to clipboard
Copied
Hi Chris,
Let me start by saying that I can’t write script code, on a good day I can hack/change an existing script… and on a really good day I can combine parts of different scripts together to make a new script or perhaps create a few lines of script from scratch.
The code that I have knocked together is a work on progress, I am not 100% happy with it but it get’s some of the job done and it is a minor miracle that I have made it this far!
What the script does:
* Duplicates the original document, removing the underscore and alphanumeric directly before the filename extension from the duplicate’s filename. This step should ideally have error checking code in case the source file has not been saved, however that is a little advanced for me at this stage, so the presumption is a saved file as the source
* Returns to the original open document, closes without saving any changes – this step presumes that you simply opened the file and did not do anything and don’t wish to save anything if something was done
* Returns to the duplicated document
* Creates a new folder titled “Processed” in the same folder as the original document
What the script doesn't do:
You then have to manually save the file and format into the processed folder, as I’m still trying to figure out how to save the file into the new directory.
Questions:
Is the input file always a JPEG? Do you always wish to save a JPEG to the processed folder? If your action creates any layers, will they be flattened before you save over the processed file?
Copy link to clipboard
Copied
Hey Stephen,
first of all thanks a lot for your time and hard work!!
What you are doing sounds a little bit like what I'm doing.
I just startet to collect scripts, analyze them and trying to modify them a little bit...
To answer your question: Yes i will only use .JPG files and will also save them as .JPG without any layers.
Maybe i can help you out because i already found a way to save a document into a new folder 😉
[CODE]
var thedoc = app.activeDocument;
var docName = thedoc.name;
if (docName.indexOf(".") != -1) {var basename = docName.match(/(.*)\.[^\.]+$/)[1]}
else {var basename = docName};
try {var docPath = thedoc.path}
catch (e) {var docPath = "~/Desktop"};
var folderString = docPath+"/processed";
var theCopy = thedoc.duplicate(basename,false);
OutFoldJPEG(folderString,basename,12,true,MatteType.NONE,Extension.LOWERCASE);
theCopy.close(SaveOptions.DONOTSAVECHANGES);
function SaveJPEG(saveFile, jpegQuality,cProfile,MType,fCase){
jpgSaveOptions = new JPEGSaveOptions();
jpgSaveOptions.embedColorProfile = cProfile;
jpgSaveOptions.formatOptions = FormatOptions.STANDARDBASELINE;
jpgSaveOptions.matte = MType;
jpgSaveOptions.quality = jpegQuality;
activeDocument.saveAs(saveFile, jpgSaveOptions, true,fCase);
}
function OutFoldJPEG(docPath,dnme,jpegQuality,cProfile,MType,fCase){
var outfolder = new Folder(docPath)
if (outfolder.exists == false){
outfolder.create();
var saveFile = new File(outfolder + "/" + dnme);
SaveJPEG(saveFile, jpegQuality,cProfile,MType,fCase);}
else{
var saveFile = new File(outfolder + "/" + dnme);
SaveJPEG(saveFile, jpegQuality,cProfile,MType,fCase);}
}
[/CODE]
Copy link to clipboard
Copied
If I understand something, then try this
//main(false); // uncomment for move files
//main(true); // uncomment for copy files
function main(move)
{
var src_folder = Folder.selectDialog("Select source", "C:\\");
if (!src_folder) return;
var dst_folder = Folder.selectDialog("Select destination", src_folder.fullName);
if (!dst_folder) return;
var files = src_folder.getFiles("*.jpg");
var cnt = 0;
var cnt2 = 0;
for (var i = 0; i < files.length; i++)
{
var name = files.name;
var n0 = name.indexOf("_");
var n1 = name.lastIndexOf(".");
if (n0 > 0 && n1 > 0 && n0 < n1) { ++cnt; name = name.substr(0,n0) + name.substr(n1); }
var f = new File(dst_folder.fullName + "\\" + name);
if (move)
{
var f = new File(dst_folder.fullName + "\\" + name);
if (f.exists) { ++cnt2; f.remove(); }
files.rename(f);
}
else
{
if (f.exists) ++cnt2;
files.copy(dst_folder.fullName + "\\" + name);
}
if (files.error) { alert(files.error); return; }
}
alert(files.length + " files were " + (move?"moved":"copied") + " and\n" + cnt + " were renamed\n" + cnt2 + " were overwritten")
}
Copy link to clipboard
Copied
Hi Bardioch
perhaps another way is also right for you:
// copy_activeDoc2otherLocationWithShortenName.jsx
// https://forums.adobe.com/thread/2537114
main ();
function main() {
try {
var aDoc = activeDocument;
var aName = aDoc.name.replace(/_\w{3}\d{4}\./,".");
var aFolder = Folder(aDoc.path+"/processec");
if (!aFolder.exists) { aFolder.create() };
var copyFile = File(aDoc.fullName).copy (aFolder+"/"+aName);
//aDoc.close();
//open(File(aFolder+"/"+aName));
}
catch (e) {
alert ("no opened document or document was never saved - exit")
return;
}
};
This renames the file name for your requirements:
aDoc.name.replace(/_\w{3}\d{4}\./,".")
Have fun
Copy link to clipboard
Copied
Hi Chris, I’ll see if I can graft your code sample onto what I have achieved so far. For what it is worth, here is my work in progress:
#target photoshop
var originalDoc = app.activeDocument
var originalPath = originalDoc.path
var sub_dir = new Folder(originalPath + '/' + 'Processed')
originalDoc.duplicate (originalDoc.name.replace(/(^.+)(_.+)(\.[^\.]+$)/, '$1'), false)
var dupedDoc = app.activeDocument
app.activeDocument = originalDoc
originalDoc.close(SaveOptions.DONOTSAVECHANGES)
app.activeDocument = dupedDoc
if (!sub_dir.exists) sub_dir.create()
Copy link to clipboard
Copied
two notes:
… and welcome in the scripting world.
Copy link to clipboard
Copied
Thanks for the warm welcom pixxxel schubser! Please do keep up the constructive feedback.
I’m a scripting newb, but not exactly a regex newb... Still very far from intermediate or expert though.
Point 1: that is why I used capture groups for the file name pattern supplied by Chris. It is easy to add the original extension back in by adding a $3 to the existing $1 regex replacement. As the save step is currently manual, my tests showed no difference in retaining or stripping the original extension as it is replaced with the manual save. A scripted save would likely need to be different, which is why I asked Chris about the output format being static.
Point 2: the regex should only remove the underscore and characters directly before/up to the period/extension. The first underscore should be retained. I’ll double check the regex again as I may have messed it up when transferring from the regex tester into the script. I didn’t do exhaustive pattern tests though, Chris has been a little lax in that area. :]
EDIT: I just double checked the regular expression, it all looks good to me!
1ABCD_BI00337684_HPK1155.jpg (Chris’s input pattern)
1ABCD_BI00337684_XYZ321_HPK1155.jpg (Another input pattern to illustrate)
1ABCD_BI00337684.jpg (Another input pattern to illustrate)
1ABCD_BI00337684.jpg (Chris’s pattern result)
1ABCD_BI00337684_XYZ321.jpg (Another pattern result to illustrate)
1ABCD.jpg (Another pattern result to illustrate)
Copy link to clipboard
Copied
Hi Chris, it turned out that your code sample was too confusing for me, however it did not take too much tweaking to come up with a usable result from my first draft post #8:
// https://forums.adobe.com/message/10628370
// https://forums.adobe.com/message/10632380#10632380
// https://forums.adobe.com/message/10633605#10633605
// Save renamed JPEG in Processed folder
#target photoshop
var originalDoc = app.activeDocument;
var originalPath = originalDoc.path;
var sub_dir = new Folder(originalPath + '/' + 'Processed');
originalDoc.duplicate (originalDoc.name.replace(/(^.+)(_.+)(\.[^\.]+$)/, '$1.jpg'), true)
var dupedDoc = app.activeDocument;
app.activeDocument = originalDoc
originalDoc.close(SaveOptions.DONOTSAVECHANGES)
app.activeDocument = dupedDoc
if (!sub_dir.exists) sub_dir.create()
var saveOptions = new JPEGSaveOptions();
saveOptions.embedColorProfile = true;
saveOptions.formatOptions = FormatOptions.STANDARDBASELINE;
saveOptions.matte = MatteType.NONE;
saveOptions.quality = 12;
dupedDoc.saveAs( sub_dir, saveOptions, false );
I’m sure that the code can be improved… For example, I can’t work out how to get the try/catch working correctly to check for the active doc being an unsaved file with no path, it always comes up with an error 8103.
I’d also like a try/catch to stop the script from saving/overwriting if the new file would overwrite another existing file of the same name, again I can’t get it to work:
if(dupedDoc.exists) saveAs.remove();
All that being said, I am pretty happy with the result so far!
Copy link to clipboard
Copied
Try
var originalDoc = app.activeDocument;
var originalPath = originalDoc.path;
var sub_dir = new Folder(originalPath + '/' + 'Processed');
if (!sub_dir.exists) sub_dir.create();
if (!sub_dir.exists) { alert("Can not create folder"); throw("Can not create folder"); }
// Your RegExp is in doubt. I did not change
var new_name = originalDoc.name.replace(/(^.+)(_.+)(\.[^\.]+$)/, '$1.jpg');
alert(new_name, "New Name"); // for debugging
var file = new File(sub_dir + '/' + new_name);
if (file.exists) { alert("File already exists"); throw("File already exists"); }
var dupedDoc = originalDoc.duplicate(new_name, true);
originalDoc.close(SaveOptions.DONOTSAVECHANGES);
app.activeDocument = dupedDoc
var saveOptions = new JPEGSaveOptions();
saveOptions.embedColorProfile = true;
saveOptions.formatOptions = FormatOptions.STANDARDBASELINE;
saveOptions.matte = MatteType.NONE;
saveOptions.quality = 12;
dupedDoc.saveAs(file, saveOptions, false);
Copy link to clipboard
Copied
Thank you r-bin I’ll try to work out what is going on with your code additions!
Your code comment:
// Your RegExp is in doubt. I did not change
Could you please elaborate?
In post #2 Chris stated that the filename pattern was: 1ABCD_§BI00337684§_HPK1155.JPG – with the requirement that the final underscore and following text up to the filename extension be removed. My reply #10 confirmed that with further testing this is the result that I am seeing.
So I am obviously not seeing this as both pixxxel schubser and yourself have commented on the regex… So, what am I missing?
EDIT: Now after re-reading Chris’s posts, I am not sure what the exact text pattern is! So Bardioch – can you please confirm the required input and output filename pattern using at least 3 examples of real-world filenames.
Copy link to clipboard
Copied
I do not often use and trust RegExp. But in our case, if the file has the name "1.psd", then the name will not change and will remain "1.psd". In this case, the script in this form will not be able to check the existence of the file "1.jpg", although the file will be saved with this name.
Copy link to clipboard
Copied
Furthermore your Grep is much to greedy:
… Now after re-reading Chris’s posts, I am not sure what the exact text pattern is! So Bardioch – can you please confirm the required input and output filename pattern using at least 3 examples of real-world filenames.
Fullack!
Without knowing the exact text pattern rules - every Grep are in doubt.
Copy link to clipboard
Copied
pixxxel schubser wrote:
Without knowing the exact text pattern rules - every Grep are in doubt.
Indeed…
However all I can do is go by Chris’s original post, which stated:
§BI00337684§_HPK1155.JPG
§AN00550282§_CHJ3942.JPG
§AD00093642§_IMG0023.JPG
It would be so cool and so much work saving if a script can delet everything after the "_" and the "_" itself.
Which is exactly what my regex does! Yes, I could have built the regex to be less flexible and to only look for a set amount of alpha, then a set amount of numeric etc., that is doable and ideally requires feedback from usage which has not been forthcoming.
Then in post #2 Chris corrected the OP:
OH, i made a mistake... my files look like "1ABCD_§BI00337684§_HPK1155.JPG" sorry...
And my original regex continues to work correctly if the highlighted bit in red above is still the same pattern that needs to be removed:
So Chris, can you please provide feedback?
How is the script?
How are the filenames being handled, are they correctly truncated or not?
Copy link to clipboard
Copied
Hey there, sorry for the late response... my daughter was ill and needed all of my attention
First of all thanks to everybody for your great work!! I tested all scripts and they worked perfektly!!
These are some of the files i´m working with:
4AHKE_§AQ00150860§_DSC0517.JPG
5BLFT_§AQ00150923§_DSC0586.JPG
4R_§AO00166906§_RKL7685.JPG
8F_§AO00166921§_RKL7700.JPG
There is only one problem left...
It doesen´t work if i´m opening files out of bridge with a photoshop action (script integrated)
Everytime i tried it photoshop asked me to dublicate the image
If the files are already open and i run the script it works perfektly fine!
Oh, and thank you so much for the awsome explanations!
Copy link to clipboard
Copied
On the filenames, just to be 100%, remove the bits in red?
4AHKE_§AQ00150860§_DSC0517.JPG
5BLFT_§AQ00150923§_DSC0586.JPG
4R_§AO00166906§_RKL7685.JPG
8F_§AO00166921§_RKL7700.JPG
Copy link to clipboard
Copied
There is only one problem left...
It doesen´t work if i´m opening files out of bridge with a photoshop action (script integrated)
Everytime i tried it photoshop asked me to dublicate the image
If the files are already open and i run the script it works perfektly fine!
Bridge was never mentioned. Can you please provide exact step by step instructions to follow with a screenshot of your actions panel with the action steps expanded so that the forum can see what is going on?
P.S. Rather than putting the script into the action, why not put the action into the script?
Copy link to clipboard
Copied
I am guessing that you wish to batch process a group of files from Bridge.
Perhaps somebody with greater experience with scripting and using Bridge as a source can comment on why the duplicate is broken when run from a batch action from Bridge, when it works fine from Photoshop on a single image source or multiple open images.
So either avoid Bridge as a source, or it you must use Bridge as a source – don’t try to run the script from there into Photoshop. You can open all of the images in the batch to open files in Photoshop from Bridge, then have Photoshop batch process the open files with a destination of none, the duplicate and rename will work correctly. The files will remain open for processing and their save path should be to the newly created file and folder. The script will then work as intended. You really don’t wish to open/save/close JPEG files more than you have to.
If I had known all of this from the beginning, I would have suggested the following. You could simply use Bridge’s Batch Rename tool to copy the selected images into a subfolder using a regex based string substitution. The Batch Rename can be saved for later use, however you may need to update the destination folder path. This of course could all be scripted in Bridge, however that is a different and deeper subject.
Copy link to clipboard
Copied
You can most likely do that in an action Save as into the processed folder and then open the saved file in the processed folder and select previous document. Both documents should be open in Photoshop and the original document should be the active document. The action woul need to be turned into a script so you cans control the save name with the logic required. Duplicating the document would not create a backing file without adding a save.