Annoying JScript column script error

Using a column script (JScript) to display aspect ratio and occasionally I get this error appearing
image
so I put in some code that I thought would stop it doing this

  if ( isNaN(c.metadata.image.aspectratio) || (c.metadata.image.aspectratio == null) )
  {
  	ar = 0;
  }
  else
  {
  	ar = c.metadata.image.aspectratio;
  }

but am still getting the occasional same error.
Is the above code wrong ?
here is the complete code

// Version 2.1
// Aspect column (as different from built in decimal Aspect Ratio column)
// Aspect ratios apply to image, display and print sizes
// Orientation (portrait, landscape) is used for aspect ratios that are not industry standard
// 
// https://resource.dopus.com/t/column-for-showing-orientation-of-image-using-words/41889
//
// some useful sites
// https://en.wikipedia.org/wiki/Display_aspect_ratio
// https://en.wikipedia.org/wiki/Graphics_display_resolution#2560_%C3%97_1440_(QHD)
// https://en.wikipedia.org/wiki/Aspect_ratio_(image)
// https://papersizes.io/us/
// https://www.blake-envelopes.com/media/wysiwyg/downloads/ISO-paper-sizes.pdf
// 
//
// The OnInit function is called by DO to initialize the script add-in
// https://www.gpsoft.com.au/help/opus12/index.html#!Documents/Scripting/OnInit.htm
/*
can put your columns into the main categories now,
Don't think it's fully documented yet,
but with script columns it's set via ScriptColumn.category,
which takes one of these string values:

"loc"
"date"
"size"
"std"
"dims"
"image"
"music"
"prog"
"doc"
"movie"
"sums"
"script"
"shell"
"other"
"eval"
*/
function OnInit(w) {
  w.name = 'Aspect';			// display name
  w.group = 'Columns';			// group name shown in the scripts dialog
  w.version = '2.1';
  w.desc = 'Column to show orientation of image';
  w.url = 'https://resource.dopus.com/t/column-for-showing-orientation-of-image-using-words/41889';
  w.copyright = '(c)2022';
  w.default_enable = true;
  w.min_version = '12.28';
}


// Initialize OnAddColumns event by creating a new ScriptColumn object y and initialize it to add the column to DO
// https://www.gpsoft.com.au/help/opus12/index.html#!Documents/Scripting/OnAddColumns.htm
function OnAddColumns(z) {
  var y = z.AddColumn();
  y.name = 'aspectzxy';		// internal keyword of the column.
  y.label = '- Aspect';		// name for the column, displayed in the menus
  y.header = '- Aspect -';	// name for the column header. If not set, the label value will be used
  y.category = 'dims';		// undocumented. defines the category sub-menu as shown when right-clicking the column header. (see above)
  y.justify = 'center';
  y.type = 'wordsort';
  y.autogroup = true;
  y.autorefresh = true;
  y.method = 'OnAspect';
}


