Home User Forums SongKong Music Tagger Jaikoz Music Tagger Albunack Music Service

Wednesday 1 August 2012

Introducing functions in rename masks

An important aspect of tagging your songs is renaming your songs so that they match the metadata within the song. Although most music players depend only on the metadata within the song itself, we all have different preference of how to name our files and this helps us to keep them organized. Some applications require the filename to be in a particular format.

Jaikoz used to have quite a simple language for the rename mask that allowed most metadata to be used in the filename but only provided simple if statements. Then this was replaced with support for full Javascript syntax, this allows you to do pretty much anything but creating the correct mask could be long winded and error prone.

Consider this simple mask
 (album.length>0 ? album + '-':'') + (trackno.length>0 ? trackno + '-':'') 
 + (title.length>0 ? title:'')

Although its fairly clear what it is doing. its very fiddly and easy to make a mistake when editing. Unfortunately as a number of you have commented the Javascript is not as easy to understand as the old syntax, I traded this for the power the Javascript notation gives us.

However recently discovered there is nothing to stop you defining functions within your filename mask so the above can be replaced by

 function ifnotempty(value,sep){
     return value != undefined && value.length > 0 ? value + sep : '';
 }
 ifnotempty(album,'-')+ ifnotempty(trackno,'-') + ifnotempty(title,'') 

As you can now see the complexity is put into the function once, then the resultant mask can be much more concise.

In Jaikoz 3.6 we have made use of functions to provide better default masks. Of course if you are upgrading Jaikoz it will keep your existing mask so you will not see them so I have posted them below.

Folder Rename Mask

 function ifnotempty(value,sep){
     return value.length > 0 ? value + sep : '';
 }
 
 function ifnotempty2(value1,value2,sep){
     return value1.length > 0 ? value1 + sep :value2.length > 0 ? value2 + sep:'' ;
 }
 
 ifnotempty2(albumartist,artist,folderseparator) + ifnotempty(album,folderseparator)
 + (disctotal>1 ? discno  + folderseperator : '') 

Filename Rename Mask

function pad(number, length) {
      var str = '' + number; 
      while (str.length < length) {  
          str = '0' + str;
      }
      return str;
  }
 
 function ifnotempty(value,sep){
     return value.length > 0 ? value + sep : '';
 }
 
 function ifnotempty2(value1,value2,sep){
     return value1.length > 0 ? value1 + sep :value2.length > 0 ? value2 + sep:'' ;
 }
 
 ifnotempty2(albumartist,artist,' - ') + ifnotempty(album,' - ') + 
 ifnotempty(pad(trackno,2),' - ') + title

In future versions of Jaikoz I'm going to add the ability to create and store custom javascript functions with a name to make this even easier, but hopefully in the meantime the use of functions should make creating masks simpler.

2 comments:

Anonymous said...

It seems difficult to send a bug report without signing-in, so I'm sorry for posting in the wrong place.

BUG REPORT: The (delete file) shortcut ++ doesn't work in linux, because this key combination is reserved.

Anonymous said...

It seems that your comments feature doesn't play nicely with special characters! The shortcut is ctrl alt F9

Jthink blog Jthink Facebook page google_plus Jthink YouTube channel Email Paul at Jthink Subscribe to Weekly Newsletter