APP REQUEST: Attribute Script Calculator *CODED*

Started by Jingo, July 08, 2017, 07:38:42 PM

Previous topic - Next topic

sinus

Quote from: Mario on August 28, 2017, 12:51:35 PM
How to get all selected files and to iterate over them (e.g., to calculate something) is explained on the Code Recipe Page under the Iterating over All Selected Files headline. You can just copy and paste that example and it will list each file name in the console.

Thanks, Mario,
I will look and try.  :D
Best wishes from Switzerland! :-)
Markus

sinus

Quote from: Mario on August 28, 2017, 12:51:35 PM
How to get all selected files and to iterate over them (e.g., to calculate something) is explained on the Code Recipe Page under the Iterating over All Selected Files headline. You can just copy and paste that example and it will list each file name in the console.

Mario, you were right, I putted some code into the script and really, the one wished attributes-field from all selected files (usually 2-10) is displayed in the Output - field.

I checked then, if the script still works, an yes, it works perfect and I can still enter some values and this will be safed only for the focussed file, what if correct.

Hence I have "only" to add the sum of the attribute-field "TOTAL" from all selected files.

Means TOTAL from file 1 plus TOTAL from file 2 and so on.
At the end I have a TOTAL sum with a number, say 500.00.

If this sum from all select files would be for example a variable (like "total-sum"), then I could led write this into one attribute-field of the focussed file, save and the work would be done.  :D

At the moment I add this total-sum with a real "table-calculator"  :-[ and write it into the blue field from the focussed file.

But how could I say the script, adding the attribute-filed from all selected files?
I can not know before the "id" 678108 and "id" 678109, if so I could simply say something like

var total-sum = id 678108 + 678109

and I had the sum, I guess.

Can you or someone please give me another hint, where I could "lern" about this?

The code so far for this fragment:

IMWS.get('v1/attributes',{
    // For which files?
    idlist: IMatch.idlist.fileWindowSelection,
    // Which set?
    set: 'Verrechnung',
    // Which attributes to retrieve?
    attributes: 'TOTAL'
}).then(function(response) {
     console.log(JSON.stringify(response,null,2));
});


If I could get the sum from this field from all selected files, I think, I would be able to get it into the field of the focussed file.
Best wishes from Switzerland! :-)
Markus

Mario

#52
That's trivial, unless you never did  it before ;)

PART 1

You first need to understand what IMWS is returning when you request Attribute data. You can see the response in your browser's network tab or you do a console.log.
For your Attribute set, the response will look like this (in this case, we have only requested the TOTAL Attribute):



The result of your query is an array with objects: "result"
Each object in the "result" array represents the data for one file in your selection.
The "id" is the file id, and the "data" array contains the Attribute data.
"data" is an array because you can have more than one row of Attribute data for a file.
If you have one record per file, the results will look like above. If you have more records per file, data will contain multiple objects.

This means to calculate the sum, you will need two nested loops:

1. The first loop iterates over all returned records (one for each selected file)
2. The second loop will iterate over the data returned for each file.

PART 2

Then you just add the value of the TOTAL attribute up. Here is how this looks in code. I made a screen shot because it is easier to understand with syntax highlighting:



How to understand this so you can do this yourself in the future:

- Run your app in your browser so you can use the debugger.
- Set a breakpoint in line 757 (your line number will be different) and run the app.
- Also set a breakpoint in 760 and 763.
- When the breakpoint is hit. look at what IMWS has returned in the Network tab of your browser.
- Look at the contents of response.result in your debugger.
- Step through the code, see how the data in each record looks, and how the "data" array looks.

This is the only way to understand this, and to learn. It's always the same, and when you have understood it, you can use whatever Attributes you have.
-- Mario
IMatch Developer
Forum Administrator
http://www.photools.com  -  Contact & Support - Follow me on 𝕏 - Like photools.com on Facebook

sinus

Puh, Mario, supercool!

Thanks really a lot!  :D :D :D


Easy for you, but not for me!
I understand your comments perfectly, thanks also for this.

I will do like you wrote with breakpoints, so that I understand it even better.
And maybe in the future I have to ask not so much!  8)

I use now notepad++ ... maybe I should switch to "Visual studio"? Is this even better?

Your code works super.
One small thing I understand not, nevertheless:

If I change this two lines of you

...
attributes: 'TOTAL'
...
total += Number(d.TOTAL);
...