// AddColumn.method function passed a ScriptColumnData object x
// https://www.gpsoft.com.au/help/opus12/index.html#!Documents/Scripting/Item.htm
function OnAspect(x) {
  var c = x.item;
  if (c.metadata != 'image') return;

  // else must be an image
  // see https://en.wikipedia.org/wiki/Aspect_ratio_(image)
  // ~cinemaatic, tv, monitor, wide monitor, ultra wide monitor, letter, A, B, postbox ????
  
  var ar = 0;

  if ( isNaN(c.metadata.image.aspectratio) || (c.metadata.image.aspectratio == null) )
  {
  	ar = 0;
  }
  else
  {
  	ar = c.metadata.image.aspectratio;
  }
  

  if ((ar > 0.94 && ar < 1.0) || (ar > 1.0 && ar < 1.22)) x.value = "Squarish";
    else if (ar == 1.0) x.value = "Square";
      else if (ar < 1.0) {
		// PORTRAIT ORIENTATED ASPECT RATIOS
		x.value = "Portrait";
		if (ar > 0.79 && ar < 0.81) x.value = "4:5";						// old monitor
		if (ar > 0.76 && ar < 0.78) x.value = "Letter (P)";					// US paper size - Letter, ANSI A,C,E
		if (ar > 0.75 && ar < 0.77) x.value = "Government Letter (P)";		// US paper size - Government Letter
		if (ar > 0.74 && ar < 0.76) x.value = "3:4";						// old TV, Arch A,C,E
		if (ar > 0.70 && ar < 0.72) x.value = "A4 Portrait";				// A,B,C series, Arch E1
		if (ar > 0.68 && ar < 0.70) x.value = "Arch E3 (P)";				// Arch E3
		if (ar > 0.67 && ar < 0.69) x.value = "Arch E2 (P)";				// Arch E2
		if (ar > 0.65 && ar < 0.67) x.value = "2:3";						// portable PC's, Arch B,D
		if (ar > 0.64 && ar < 0.66) x.value = "Government Legal (P)";		// US paper size - Government Legal
		if (ar > 0.63 && ar < 0.65) x.value = "Tabloid (P)";				// US paper size - Tabloid, Ledger, Half-Letter, ANSI B,D						
		if (ar > 0.61 && ar < 0.63) x.value = "10:16";						// computer monitor, US paper size Junior Legal
		if (ar > 0.59 && ar < 0.61) x.value = "Legal (P)";					// US paper size - Legal
		if (ar > 0.55 && ar < 0.57) x.value = "9:16";						// Widescreen
		if (ar > 0.41 && ar < 0.43) x.value = "9:21";						// Ultrawide
		if (ar > 0.27 && ar < 0.29) x.value = "9:32";						// Superwide
	}
        else {
			// LANDSCAPE ORIENTATED ASPECT RATIOS
			x.value = "Landscape";
			if (ar > 1.24 && ar < 1.26) x.value = "5:4";						// old monitor
			if (ar > 1.28 && ar < 1.30) x.value = "Letter (L)";					// US paper size - Letter, ANSI A,C,E
			if (ar > 1.30 && ar < 1.32) x.value = "Government Letter (L)";		// US paper size - Government Letter
			if (ar > 1.32 && ar < 1.34) x.value = "4:3";						// old TV, Arch A,C,E
			if (ar > 1.39 && ar < 1.41) x.value = "Arch E1 (L)";				// Arch E1
			if (ar > 1.40 && ar < 1.42) x.value = "A4 Landscape";				// A,B,C series
			if (ar > 1.43 && ar < 1.45) x.value = "Arch E3 (L)";				// Arch E3
			if (ar > 1.45 && ar < 1.47) x.value = "Arch E2 (L)";				// Arch E2
			if (ar > 1.49 && ar < 1.51) x.value = "3:2";						// portable PC's, Arch B,D
			if (ar > 1.51 && ar < 1.53) x.value = "Government Legal (L)";		// US paper size - Government Legal
			if (ar > 1.53 && ar < 1.55) x.value = "Tabloid (L)";				// US paper size - Tabloid, Ledger, Half-Letter, ANSI B,D						
			if (ar > 1.59 && ar < 1.61) x.value = "16:10";						// computer monitor, US paper size Junior Legal
			if (ar > 1.63 && ar < 1.65) x.value = "Legal (L)";					// US paper size - Legal
			if (ar > 1.76 && ar < 1.78) x.value = "16:9";						// Widescreen
			if (ar > 2.32 && ar < 2.34) x.value = "21:9";						// Ultrawide
			if (ar > 3.54 && ar < 3.56) x.value = "32:9";						// Superwide
		}
}

Try swapping the two conditions in the if statement.

1 Like

Learnt try ... throw ... catch ... finally instead, supported by JScript 5.

for anyone trying to use the above code use this more robust version instead

// Version 2.2
// Aspect column (as different from built in decimal Aspect Ratio column)
// Aspect ratios apply to image, display and print sizes
// Orientation (portrait, landscape) is used for aspect ratios that are not industry standard
// 
// https://resource.dopus.com/t/column-for-showing-orientation-of-image-using-words/41889
//
// some useful sites
// https://en.wikipedia.org/wiki/Display_aspect_ratio
// https://en.wikipedia.org/wiki/Graphics_display_resolution#2560_%C3%97_1440_(QHD)
// https://en.wikipedia.org/wiki/Aspect_ratio_(image)
// https://papersizes.io/us/
// https://www.blake-envelopes.com/media/wysiwyg/downloads/ISO-paper-sizes.pdf
// 
//
// The OnInit function is called by DO to initialize the script add-in
// https://www.gpsoft.com.au/help/opus12/index.html#!Documents/Scripting/OnInit.htm
/*
can put your columns into the main categories now,
Don't think it's fully documented yet,
but with script columns it's set via ScriptColumn.category,
which takes one of these string values:

"loc"
"date"
"size"
"std"
"dims"
"image"
"music"
"prog"
"doc"
"movie"
"sums"
"script"
"shell"
"other"
"eval"
*/
function OnInit(w) {
  w.name = 'Aspect';			// display name
  w.group = 'Columns';			// group name shown in the scripts dialog
  w.version = '2.1';
  w.desc = 'Column to show orientation of image';
  w.url = 'https://resource.dopus.com/t/column-for-showing-orientation-of-image-using-words/41889';
  w.copyright = '(c)2022';
  w.default_enable = true;
  w.min_version = '12.28';
}


// Initialize OnAddColumns event by creating a new ScriptColumn object y and initialize it to add the column to DO
// https://www.gpsoft.com.au/help/opus12/index.html#!Documents/Scripting/OnAddColumns.htm
function OnAddColumns(z) {
  var y = z.AddColumn();
  y.name = 'aspectzxy';		// internal keyword of the column.
  y.label = '- Aspect';		// name for the column, displayed in the menus
  y.header = '- Aspect -';	// name for the column header. If not set, the label value will be used
  y.category = 'dims';		// undocumented. defines the category sub-menu as shown when right-clicking the column header. (see above)
  y.justify = 'center';
  y.type = 'wordsort';
  y.autogroup = true;
  y.autorefresh = true;
  y.method = 'OnAspect';
}


