more help for scripting

Advanced Renamer forum
#1 : 09/09-12 15:13
Meiner.Einer
Meiner.Einer
Posts: 4
Is there any documentation on scripting in AR?

The page http://www.advancedrenamer.com/user_guide/metho d_script does just say there is scripting available and that JavaScript is used.
But it seems, not the complete JS is implemented, as some functions just won't work. Or I'm to stupid to find out how :-(

For example I try to use a date and can't get it to convert to a normal string.
The code
---
var d = item.imgDate;
var date = d.toString();
---
just delivers the TDateTime as string - an unuseable numer. All my tests with
d.GetFullYear();
d.GetMonth();
or any other JS date conversion functions did not work and it's tiresome to find out whats implemented by trial and error.

It took me a while to realise, that d.toString() works, but d.tostring() don't.

If there is anybody using scripting in AR, please share your code!!! Others could learn by reading it. Unfortunately there is not much in the forum yet.

I'll start by copying what I've got so far:

-------------
var name = item.newBasename + "_noDate-";
if ( index < 10 ) name = name + "0";
if ( index < 100 ) name = name + "0";
name = name + index;

item.newPath = "G:\\Bilder\\unsortiert\\_umzubenennen\\Output";

var d = 0;
if ( item.createdDate > 0 ) d = item.createdDate;
if ( item.imgDate > 0 ) d = item.imgDate;

if ( d > 0 ) {
var date = d.toString();
name = date;
};
name = name + item.ext.toUpperCase();
item.newName = name;

return item.newName;
-------------
It's not yet finished, but it works.

Thank you all!


09/09-12 15:13
#2 : 09/09-12 16:16
Stefan
Stefan
Posts: 264
Reply to #1:

Hi Meiner, please note that scripting is only implemented for a few weeks.
So please be patient till the forum is filled with useful info about scripting.

An good start is to read some basic infos about JS so to know that
JS keywords are case-sensitive and 'tostring' is different to 'toString'.


I am not an coder my self but here is an piece of code to work with date:

//====Code Start
date = new Date(item.imgDate);
d = date.toDateString();
Y = date.getFullYear();
M = '0'+date.getMonth()+1; M = M.slice(-2);
D = '0'+date.getDay();D.slice(-2);
return d+' - '+Y+'_'+M+'_'+D + item.ext;
//====Code End

but imgDate give me always 01.01.1970, so this code is wrong too.
So what is "item.imgDate" ? The same as "exif date taken" or what?

----------
If this tool had saved you time, perhaps you want to donate an Dollar to Kim?
See http://www.advancedrenamer.com/download >>> [Donate] button.


09/09-12 16:16
#3 : 09/09-12 18:16
Kim Jensen
Kim Jensen
Administrator
Posts: 799
Reply to #1:
Working with dates and times is a thing not properly implemented in scripting yet. Because of different date/time formats, time zones, and daylight saving times, working with dates and times is always more complicated than it needs to be.

The imgDate and modifiedDate are exposed to the JavaScript engine as days since january first 1899. This means that anything below one day is represented as fractions of a day. This is both silly and complicated ti work with.

I have tried to create a function that will convert to a JavaScript Date. Try this script out:
var toJSDate = function(d) {
var days = Math.floor(d - 25569) * 86400 * 1000;
var time = Math.floor((d - Math.floor(d)) * 86400 * 1000);
return new Date(days + time);
}
return toJSDate(item.imgDate).getFullYear();

It is really great that more people are beginning to work with the scripting functionality. In time it will be a really great addition to the existing functionality.

I am not sure more documentation will help a lot with getting started with scripting. More examples are in my opinion a better way. I plan to put up a blog where I among other things will talk about how to use scripting in Advanced Renamer.
Also this forum needs more functionality. JavaScript syntax highlighting and file upload is on my todo list.


09/09-12 18:16
#4 : 09/09-12 20:26
Meiner.Einer
Meiner.Einer
Posts: 4
Reply to #2:
Hi,

I didn't know, the script feature is so new. It was just a few days ago, that I stumbled over AR. I will be patient.

It's the same with JS. I know how to write scripts in different languages, but until now I never used JS. But I like learning by doing.

Thanks for the example, but as you said, its not realy working.

Yes, when using new date() the .GetFullYear()- and the other functions work, but somehow its not reading the item- object- properties correctly.

item.imgDate is the property delivering some exif date. It depends, what you configure (see http://www.advancedrenamer.com/user_guide/setti ngs_imagefiles ) The returned value is in TDateTime. Same goes for item.createdDate, which I want to use as some kind of fallback.

I just need a hint, how to convert TDateTime to String, if possible all the parts separately.

Thanks!


09/09-12 20:26
#5 : 09/09-12 20:45
Meiner.Einer
Meiner.Einer
Posts: 4
Reply to #3:
Ups, I didn't see your post until after I finished mine.
Its great to get answers so quickly.
I'll try your function as soon as possible.

Your idea with a scripting blog is good. I'd read it :-D

Thanks!


09/09-12 20:45
#6 : 09/09-12 21:04
Meiner.Einer
Meiner.Einer
Posts: 4
Reply to #3:

I just copied your function in, but its not calculating correct :-(

For example the date should be
02.09.2012 14:49:15 and it is 02.09.2012 16:49:15
27.08.2012 10:02:53 and it is 27.08.2012 12:02:52

When I just convert it to a string it shows TZ is GMT+0200

That could be the problem.

I do live in Germany - we have GMT+1 and its summer.

Maybe that helps debugging.

And again - thanks!


09/09-12 21:04
#7 : 10/09-12 07:15
Kim Jensen
Kim Jensen
Administrator
Posts: 799
Reply to #6:
The method I wrote does not handle time zones and daylight savings well. Try this function out instead:

var toJSDate = function(d) {
var days = Math.floor(d - 25569) * 86400 * 1000;
var time = Math.floor((d - Math.floor(d)) * 86400 * 1000);
return new Date(days + time + (60000 * new Date().getTimezoneOffset()));
}

I don't know if this is the best way to do it and I don't know if it will work for everybody, but seems to work for me. Here in Denmark we are in the same time zone as you.


10/09-12 07:15