I’ve been succesful in checking if Images and PDF’s exists with the resource() function, but, I want to do the same for Media-selection.
I have several Medias defined in the Media-folder in the PresConnect template (one of them is 1860586).
I have a standard script that selects the correct media based a field from the datamapper. In below example I have simplified it by hardcoding the Media-name, but it says the Media doesn’t exist even though it actually does.
I want the template to give fatal error if we have a Media that is missing in our template.
Does anyone know how to achieve this?
var pdfmedia = “Media/1860586”;
if (!resource(pdfmedia)) {
fatalError(“PDF (” + pdfmedia + ") is missing for record: " + record.rank_in_job);
}
I’m trying it as simple as possible, I have 4 Medias in my Media-folder, where one of them is named “1860585”. But, this function always goes into the fatalError line as if the media is undefined, but it is not. I can see in the preview that the correct media is loaded (1860585).
I might have misunderstood how this function is supposed to work.
My condition is as below:
var mediapaper = "1860585";
results.attr("content", mediapaper);
if (typeof merge.template.media[mediapaper] === "undefined") {
fatalError("No mediapaper found: " + mediapaper);
}
I’m able to reproduce this behaviour. However, I am not able to reproduce this behaviour when I change the name of the media from 1860585 to m1860585 (as in: when I make sure that it starts with a letter rather than a number) and apply the following code:
var mediaName = "m1860585";
if (typeof merge.template.media[mediaName] === "undefined") {
// ...
}
By the way, what are you using as for the selector of your standard script? The reason why I am wondering is because I see that you have also applied the following:
Thank you for sharing all the information requested so far.
It is fine to use meta[name^='media'] as selector but I would recommend to use a Control Script instead of a Standard Script for this and to apply something similar to the below to this new Control Script:
var mediaName = "m1860585";
if (typeof merge.template.media[mediaName] === "undefined") {
fatalError("No media with name '" + mediaName + "' found.");
} else {
var section = merge.template.contexts.PRINT.sections["Section 1"];
section.sheetConfig.positions.all.media = mediaName;
}
Thanks for the code example, unfortunately I can’t use the “typeof” if it doesn’t support only numeric characters, because I have thousands of articles that are numeric, and the media selection is based on the article number of the paper.
I moved the code to my control-script, and it works, but, I don’t like the idea of having to define every print-section I have in my template. Is there someway of applying this media to all print-sections instead of defining them all in the control script? See below example, I want to apply the media selection to all sections except for the FrontPage section which should always use the media PreprintedPaperA4.
var mediaArray = Object.keys(merge.template.media);
var media = "";
switch (record.printflow) {
case 'PRI':
case 'TPS':
media = record.article_number_carrier;
break;
case 'PER':
media = "PreprintedPaperA4";
break
}
if (!mediaArray.includes(media)) {
fatalError("No media found: " + media);
} else {
var section = "";
section = merge.template.contexts.PRINT.sections["FrontPage"];
section.sheetConfig.positions.all.media = "PreprintedPaperA4";
section = merge.template.contexts.PRINT.sections["snippet"];
section.sheetConfig.positions.all.media = media;
section = merge.template.contexts.PRINT.sections["pdf"];
section.sheetConfig.positions.all.media = media;
section = merge.template.contexts.PRINT.sections["pdf_from_disk"];
section.sheetConfig.positions.all.media = media;
section = merge.template.contexts.PRINT.sections["TPS"];
section.sheetConfig.positions.all.media = media;
}