Salir
  • Comunidad global
    • Idioma:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티

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

Explorador ,
Jan 02, 2024 Jan 02, 2024

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

TEMAS
Importar y exportar , Scripts
3.6K
Traducir
Informe
Directrices de la comunidad
Sé amable y respetuoso, muestra títulos de crédito de la fuente de contenido original y busca duplicados antes de publicar. Más información
community guidelines

correct answers 2 respuestas correctas

LEYENDA , Jan 02, 2024 Jan 02, 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")

 

Traducir
LEYENDA , Jan 03, 2024 Jan 03, 2024

I think:

 

var pg = 3;

 

should work - or "3".

 

Traducir
LEYENDA ,
Sep 12, 2024 Sep 12, 2024
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:

 

RobertatIDTasker_1-1726135976072.png

 

Traducir
Informe
Directrices de la comunidad
Sé amable y respetuoso, muestra títulos de crédito de la fuente de contenido original y busca duplicados antes de publicar. Más información
community guidelines
Explorador ,
Sep 12, 2024 Sep 12, 2024

Here it is, with a more simple target adress on the PC…

Traducir
Informe
Directrices de la comunidad
Sé amable y respetuoso, muestra títulos de crédito de la fuente de contenido original y busca duplicados antes de publicar. Más información
community guidelines
Explorador ,
Sep 12, 2024 Sep 12, 2024
//the export pixel width
var targetWidthPixels = 1000;

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="Print:3";

//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("C:\Users\louis\Documents\TEST_MTP\OUT" + doc.name.substring(0,13) + ".jpg"), 
		false
);
Traducir
Informe
Directrices de la comunidad
Sé amable y respetuoso, muestra títulos de crédito de la fuente de contenido original y busca duplicados antes de publicar. Más información
community guidelines
Explorador ,
Sep 12, 2024 Sep 12, 2024

And a screen capture of my pages on Indesign

Traducir
Informe
Directrices de la comunidad
Sé amable y respetuoso, muestra títulos de crédito de la fuente de contenido original y busca duplicados antes de publicar. Más información
community guidelines
LEYENDA ,
Sep 12, 2024 Sep 12, 2024

JavaScript is case sensitive, so it rather should be:

 

ExportFormat.JPG

 

in the exportFile()?

 

https://www.indesignjs.de/extendscriptAPI/indesign-latest/#ExportFormat.html

 

But, it's the same in your original code - that was working on Mac? 

 

Traducir
Informe
Directrices de la comunidad
Sé amable y respetuoso, muestra títulos de crédito de la fuente de contenido original y busca duplicados antes de publicar. Más información
community guidelines
Explorador ,
Sep 12, 2024 Sep 12, 2024

OK thanks, I will try it although it works like that on MacOS 🙂

Traducir
Informe
Directrices de la comunidad
Sé amable y respetuoso, muestra títulos de crédito de la fuente de contenido original y busca duplicados antes de publicar. Más información
community guidelines
Community Expert ,
Sep 12, 2024 Sep 12, 2024

@Stephane27925305ylf8 wrote:

//export the JPEG
doc.exportFile(
		ExportFormat.jpg,
		File("C:\Users\louis\Documents\TEST_MTP\OUT" + doc.name.substring(0,13) + ".jpg"), 
		false
);

 

 

I think you want forward slashes for the file path—try this:

 

 

doc.exportFile(ExportFormat.jpg, File("C:/Users/louis/Documents/TEST_MTP/OUT/" + doc.name.replace(RegExp('()?\.[^\.]+$'), '') + ".jpg"), false);

//returns C:/Users/louis/Documents/TEST_MTP/OUT/MyExportDocument.jpg for a doc named MyExportDocument.indd

 

 

Traducir
Informe
Directrices de la comunidad
Sé amable y respetuoso, muestra títulos de crédito de la fuente de contenido original y busca duplicados antes de publicar. Más información
community guidelines
LEYENDA ,
Sep 12, 2024 Sep 12, 2024

@rob day

 

Why? Windows prefers "\".

 

