Skip to main content
Known Participant
January 2, 2024
Answered

Script to save a file in a specific folder with a specific name

  • January 2, 2024
  • 2 replies
  • 4055 views

Hello,

I found a very useful script to save covers as JPEG, with a fixed width, whatever is the original size :

//the export pixel width
var targetWidthPixels = 600;

var doc = app.activeDocument;
with(doc.viewPreferences){
    horizontalMeasurementUnits = MeasurementUnits.INCHES;
    verticalMeasurementUnits = MeasurementUnits.INCHES;
}

//get the page width in inches
var pw=doc.documentPreferences.pageWidth;

//calulate the resolution needed to export the jpeg as 1500px wide
//if the page is 8.5" x 11", the output pixel dimensions will be 600px x 776px
var resout= targetWidthPixels/pw;

//the active page
var pg=app.activeWindow.activePage.name;

//set the res preference and page range
 with(app.jpegExportPreferences){
     exportResolution = resout;
     pageString = pg;
     antiAlias = true;
     embedColorProfile = true;
     jpegColorSpace = JpegColorSpaceEnum.RGB;
     //add other properties as needed
}
 
//export the JPEG
doc.exportFile(
		ExportFormat.jpg,
		File(Folder.desktop + "/ExportJPG.jpg"), 
		false
);

My question is : how to put this file in a specific folder (/Users/toto/Desktop/TEST MTP/OUT site) with a specific name wich is the 14 first characters of every Indesign file name (9782340-046337_COUV_2022_02_24.indd becomes 9782340-046337.jpg)

 

Thanks for your Help

This topic has been closed for replies.
Correct answer Robert at ID-Tasker

I think:

 

var pg = 3;

 

should work - or "3".

 

2 replies

rob day
Adobe Expert
September 12, 2024

Hi @Stephane27925305ylf8 , you could also choose the destination folder from the script, something like this"

 

//Choose a folder
var f = Folder.selectDialog("Select a Destination Folder"); 
if(f != null){ 
    app.scriptPreferences.measurementUnit = MeasurementUnits.INCHES;

    //the export pixel width
    var targetWidthPixels = 600;

    var doc = app.activeDocument;
    //use the document’s name for export
    var n = doc.name.replace(RegExp('()?\.[^\.]+$'), '');

    //get the page width in inches
    var pw=doc.documentPreferences.pageWidth;

    //calulate the resolution needed to export the jpeg as 1500px wide
    //if the page is 8.5" x 11", the output pixel dimensions will be 600px x 776px
    var resout= targetWidthPixels/pw;

    //the active page
    var pg=app.activeWindow.activePage.name;

    //set the res preference and page range
    with(app.jpegExportPreferences){
        exportResolution = resout;
        pageString = pg;
        antiAlias = true;
        embedColorProfile = true;
        jpegColorSpace = JpegColorSpaceEnum.RGB;
        //add other properties as needed
    }
    //export the JPEG
    doc.exportFile(ExportFormat.jpg, File(f + "/" + n + ".jpeg"), false);
    app.scriptPreferences.measurementUnit = AutoEnum.AUTO_VALUE;
}
Known Participant
September 12, 2024

Thank you Rob, I'll try that (even if my goal is always the same place on the computer)

Robert at ID-Tasker
Brainiac
January 2, 2024

You have to modify this part:

 

File(Folder.desktop + "/ExportJPG.jpg") 

 

"Folder.desktop" would be your "/Users/toto/Desktop/TEST MTP/OUT site"

 

"/ExportJPG.jpg" would be something like that:

 

'/' + doc.name.substring(0,13) + '.jpg'

 

But I'm not JS guy so someone else needs to check and confirm.

 

File("/Users/toto/Desktop/TEST MTP/OUT site/" + doc.name.substring(0,13) + ".jpg")

 

Known Participant
January 3, 2024

Thanks a lot, it works fine.

Can you tell me how to select a specific page in the script.

Probably a change here?:

//the active page
var pg=app.activeWindow.activePage.name;

 The name of the page is "Print" and the number is 3

Robert at ID-Tasker
Brainiac
September 12, 2024

No, it's just that the script does not launch when I play it on the PC. Just Nothing…


quote

No, it's just that the script does not launch when I play it on the PC. Just Nothing…


By @Stephane27925305ylf8

 

Could you please post your latest code again - using: