How to check if Media exists?

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);
}

Hello Sebastian,

Welcome to our OL Forum community!

To check whether a media exist or not you could apply something like this:

if (typeof merge.template.media["mediaName"] === "undefined") {
	// ...
}

Or:

var mediaArray = Object.keys(merge.template.media);

if (!mediaArray.includes("mediaName")) {
	// ...
}

Hello, thank you for welcoming me!

I could not get the “typeof merge.templ…” to work, but the mediaArray code works as intended!

Thanks a lot for quick answer, this will be very helpful in our templates.

1 Like

Out of curiousity, can you let us know please what you condition looked like? Because I would expect that this should work.

Hello,

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:

results.attr("content", mediapaper);

I’m using meta[name^='media'] as selector.

If i don’t use the results.attr("content", mediapaper); then I don’t get the media as background.

I’m a newbie so I’m not sure if this is a good way of doing it, but it works perfectly for me :slight_smile:

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;
}

Tip: See sheetConfig - OL Connect 2024.2 Help for more information about the .sheetConfig object.

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;
}

Yes, there actually is. You may want replace the code inside the else-block with the following:

var printSections = merge.template.contexts.PRINT.sections;

for (var i = 0; i < printSections.length; i++) {
	var mediaName = (printSections[i].name === "FrontPage" ? "PreprintedPaperA4" : media);
	
	printSections[i].sheetConfig.positions.all.media = mediaName;
}

Thanks, seems to work perfectly!

1 Like