by Andre_H » Tue Jun 08, 2021 2:32 am
Since the two scripts are very individually tailored to my use of the custom tags, i guess hardly anyone will use them directly. I was thinking of posting the final code in full rather than a sample for similar ideas. So, here we go:
File info.json:
Code: Select all
{
"title": "saveStatisticsToTag",
"id": "saveStatistics",
"description": "save statistics to IDTag-CustomFields",
"version": "1.0.0",
"author": "programmed by 'ludek' and 'drakinite' (MediaMonkey), cobbled together by Andre_H :-)",
"type": "general",
"icon": "saveStatistics.svg"
}
File init.js - for automatic saving on "change"-event (after playing, editing data or syncing back from MMA):
Code: Select all
//* save track-statistics to CustomFields.*//
// Custom1=DateAdded
// Custom2=Source
// Custom3=Media
// Custom4=CoverCheck
// Custom5=Rating
// Custom6=Playcount
// Custom7=LastPlayed
requirejs('actions');
// Execute when the window is ready
window.whenReady(() => {
app.listen(app, 'trackmodified', function (track) {
var modified = false;
//convert rating from 20-100 to 1-5 value as "varrating", save "varrating" to customfield, if changed ...
let varrating = track.rating/20;
if(track.custom5 != varrating.toString()) {
track.custom5 = varrating.toString();
modified = true;
};
//save playcounter & last played, if playcounter>0 and changed ...
if(track.playCounter > 0 && track.custom6 != track.playCounter.toString()) {
track.custom6 = track.playCounter.toString();
track.custom7 = app.utils.dateTime2Timestamp(UTCtoLocal(track.lastTimePlayed_UTC));
modified = true;
};
if (modified)
track.commitAsync();
});
});
function UTCtoLocal(date) {
var now = new Date();
var offset = now.getTimezoneOffset(); // offset from UTC in minutes
return new Date(date.valueOf() - offset * 60000);
}
File actions_add.js - same functionality "on demand", per right-click menu under "edit tags"
Code: Select all
//* save track-statistics to CustomFields.*//
// Custom1=DateAdded
// Custom2=Source
// Custom3=Media
// Custom4=CoverCheck
// Custom5=Rating
// Custom6=Playcount
// Custom7=LastPlayed
actions.saveStatistics = {
title: _('Speichere Statistik-Daten in CustomFields ...'),
hotkeyAble: false,
//icon: 'saveStatistics',
disabled: uitools.notMediaListSelected,
visible: window.uitools.getCanEdit,
execute: async function () {
var list = await uitools.getSelectedTracklist().whenLoaded();
if (list.count === 0) {
return;
}
list.forEach(function(itm) {
var modified = false;
//convert rating from 20-100 to 1-5 value as "varrating", save "varrating" to customfield, if changed ...
let varrating = itm.rating/20;
if(itm.custom5 != varrating.toString()) {
itm.custom5 = varrating.toString();
modified = true;
};
//save playcounter, if changed ...
if(itm.custom6 != itm.playCounter.toString()) {
itm.custom6 = itm.playCounter.toString();
modified = true;
};
//save lastplayed, if playcounter>0 and changed ...
if(itm.playCounter > 0 && itm.custom7 != app.utils.dateTime2Timestamp(UTCtoLocal(itm.lastTimePlayed_UTC))) {
itm.custom7 = app.utils.dateTime2Timestamp(UTCtoLocal(itm.lastTimePlayed_UTC));
modified = true;
};
if (modified)
itm.commitAsync();
});
}
}
function UTCtoLocal(date) {
var now = new Date();
var offset = now.getTimezoneOffset(); // offset from UTC in minutes
return new Date(date.valueOf() - offset * 60000);
}
window._menuItems.editTags.action.submenu.push({
action: actions.saveStatistics,
order: 90,
grouporder: 10
});
icon commented out, as that file has to be in the right place.
Of course I create the MMIP if someone wants it. PM!
Since the two scripts are very individually tailored to my use of the custom tags, i guess hardly anyone will use them directly. I was thinking of posting the final code in full rather than a sample for similar ideas. So, here we go:
[b][u]File[/u][/b] info.json:
[code]{
"title": "saveStatisticsToTag",
"id": "saveStatistics",
"description": "save statistics to IDTag-CustomFields",
"version": "1.0.0",
"author": "programmed by 'ludek' and 'drakinite' (MediaMonkey), cobbled together by Andre_H :-)",
"type": "general",
"icon": "saveStatistics.svg"
}[/code]
[b][u]File[/u][/b] init.js - for automatic saving on "change"-event (after playing, editing data or syncing back from MMA):
[code]//* save track-statistics to CustomFields.*//
// Custom1=DateAdded
// Custom2=Source
// Custom3=Media
// Custom4=CoverCheck
// Custom5=Rating
// Custom6=Playcount
// Custom7=LastPlayed
requirejs('actions');
// Execute when the window is ready
window.whenReady(() => {
app.listen(app, 'trackmodified', function (track) {
var modified = false;
//convert rating from 20-100 to 1-5 value as "varrating", save "varrating" to customfield, if changed ...
let varrating = track.rating/20;
if(track.custom5 != varrating.toString()) {
track.custom5 = varrating.toString();
modified = true;
};
//save playcounter & last played, if playcounter>0 and changed ...
if(track.playCounter > 0 && track.custom6 != track.playCounter.toString()) {
track.custom6 = track.playCounter.toString();
track.custom7 = app.utils.dateTime2Timestamp(UTCtoLocal(track.lastTimePlayed_UTC));
modified = true;
};
if (modified)
track.commitAsync();
});
});
function UTCtoLocal(date) {
var now = new Date();
var offset = now.getTimezoneOffset(); // offset from UTC in minutes
return new Date(date.valueOf() - offset * 60000);
}[/code]
[b][u]File[/u][/b] actions_add.js - same functionality "on demand", per right-click menu under "edit tags"
[code]//* save track-statistics to CustomFields.*//
// Custom1=DateAdded
// Custom2=Source
// Custom3=Media
// Custom4=CoverCheck
// Custom5=Rating
// Custom6=Playcount
// Custom7=LastPlayed
actions.saveStatistics = {
title: _('Speichere Statistik-Daten in CustomFields ...'),
hotkeyAble: false,
//icon: 'saveStatistics',
disabled: uitools.notMediaListSelected,
visible: window.uitools.getCanEdit,
execute: async function () {
var list = await uitools.getSelectedTracklist().whenLoaded();
if (list.count === 0) {
return;
}
list.forEach(function(itm) {
var modified = false;
//convert rating from 20-100 to 1-5 value as "varrating", save "varrating" to customfield, if changed ...
let varrating = itm.rating/20;
if(itm.custom5 != varrating.toString()) {
itm.custom5 = varrating.toString();
modified = true;
};
//save playcounter, if changed ...
if(itm.custom6 != itm.playCounter.toString()) {
itm.custom6 = itm.playCounter.toString();
modified = true;
};
//save lastplayed, if playcounter>0 and changed ...
if(itm.playCounter > 0 && itm.custom7 != app.utils.dateTime2Timestamp(UTCtoLocal(itm.lastTimePlayed_UTC))) {
itm.custom7 = app.utils.dateTime2Timestamp(UTCtoLocal(itm.lastTimePlayed_UTC));
modified = true;
};
if (modified)
itm.commitAsync();
});
}
}
function UTCtoLocal(date) {
var now = new Date();
var offset = now.getTimezoneOffset(); // offset from UTC in minutes
return new Date(date.valueOf() - offset * 60000);
}
window._menuItems.editTags.action.submenu.push({
action: actions.saveStatistics,
order: 90,
grouporder: 10
});[/code]
icon commented out, as that file has to be in the right place.
Of course I create the MMIP if someone wants it. PM!