I'm having trouble saving the jpg file [via script]

Engaged ,
Sep 20, 2022 Sep 20, 2022

Copy link to clipboard

Copied

Greetings to all
I know I have a lot of discussions, but I know you are very experienced and knowledgeable people, and I sometimes have problems when working


I use this code to save the file as a jpg image with the content of the text layer name

 

var doc = app.activeDocument;
var docpath = doc.path;
var activeLayer = doc.activeLayer;
var textItemRef = app.activeDocument.activeLayer.textItem;
    textContents = textItemRef.contents;
    

var saveFile = File(docpath + "/" + textContents + ".jpg");
  saveJPG(saveFile, 10)
function saveJPG(saveFile, jpegQuality) {
  var jpgSaveOptions = new JPEGSaveOptions();
  jpgSaveOptions.embedColorProfile = true;
  jpgSaveOptions.formatOptions = FormatOptions.STANDARDBASELINE;
  jpgSaveOptions.matte = MatteType.NONE;
  jpgSaveOptions.quality = jpegQuality;
  activeDocument.saveAs(saveFile, jpgSaveOptions, true, Extension.LOWERCASE);
};

 


But I have a problem when repeating the name, especially if it is the name of a person who works in more than one sector
This code saves the old file
I want to add this code only that does a check before saving if the file was previously saved .. and if previously saved the new file is registered, but by adding a space and not a number so that the numbers are not understood that the person is associated with a number of places

I just want to save the new file with the same name but with a space added, and this process is repeated if the name exists more than once

TOPICS
Actions and scripting , SDK

Views

108

Likes

Translate

Translate

Report

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

Community Expert , Sep 23, 2022 Sep 23, 2022

Okay, try this script. it should keep adding a space.

var doc = app.activeDocument;
var docpath = doc.path;
var activeLayer = doc.activeLayer;
var textItemRef = app.activeDocument.activeLayer.textItem;
    textContents = textItemRef.contents;
    
var saveFile = File(docpath + "/" + textContents + ".jpg");

while(saveFile.exists){
    textContents = textContents + ' ';    
    saveFile = File(docpath + "/" + textContents + ".jpg")
    };

  saveJPG(saveFile, 10);
function saveJPG(saveFile, jpegQ
...

Likes

Translate

Translate
Community Expert ,
Sep 20, 2022 Sep 20, 2022

Copy link to clipboard

Copied

I'm not 100% sure on what you want, but it sounds like you need to do a check to make sure a similar file doesn't exist. You can add code that takes the file name that you plan to use and do a simple check:

var newfjle = new File (add path and file name)
alert(newFile.exists)

If you get a true from the alert or an if statement, then you can do a get files command and put them in an array, and loop to see what you get. You would then need to split the content of the file name if you want to add a sequence number or letter.

I'm not on my computer, so it's hard to describe what I mean.

Likes

Translate

Translate

Report

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
Engaged ,
Sep 21, 2022 Sep 21, 2022

Copy link to clipboard

Copied

To illustrate it
For example, I am working on a file in the name of a person, for example: Muhammad Hamid, and he works in more than one department within the company
So the code registers a single file in the name of Mohamed Hamid
But I want when registering a file more than once for the same name, to add a space between the file name and the file extension
I hope I explained what was required

Likes

Translate

Translate

Report

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
Engaged ,
Sep 22, 2022 Sep 22, 2022

Copy link to clipboard

Copied

Does the topic require more clarification than this?
I searched a lot about saving the file and found most of the code adding a number to the file name.. but increasing the file number for me may cause a problem when sending the file for printing. These numbers will be understood as the copy number
This code is very excellent in saving the file, but I only want to add a space when saving again in case the same name is repeated or the same file exists previously

Likes

Translate

Translate

Report

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 ,
Sep 22, 2022 Sep 22, 2022

Copy link to clipboard

Copied

Sorry it took so long to respond. I don't use my computer much.

I'm not sure if I got this right, but I just modified your script to add an if statement to see if that file name was used before. If it was used, then a space is added at the end of the file name. I'm not sure how this will for with multiple instances of a name, but I assume it will keep adding spaces.

var doc = app.activeDocument;
var docpath = doc.path;
var activeLayer = doc.activeLayer;
var textItemRef = app.activeDocument.activeLayer.textItem;
    textContents = textItemRef.contents;
    
var saveFile = File(docpath + "/" + textContents + ".jpg");

if(saveFile.exists){saveFile = File(docpath + "/" + textContents + " .jpg")}//note space between beginning quote and period

  saveJPG(saveFile, 10)
function saveJPG(saveFile, jpegQuality) {
  var jpgSaveOptions = new JPEGSaveOptions();
  jpgSaveOptions.embedColorProfile = true;
  jpgSaveOptions.formatOptions = FormatOptions.STANDARDBASELINE;
  jpgSaveOptions.matte = MatteType.NONE;
  jpgSaveOptions.quality = jpegQuality;
  activeDocument.saveAs(saveFile, jpgSaveOptions, true, Extension.LOWERCASE);
};

Likes

Translate

Translate

Report

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
Engaged ,
Sep 22, 2022 Sep 22, 2022

Copy link to clipboard

Copied

@Chuck Uebele 

Thank you very much for your interest and cooperation
But I tried the code, it only saves two files.. it saves a copy and then another copy followed by a space, but when the name is repeated more than once it only records two files.

- If space is not acceptable for the code (is it possible to put a code instead of putting numbers for the files)

Code resultCode result

Likes

Translate

Translate

Report

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 ,
Sep 23, 2022 Sep 23, 2022

Copy link to clipboard

Copied

Okay, try this script. it should keep adding a space.

var doc = app.activeDocument;
var docpath = doc.path;
var activeLayer = doc.activeLayer;
var textItemRef = app.activeDocument.activeLayer.textItem;
    textContents = textItemRef.contents;
    
var saveFile = File(docpath + "/" + textContents + ".jpg");

while(saveFile.exists){
    textContents = textContents + ' ';    
    saveFile = File(docpath + "/" + textContents + ".jpg")
    };

  saveJPG(saveFile, 10);
function saveJPG(saveFile, jpegQuality) {
  var jpgSaveOptions = new JPEGSaveOptions();
  jpgSaveOptions.embedColorProfile = true;
  jpgSaveOptions.formatOptions = FormatOptions.STANDARDBASELINE;
  jpgSaveOptions.matte = MatteType.NONE;
  jpgSaveOptions.quality = jpegQuality;
  activeDocument.saveAs(saveFile, jpgSaveOptions, true, Extension.LOWERCASE);
};

Likes

Translate

Translate

Report

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
Engaged ,
Sep 23, 2022 Sep 23, 2022

Copy link to clipboard

Copied

LATEST

@Chuck Uebele 

Thank you
Great job and hard work..sorry I bothered you so much..but you are good at spotting problems and working on them.

Likes

Translate

Translate

Report

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