maybe someone can link me to a codesample or something, my bumbling attempts to set a value and googling the syntax for it dont work. I simply want to check some fields and write the value of "now()" to the "track.lastTimePlayed"-Field, but so far I only get invalid values (i tried "Date.Now()" and "app.utils.dateTime2Timestamp(Date.now())" in some different variants, they seem to gives invalid values, or i'm doing it wrong ...
let varrating = track.rating/20;
if(varrating > 1 && track.playCounter === 0) {
track.playCounter = 1; // works!
track.lastTimePlayed=Date.now(); // what syntax has to be set here?
track.commitAsync();
};
Thank you!
Last edited by Andre_H on Wed Jan 05, 2022 12:31 pm, edited 1 time in total.
- MMW 5.0.4.2690 (non-portable, shared DB & files) on Windows 2016 # only essential addons # my 24/7 media server
- MMW MMW 5.0.4.2690 (non-portable, shared DB & files) on Windows 10 # playing, testing skins & addons # my desktop app
- MMA Pro (2.0.0.1063) on Android 10, 11, 12 Phones & Tabs # WiFi Sync # playing - MP3Tag, MP3Diags, MP3DirectCut, IrfanView
Andre_H wrote: ↑Mon Jan 03, 2022 12:56 pm
Hi guys,
maybe someone can link me to a codesample or something, my bumbling attempts to set a value and googling the syntax for it dont work. I simply want to check some fields and write the value of "now()" to the "track.lastTimePlayed"-Field, but so far I only get invalid values (i tried "Date.Now()" and "app.utils.dateTime2Timestamp(Date.now())" in some different variants, they seem to gives invalid values, or i'm doing it wrong ...
if you are going to write these values into the database, you could let sql do the work for you. See here for SQLITE builtin date and time functions.
@Barry: Thanks for the suggestion, but I don't know how to initiate (automated) SQL updates. The code excerpt shown is part of a larger script (see https://www.mediamonkey.com/forum/viewtopic.php?t=98235), and much more than what is shown in it then exceeds my capabilities.
@TIV73: I'm not sure what exactly isn't working, but nothing is saved. I've tried both variants, I don't get an error message, but nothing is written in the "LastTimePlayed" field. Script itself runs, the playCounter is set after.
- MMW 5.0.4.2690 (non-portable, shared DB & files) on Windows 2016 # only essential addons # my 24/7 media server
- MMW MMW 5.0.4.2690 (non-portable, shared DB & files) on Windows 10 # playing, testing skins & addons # my desktop app
- MMA Pro (2.0.0.1063) on Android 10, 11, 12 Phones & Tabs # WiFi Sync # playing - MP3Tag, MP3Diags, MP3DirectCut, IrfanView
Please disregard the previous snippet. While the code posted works for some fields using dates, the lastTimePlayed field wants a full timestamp with time, not a date. Also make sure to lock the tracks you are trying to update, just writing the properties won't work.
var tracklist = uitools.getSelectedTracklist();
tracklist.modifyAsync(function () {
var track = tracklist.getValue(0);
track.lastTimePlayed = convertUnixToMSTimestamp(Date.now())
});
edit:
btw, if you need to figure out what kind of data a specific field wants you can either check the track property in the API reference or have a look the tracks of a tracklist using the asJson property of a tracklist which will return the values of all fields.
track.lastTimePlayed_UTC = app.utils.timestamp2DateTime( new Date().toISOString());
i.e. the param into timestamp2DateTime needs to be in the ISO form 'YYYY-MM-DD HH:MM:SS' but seeing the code now it should accept also the '2022-10-05T14:48:00.000Z' variants, i.e. every ISO 8601 variant longer than 18 chars.
EDIT2: Also seeing that toISOString is UTC, so you must use track.lastTimePlayed_UTC
EDIT3: Also, the date properties (like fileModified, dateAdded, lastTimePlayed) and its UTC variants aren't documented at https://www.mediamonkey.com/docs/api/classes/Track.html
We should add them to the docs! I'll do it.
- MMW 5.0.4.2690 (non-portable, shared DB & files) on Windows 2016 # only essential addons # my 24/7 media server
- MMW MMW 5.0.4.2690 (non-portable, shared DB & files) on Windows 10 # playing, testing skins & addons # my desktop app
- MMA Pro (2.0.0.1063) on Android 10, 11, 12 Phones & Tabs # WiFi Sync # playing - MP3Tag, MP3Diags, MP3DirectCut, IrfanView
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(track) {
var modified = false;
let varrating = track.rating/20;
////////////////////////////////////////////////////////////////////////////////////
// PLAYCOUNTER & LASTPLAYED: "PlayCounter"=1 & "LastPlayed"=Now(), wenn "PlayCounter"=0 und "Rating">1 ...
if(track.playCounter === 0 && varrating > 1) {
track.playCounter = 1;
track.lastTimePlayed_UTC = app.utils.timestamp2DateTime(new Date().toISOString());
// track.commitAsync();
modified = true;
};
if (modified)
track.commitAsync();
});
}
}
window._menuItems.editTags.action.submenu.push({
action: actions.saveStatistics,
order: 10,
grouporder: 10
});
... so, no direct SQL statements. the script runs on every change of the track as well as on right-click.
if you want the whole script (all the files needed to run it), just PM me.
- MMW 5.0.4.2690 (non-portable, shared DB & files) on Windows 2016 # only essential addons # my 24/7 media server
- MMW MMW 5.0.4.2690 (non-portable, shared DB & files) on Windows 10 # playing, testing skins & addons # my desktop app
- MMA Pro (2.0.0.1063) on Android 10, 11, 12 Phones & Tabs # WiFi Sync # playing - MP3Tag, MP3Diags, MP3DirectCut, IrfanView