with another attribute-field (instead TOTAL), like "expenses", this works also great.
I can use every attribute-field, your code works ... except if the field has a "Bindestrich".

But unfortunately my attribute-field has a divide-sign and calls

Zw-Total

If I now change your two lines with this
...
attributes: 'Zw-Total'
...
total += Number(d.Zw-Total);
...


the output displays nothing.
Is this possible, that is has to do with this - sign (Bindestrich) from Zw-Total?

Maybe JS has troubles with such a sign?
I tried alsready a lot, but it works not.

As soon as I use another attribute-field without such a - then your code works perfectly.
Maybe I should change the real attribute-field in IMatch?



Best wishes from Switzerland! :-)
Markus

Mario

#54
Visual Studio Code is far better than Notepad++.

A minus sign in an Attribute name. Geez, users come up with the strangest things, really...

If your Attribute name contains a - it cannot be used directly in the way I used d.TOTAL.
Because in your case this would read d.Zw-Total and this is interpreted as

d.Zw - Total

and neither variable exists. JavaScript will tell you that in the console in your browser.
Just change this to

total += Number(d['Zw-Total']);

This accesses the element named Zw-Total in the d object.

It general, it's the same with Attribute names, folder names, category names and file names: Simpler is better.
No special characters, if possible use only [a-z] or [A-Z] etc. Especially if you plan to access the data from apps or other software. For example, if you export attribute data to a CSV file for a database systems or Excel, these may choke on the Zw-Total field name too.
-- Mario
IMatch Developer
Forum Administrator
http://www.photools.com  -  Contact & Support - Follow me on 𝕏 - Like photools.com on Facebook

sinus

Quote from: Mario on August 29, 2017, 01:02:03 PM
Visual Studio Code is far better than Notepad++.

Thanks for this.
I use Notepad++ on a proposal of you (longer ago) and I like it very much.
But now, on your new proposal, I will try Visual Studio.  :D

Quote from: Mario on August 29, 2017, 01:02:03 PM
A minus sign in an Attribute name. Geez, users come up with the strangest things, really...
;D
True, usually I use not such letters, instead I use underscores (for example in my filenames).
But I can remember, because I thought about this, when I started to use Attributes.
And I have seen, that you have used spaces for Attributes-fields in your help-file.
Hence I thought, if spaces are allowed, minus-sign will also be ok.  :)

But now I know it better, and I will not more use them. But I think, now it is better to leave them, you have also warning about this in your help:
Renaming Attributes or Attribute Sets can break variables referring to the Attribute or Set by name, custom sort profiles and other IMatch objects and features which refer to Attributes by name. If you use Attribute variables somewhere, make sure you update the variable after renaming the Attribute or Set.

If I have plenty of time, I can rename them, means in 20 years.  ;D


Quote from: Mario on August 29, 2017, 01:02:03 PM
If your Attribute name contains a - it cannot be used directly in the way I used d.TOTAL.
Because in your case this would read d.Zw-Total and this is interpreted as

d.Zw - Total

and neither variable exists. JavaScript will tell you that in the console in your browser.
Just change this to

total += Number(d['Zw-Total']);

This accesses the element named Zw-Total in the d object.

Ahhh, thanks for this, it works. I tried several such things (auf gut Glück), but had no luck.

Take your code and ... it works! It seems simple, but if you do not know it, it is very difficult.  8)


Quote from: Mario on August 29, 2017, 01:02:03 PM
It general, it's the same with Attribute names, folder names, category names and file names: Simpler is better.
No special characters, if possible use only [a-z] or [A-Z] etc. Especially if you plan to access the data from apps or other software. For example, if you export attribute data to a CSV file for a database systems or Excel, these may choke on the Zw-Total field name too.

I will have again a better eye on this, tough I usually really look at this.
If I see sometimes filenames of my brother, phew, points over points, Umlaute, spaces and so on ... ::)

Mario, thank you very much, you have helped me a lot.

It is amazing, what can be done with JS.
I cannot do a lot, but I understand it better. And that is a good start.

I am not finnished with the script, but nearly.
And since we are in the this thread, I attach the actual script, what I could manage.
But as written several times, without Thorsten script I could not have done it and would be still with IM5.  8)

And you and Andy helped me also a lot.
So we have here a really helpful and competent community!

That is also, I think, another plus using IMatch.  :D

Best wishes from Switzerland! :-)
Markus