Traducir
Informe
Directrices de la comunidad
Sé amable y respetuoso, muestra títulos de crédito de la fuente de contenido original y busca duplicados antes de publicar. Más información
community guidelines
Community Expert ,
Sep 12, 2024 Sep 12, 2024

This is what I get  for VS Code and it fails on OSX:

 

 

$.writeln("C:\Users\louis\Documents\TEST_MTP\OUT" + doc.name.replace(RegExp('()?\.[^\.]+$'), '') + ".jpg")

//returns C:UserslouisDocumentsTEST_MTPOUTMyExportDocument.jpg

 

 

Or if I get the ID document’s .filePath the file path string returns as this

 

 $.writeln(doc.filePath)
 //returns ~/~FISHERCAT%20ONGOING/CLOUD%20ONGOING%20/Dropbox/SCRIPTING

 

Traducir
Informe
Directrices de la comunidad
Sé amable y respetuoso, muestra títulos de crédito de la fuente de contenido original y busca duplicados antes de publicar. Más información
community guidelines
LEYENDA ,
Sep 12, 2024 Sep 12, 2024

@rob day

 

So writeln is stripping all "\"??

 

OK, because all "\" need to be doubled.

 

Traducir
Informe
Directrices de la comunidad
Sé amable y respetuoso, muestra títulos de crédito de la fuente de contenido original y busca duplicados antes de publicar. Más información
community guidelines
Community Expert ,
Sep 12, 2024 Sep 12, 2024

I think ExtendScript wants the folder path in URI notation so it is cross platform:

 

https://www.indesignjs.de/extendscriptAPI/indesign-latest/#Folder.html

 

Screen Shot 29.png

Traducir
Informe
Directrices de la comunidad
Sé amable y respetuoso, muestra títulos de crédito de la fuente de contenido original y busca duplicados antes de publicar. Más información
community guidelines
Explorador ,
Sep 13, 2024 Sep 13, 2024

Yes! Thanks very much, it works! 🙂

But… Instead of having the specific page as requested here in the script :

//the active page
var pg="Print:3";

It exports all the pages of the document :-((

 

I make my tests with exactly the same Indd document on Mac and on PC.

Traducir
Informe
Directrices de la comunidad
Sé amable y respetuoso, muestra títulos de crédito de la fuente de contenido original y busca duplicados antes de publicar. Más información
community guidelines
Community Expert ,
Sep 13, 2024 Sep 13, 2024
MÁS RECIENTES

You’ll need to set range rather than all in the jpegExportPreferences by adding jpegExportRange

 

//set the res preference and page range
    with(app.jpegExportPreferences){
        exportResolution = resout;
        jpegExportRange = ExportRangeOrAllPages.EXPORT_RANGE;
        pageString = pg;
        antiAlias = true;
        embedColorProfile = true;
        jpegColorSpace = JpegColorSpaceEnum.RGB;
        //add other properties as needed
    }

 

If that doesn’t work you might need to get the page string:

 

var pg=app.activeWindow.activePage.name;
Traducir
Informe
Directrices de la comunidad
Sé amable y respetuoso, muestra títulos de crédito de la fuente de contenido original y busca duplicados antes de publicar. Más información
community guidelines
Community Expert ,
Sep 12, 2024 Sep 12, 2024

Try running this:

 

var f = Folder.selectDialog("Select a Destination Folder"); 
if(f != null){ 
    alert(f)
}

 

I get:

Screen Shot 28.png

Traducir
Informe
Directrices de la comunidad
Sé amable y respetuoso, muestra títulos de crédito de la fuente de contenido original y busca duplicados antes de publicar. Más información
community guidelines
Community Expert ,
Sep 12, 2024 Sep 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;
}
Traducir
Informe
Directrices de la comunidad
Sé amable y respetuoso, muestra títulos de crédito de la fuente de contenido original y busca duplicados antes de publicar. Más información
community guidelines
Explorador ,
Sep 12, 2024 Sep 12, 2024

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

Traducir
Informe
Directrices de la comunidad
Sé amable y respetuoso, muestra títulos de crédito de la fuente de contenido original y busca duplicados antes de publicar. Más información
community guidelines