// AddColumn.method function passed a ScriptColumnData object x
// https://www.gpsoft.com.au/help/opus12/index.html#!Documents/Scripting/Item.htm
function OnAspect(x)
{
	var c = x.item;
	if (c.metadata != 'image') return;
	
	// else must be an image
	// see https://en.wikipedia.org/wiki/Aspect_ratio_(image)
	// ~cinematic, tv, monitor, wide monitor, ultra wide monitor, letter, A, B, postbox ????
	
	var ar = 0;



	try
	{
		if ( c.metadata.image.aspectratio == null ) throw "a null error";
		if ( isNaN(c.metadata.image.aspectratio) ) throw "a NaN error";
	}
	catch (error)
	{
		ar = 0;
		DOpus.Output('aspect error: ' + error);
	}
	finally
	{
		ar = c.metadata.image.aspectratio;
	}



	if ((ar > 0.94 && ar < 1.0) || (ar > 1.0 && ar < 1.22)) x.value = "Squarish";
	else if (ar == 1.0) x.value = "Square";
	else if (ar == 0) x.value = "---";
	else if (ar < 1.0)
	{
		// PORTRAIT ORIENTATED ASPECT RATIOS
		x.value = "Portrait";
		if (ar > 0.79 && ar < 0.81) x.value = "4:5";						// old monitor
		if (ar > 0.76 && ar < 0.78) x.value = "Letter (P)";					// US paper size - Letter, ANSI A,C,E
		if (ar > 0.75 && ar < 0.77) x.value = "Government Letter (P)";		// US paper size - Government Letter
		if (ar > 0.74 && ar < 0.76) x.value = "3:4";						// old TV, Arch A,C,E
		if (ar > 0.70 && ar < 0.72) x.value = "A4 Portrait";				// A,B,C series, Arch E1
		if (ar > 0.68 && ar < 0.70) x.value = "Arch E3 (P)";				// Arch E3
		if (ar > 0.67 && ar < 0.69) x.value = "Arch E2 (P)";				// Arch E2
		if (ar > 0.65 && ar < 0.67) x.value = "2:3";						// portable PC's, Arch B,D
		if (ar > 0.64 && ar < 0.66) x.value = "Government Legal (P)";		// US paper size - Government Legal
		if (ar > 0.63 && ar < 0.65) x.value = "Tabloid (P)";				// US paper size - Tabloid, Ledger, Half-Letter, ANSI B,D						
		if (ar > 0.61 && ar < 0.63) x.value = "10:16";						// computer monitor, US paper size Junior Legal
		if (ar > 0.59 && ar < 0.61) x.value = "Legal (P)";					// US paper size - Legal
		if (ar > 0.55 && ar < 0.57) x.value = "9:16";						// Widescreen
		if (ar > 0.41 && ar < 0.43) x.value = "9:21";						// Ultrawide
		if (ar > 0.27 && ar < 0.29) x.value = "9:32";						// Superwide
	}
	else if (ar > 1.0)
	{
		// LANDSCAPE ORIENTATED ASPECT RATIOS
		x.value = "Landscape";
		if (ar > 1.24 && ar < 1.26) x.value = "5:4";						// old monitor
		if (ar > 1.28 && ar < 1.30) x.value = "Letter (L)";					// US paper size - Letter, ANSI A,C,E
		if (ar > 1.30 && ar < 1.32) x.value = "Government Letter (L)";		// US paper size - Government Letter
		if (ar > 1.32 && ar < 1.34) x.value = "4:3";						// old TV, Arch A,C,E
		if (ar > 1.39 && ar < 1.41) x.value = "Arch E1 (L)";				// Arch E1
		if (ar > 1.40 && ar < 1.42) x.value = "A4 Landscape";				// A,B,C series
		if (ar > 1.43 && ar < 1.45) x.value = "Arch E3 (L)";				// Arch E3
		if (ar > 1.45 && ar < 1.47) x.value = "Arch E2 (L)";				// Arch E2
		if (ar > 1.49 && ar < 1.51) x.value = "3:2";						// portable PC's, Arch B,D
		if (ar > 1.51 && ar < 1.53) x.value = "Government Legal (L)";		// US paper size - Government Legal
		if (ar > 1.53 && ar < 1.55) x.value = "Tabloid (L)";				// US paper size - Tabloid, Ledger, Half-Letter, ANSI B,D						
		if (ar > 1.59 && ar < 1.61) x.value = "16:10";						// computer monitor, US paper size Junior Legal
		if (ar > 1.63 && ar < 1.65) x.value = "Legal (L)";					// US paper size - Legal
		if (ar > 1.76 && ar < 1.78) x.value = "16:9";						// Widescreen
		if (ar > 2.32 && ar < 2.34) x.value = "21:9";						// Ultrawide
		if (ar > 3.54 && ar < 3.56) x.value = "32:9";						// Superwide
	}
}