Adding a suffix to a filename in an action

Anyone, especially newbies, asking for help with Photoshop Scripting and Photoshop Automation - as opposed to those contributing to discussion about an aspect of Photoshop Scripting

Moderators: Tom, Kukurykus

promotype
Posts: 5
Joined: Mon Oct 17, 2022 7:56 pm

Adding a suffix to a filename in an action

Post by promotype »

Hi guys,

I made this simple action for quickly saving a low-res preview of very large billboard designs, for client approval:
It Duplicates the source (layered psd) to a new merged document, resizes it to max 2000x2000 and saves it to my D drive with Save for Web.

Its almost perfect, except for the filename: The duplicate step in the action makes me end up with "Filename Copy.jpg"
I'd ideally want the " Copy" part stripped and "_LowRes_Preview" suffix added.

Ive found some scripts that help me partially, but the fact that its an unsaved document and both stripping and adding to the filename needs to be done with save for web, make it a bit too advanced for my level :)

Anybody that could help a hand? Much appreciated!
User avatar
Kukurykus
Posts: 506
Joined: Mon Jul 25, 2016 12:36 pm

Re: Adding a suffix to a filename in an action

Post by Kukurykus »

Is the saved document, so the original you use to make a duplicate from, located in the same folder as your webforweb output? If so the last step of your action can be used for script that is going to rename exported file to what you want. The base is the relation of original path to resultant.
promotype
Posts: 5
Joined: Mon Oct 17, 2022 7:56 pm

Re: Adding a suffix to a filename in an action

Post by promotype »

Hi,
No, the original document is in a client folder, and the preview jpg will be saved in the root of my D drive...
User avatar
Kukurykus
Posts: 506
Joined: Mon Jul 25, 2016 12:36 pm

Re: Adding a suffix to a filename in an action

Post by Kukurykus »

I guess the SaveForWeb action step is automatic and uses always same path? If so paste this path, so the extra script step is going to rename it.
promotype
Posts: 5
Joined: Mon Oct 17, 2022 7:56 pm

Re: Adding a suffix to a filename in an action

Post by promotype »

Yes the SaveForWeb step is automatic and will always use the same output path: /d/ (the root of my D drive, D:/)
Last edited by promotype on Tue Oct 18, 2022 10:18 am, edited 1 time in total.
User avatar
Kukurykus
Posts: 506
Joined: Mon Jul 25, 2016 12:36 pm

Re: Adding a suffix to a filename in an action

Post by Kukurykus »

Code: Select all

with(File('/d/' + activeDocument.name + '.jpg'))
	rename(decodeURI(name).split(' copy')
	.join('_LowRes_Preview'))
If you are on Windows, save the above code to 'Presets / Scripts' of your Photoshop folder, as LstStp.jsx. (Re)launch app and from your Actions contextual dropdown menu choose 'Select Menu Item' and click 'File > Scripts > LstStp' to add it as last step of you action, after SaveForWeb item.

Ps. try snippet singly after creating new document, duplicating it to 'Untitled-1 copy', and saving to appropriate 'Untitled-1 copy.jpg' file on disk D.
promotype
Posts: 5
Joined: Mon Oct 17, 2022 7:56 pm

Re: Adding a suffix to a filename in an action

Post by promotype »

Thanks for the script Kukurykus, however its not working for me.
It might have something to do with the fact that photoshop creates a duplicate as 'Filename copy' in Photoshop but when saved, the jpg replaces the space with a hyphen 'Filename-copy.jpg'?

However with that logic, if I remove just the space in the script, it should rename the file to Filename-LowRes_Preview.jpg?
I tried that and opened the file 'Filename-copy.jpg' on the D drive, ran your script without the action, and it didn't get renamed either..

Ill include my original action, just in case it's of any help to you.

Preview-action.zip
(909 Bytes) Downloaded 25 times
Last edited by promotype on Tue Oct 18, 2022 12:25 pm, edited 1 time in total.
User avatar
Kukurykus
Posts: 506
Joined: Mon Jul 25, 2016 12:36 pm

Re: Adding a suffix to a filename in an action

Post by Kukurykus »

Thanks to action I could realise what I know but forgot as I never need to use SaveForWeb. It adapts spaces to URL's by changing space to hyphen:

Code: Select all

String.prototype.rplc = function(v1, v2){return this.split(v1).join(v2)}
with(File('/d/' + activeDocument.name.rplc(' ', '-') + '.jpg'))
	rename(name.rplc('-copy', '_LowRes_Preview'))
promotype
Posts: 5
Joined: Mon Oct 17, 2022 7:56 pm

Re: Adding a suffix to a filename in an action

Post by promotype »

Yesss perfect, that worked exactly as I wanted!!
This will save me a lot of time every day for years to come :P

Thanks 1000x!