Save Current File As with addition to filename

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: Kukurykus, Tom, xbytor

James Jellings
Posts: 2
Joined: Wed Aug 17, 2016 1:11 pm

Save Current File As with addition to filename

Post by James Jellings »

Hi, I'm wondering if anyone can help - I need to create a very simple script but I am already struggling - I need to save the active document in the current location, same filetype, with the existing filename, but with an addition e.g. 'C:\currentpath\currrentfile_alternative.currentextention'

Thanks,
James

James Jellings
Posts: 2
Joined: Wed Aug 17, 2016 1:11 pm

Re: Save Current File As with addition to filename

Post by James Jellings »

THought I'd got there with the below, but no luck!



#target photoshop

var doc = app.activeDocument;
var docName = doc.name;
docName = docName.match(/(.*)(\.[^\.]+)/) ? docName = docName.match(/(.*)(\.[^\.]+)/):docName = [docName, docName];
var suffix = '_example';
var saveName = new File(decodeURI(doc.path)+'/'+docName[1]+suffix);
var saveJPEG = File(app.activeDocument, saveName, 10);

User avatar
Kukurykus
Posts: 302
Joined: Mon Jul 25, 2016 12:36 pm

Re: Save Current File As with addition to filename

Post by Kukurykus »

Tips:
  1. don't use app. or there is really need as SOMETHING without app, so app.SOMETHING wouldn't work. As far as only SOMETHING == app.SOMETHING gives the result use the simplest form (or you have your reason to do it other way)
  2. it's alike variable (var). Maybe visually it's easier for beggining to use it (or loop some function code in source form for searching for variables), but after all you need it only untill some variable is undefined before it finally (or not) get same value during running the code
  3. you may also use one shorter line instead of two like "docName = activeDocument.name", or don't use even one line untill you won't recall the same "element", so value set to variable, however in early stage maybe it can be more readable for 3rd person coders
I assume you saved your file before another saving with changed name:

Code: Select all

jpg = new JPEGSaveOptions, jpg.quality = 10;
(aD = activeDocument).saveAs(File((aD.path) + '/' + aD.name.replace(/(\.\w+$)/, '_alternative$1')), jpg)
if not then use modified code for just created, but not saved yet document:

Code: Select all

$.level = 0, jpg = new JPEGSaveOptions, jpg.quality = 10;
try{(aD = activeDocument).saveAs(File((aD.path) + '/' + aD.name.replace(/(\.\w+$)/, '_alternative$1')), jpg)}
catch (err) {alert('Save your file before checking its path!')}
Attachments
File Saving.zip
(453 Bytes) Downloaded 204 times

MIRYAMM
Posts: 7
Joined: Thu Sep 15, 2016 6:15 pm

Re: Save Current File As with addition to filename

Post by MIRYAMM »

Thanks Kukurykus

This script is what I needed to add another.

Thanks a million.

PD
Thank God. I could find the Forum.
It was, missing.
Why? It has been so long out of network

User avatar
Kukurykus
Posts: 302
Joined: Mon Jul 25, 2016 12:36 pm

Re: Save Current File As with addition to filename

Post by Kukurykus »

I'm glad it helped. I remember when such short codes were valuable for me, but now are just some regular ones I use in programs quite often.

MIRYAMM
Posts: 7
Joined: Thu Sep 15, 2016 6:15 pm

Re: Save Current File As with addition to filename

Post by MIRYAMM »

;)

User avatar
DavideBarranca
Posts: 16
Joined: Tue Jul 26, 2016 2:12 pm

Re: Save Current File As with addition to filename

Post by DavideBarranca »

Actually I wouldn't suggest (especially to beginners) to skip "var" altogether, see here. Just my own point of view :-)
Cheers,

Davide

User avatar
Kukurykus
Posts: 302
Joined: Mon Jul 25, 2016 12:36 pm

Re: Save Current File As with addition to filename

Post by Kukurykus »

Now I understand why some variables in / out functions could spoil my fun :) But I thought problem of local / global variables was solved in later versions of JavaScript. Later I'll examine it for sure...

User avatar
DavideBarranca
Posts: 16
Joined: Tue Jul 26, 2016 2:12 pm

Re: Save Current File As with addition to filename

Post by DavideBarranca »

Well, ES6 introduces "let" as a locally scoped "var", but remember that when it comes to ExtendScript we're stuck to EcmaScript version 3, i.e. pre-2009.
We're... vintage :P

Davide