Author Topic: Write and Read a file local with Javascript (App)  (Read 3097 times)

sinus

  • Global Moderator
  • *****
  • Posts: 4707
  • IMatch-User since 2001 (IMatch 3.6)
Write and Read a file local with Javascript (App)
« on: February 23, 2016, 10:52:47 AM »
Hi
I have tried and tried and tried  8) without success.
And I have read and read with google, without success.

Does anybody know, how we can, inside an IMatch-App, with help of Javascript (or/and HTML) simply open a textfile (txt), write something and store it again?

(Local, on my harddisk)

What with Visual Basic is very easy, I am not able to do so with JS.  :-[

Thanks for help.
Best wishes from Switzerland! :-)
Markus

Mario

  • IMatch Developer
  • Administrator
  • *****
  • Posts: 31580
Re: Write and Read a file local with Javascript (App)
« Reply #1 on: February 23, 2016, 01:39:11 PM »
Reading and writing data in the local file system is severely restricted in JavaScript, for security reasons.

You don't want a web site to have direct access to files on your local computer, easily right? If JavaScript would be allowed to read files from your local disk, your security would be severely affected. Any web site you visit could scan your local computer for interesting information... :o

Potential solutions:

http://www.html5rocks.com/en/tutorials/file/dndfiles/

or (easier for a starter) use the

ReadTextFile and WriteTextFile methods available in the IMatch Application class.

I'm not sure if these will work (never tried) but you can access the Match application from your app via IMatchLib.application


JohnZeman

  • Global Moderator
  • *****
  • Posts: 1524
  • I'm too damn old to act my age.
Re: Write and Read a file local with Javascript (App)
« Reply #2 on: February 23, 2016, 02:37:24 PM »
This isn't exactly what you're asking for Markus, but if it helps you any in my Collapsible Metadata Viewer app I store my JavaScript variable values in IMatch settings, not in an external text file.

So when you click to open (or close) a section of metadata the app writes the changed value to settings then the next time you run IMatch it reads those values from IMatch settings so the app opens like it had been when you last closed it.

I'm afraid I don't have any more time to get into right now, we're working full long days at the nursery now and will be until June but you might get an idea from looking at the code of that app.

sinus

  • Global Moderator
  • *****
  • Posts: 4707
  • IMatch-User since 2001 (IMatch 3.6)
Re: Write and Read a file local with Javascript (App)
« Reply #3 on: February 23, 2016, 03:15:01 PM »
Reading and writing data in the local file system is severely restricted in JavaScript, for security reasons.

You don't want a web site to have direct access to files on your local computer, easily right? If JavaScript would be allowed to read files from your local disk, your security would be severely affected. Any web site you visit could scan your local computer for interesting information... :o

Potential solutions:

http://www.html5rocks.com/en/tutorials/file/dndfiles/

or (easier for a starter) use the

ReadTextFile and WriteTextFile methods available in the IMatch Application class.

I'm not sure if these will work (never tried) but you can access the Match application from your app via IMatchLib.application

Thanks, Mario, I will look at your links and tips.

Hm, if I do have a JavaScript inside an App of IMatch and would ONLY use it here, then nobody could have access on this way, I think.
But if it is not possible, then it is not possible.  ;D

Thanks for your answer!

and John, BTW, if you have again once time, in your great app (collapsile...) the only thing, what you could implement still, is an image of the current selected image. Not necessary of course, but I work with 2 monitors and is is nice to see the image also in the app (I have one created, but far not that sophisticates like yours).
« Last Edit: February 23, 2016, 03:23:29 PM by sinus »
Best wishes from Switzerland! :-)
Markus

sinus

  • Global Moderator
  • *****
  • Posts: 4707
  • IMatch-User since 2001 (IMatch 3.6)
Re: Write and Read a file local with Javascript (App)
« Reply #4 on: February 23, 2016, 03:20:30 PM »
This isn't exactly what you're asking for Markus, but if it helps you any in my Collapsible Metadata Viewer app I store my JavaScript variable values in IMatch settings, not in an external text file.

So when you click to open (or close) a section of metadata the app writes the changed value to settings then the next time you run IMatch it reads those values from IMatch settings so the app opens like it had been when you last closed it.

I'm afraid I don't have any more time to get into right now, we're working full long days at the nursery now and will be until June but you might get an idea from looking at the code of that app.

Hi John,
Great, this could be a good point for me. Yep, your collapsile Metatdata Viewer is, btw, very nice!
Of course, work at the nursery is important (btw: was there mot a fine music-LP (long play), with the title "nursery crime" of Genesis?).

Your hint could be really very good, if it is not too complicated I will do so, or else I will check Marios way.

(I have "created" a small memory game as an IMatch-App, and I am not finnished and want to implement a small highscore-list, so maybe your way could be a good way).

Thanks for jumping in and hey, good work in the nursery!
Best wishes from Switzerland! :-)
Markus

JohnZeman

  • Global Moderator
  • *****
  • Posts: 1524
  • I'm too damn old to act my age.
Re: Write and Read a file local with Javascript (App)
« Reply #5 on: February 24, 2016, 02:10:34 AM »
and John, BTW, if you have again once time, in your great app (collapsile...) the only thing, what you could implement still, is an image of the current selected image. Not necessary of course, but I work with 2 monitors and is is nice to see the image also in the app (I have one created, but far not that sophisticates like yours).

That could be done Markus.  I didn't want to include an image in the CMV because my purpose in the design of that app was to simply display metadata.

However about the same time I also wrote a script called Image Info which creates an HTML popup dialog that also shows metadata along with the currently selected image (screen shot attached).  I never submitted that script to the gallery here because I'd customized it expressly for my own needs, but if you'd like me to upload the script so you could see how to insert a photo into the CMV I'd be happy to do that.


[attachment deleted by admin]

sinus

  • Global Moderator
  • *****
  • Posts: 4707
  • IMatch-User since 2001 (IMatch 3.6)
Re: Write and Read a file local with Javascript (App)
« Reply #6 on: February 24, 2016, 08:50:42 AM »
Hi John

That looks really good. It is nice, to see an image with (only) the data, what are interesting to see.

Though we can do a lot with IMatch, sometimes we want to create something special, and IM allows to do so, because Mario allowe it.  ;D

Thanks John, for me it is not necessary, that you upload your app, though some other users maybe would love it.
I have created an app, also with the selected image.
But I know for sure, I do really a lot of my "code" in a horrible way, but finally it works. To include the image, I am really sure, that your way is better. Mario told me also a sophisticated way to do so (the same way like he did in the app "tiles"), but I was never able to do it really like his good way.  :-[

But finally one way works now, and is fast enough and I am glad to have this app.
I created this app also, because I looked at your collapsile app, this app showed me also some very good things and I could lern from your code, thanks!!!

I think, it is really so, that I am not a kind of programmer, but a kind of "take code here and there" and paste it together. Like I have now a small project, to create a memory-game with highscores, and although Mario told me how to take access to the images in the DB (like his "tiles"-app), I was until now not able to do so. But at the moment I can take access to one folder and one day I will hopefully be able to take access to the whole DB. Mario's "tiles" is very good, but I am not able to lern from it, because this code is so big and sophisticated, that I simply have not enough knowledge, to even only take some parts out of it. Well, you could it, I am sure, but I can not. Well, no problem, sometimes we must be content, what we are an can do.  :D

I know also, that you (and others here on the forum like Ferdinand for example) are much better "scripter". Maybe it is not (only) the age (because I guess, we are rougly the same age), but it has something to do with the "view" and "feeling", like a good programmer can "see" the code and how it should be.

It is always very nice of you, that you are trying to help, thanks for that. (of course also others, but some, like you, are outstanding).

In the attach you can see my result with the selected image with my app, this works for me, and I do better not "fiddle" around it, although the horrible code it is working.  ;D

Have a good day at your nursery.  :D

[attachment deleted by admin]
Best wishes from Switzerland! :-)
Markus

JohnZeman

  • Global Moderator
  • *****
  • Posts: 1524
  • I'm too damn old to act my age.
Re: Write and Read a file local with Javascript (App)
« Reply #7 on: February 24, 2016, 03:24:25 PM »
Thank you Markus but you give me too much credit.  Like you, I'm a self taught everything when it comes to anything to do with computers, including scripting.  Also like you, I've learned by studying the code of others over the years, plus I've done a lot of reading on my own.

Your app looks very good to me!  And if it does what you want, then it's a very good app indeed. :)

By the way, one thing I like to do when I write scripts, is to have comments everywhere in the script to help me remember why I wrote the code the way I did.  I'm about to turn 66 years old now and writing scripts does not come nearly as easy to me as it once used to so these days I need all the help I can get.  ;)

Now I'm off to spend the rest of the day in the land of blooming flowers again.  ;D

sinus

  • Global Moderator
  • *****
  • Posts: 4707
  • IMatch-User since 2001 (IMatch 3.6)
Re: Write and Read a file local with Javascript (App)
« Reply #8 on: February 24, 2016, 04:03:04 PM »
Thanks John,

that with the comments is a good idea.

And we can see, how very good scripts/apps we can write, if we does "only" like you wrote: lern from others, reading and so on.
At least for me your scripts are always very good.  :)

Have fun in the land of blooming flowers!
Best wishes from Switzerland! :-)
Markus

sinus

  • Global Moderator
  • *****
  • Posts: 4707
  • IMatch-User since 2001 (IMatch 3.6)
Re: Write and Read a file local with Javascript (App)
« Reply #9 on: February 29, 2016, 07:49:37 PM »
Just to say:

at the moment I give up.
I tried a lot, looked the net, VarToy, the collapsile App from John and so on, and I am simply not able (until now), to store somewhere on my local computer something. And read it again.

Say, create a box, the user can type in a name, this name will be stored and the next time the app reads again this name, the user can type a new name or take an old one.

Sometimes we must give up for a while, and this I will do now.  8)
(I guess, someone wise has said this somewhere  ;D)

I make a pause and try it later, in some days or better weeks.

Maybe JS is too heavy for me, at least at the moment.

I will post some news again, if I make progress.  :-\

Best wishes from Switzerland! :-)
Markus

Mario

  • IMatch Developer
  • Administrator
  • *****
  • Posts: 31580
Re: Write and Read a file local with Javascript (App)
« Reply #10 on: February 29, 2016, 08:01:34 PM »
Why don't you just store this as a setting? IMatch makes that easy from apps.
Many of the sample apps store settings inside the IMatch settings database, e.g. the tiles app or the MP3 app.
If you not really need a local text file that is. Else use the methods provided by the Application class.

sinus

  • Global Moderator
  • *****
  • Posts: 4707
  • IMatch-User since 2001 (IMatch 3.6)
Re: Write and Read a file local with Javascript (App)
« Reply #11 on: March 01, 2016, 08:12:40 AM »
Why don't you just store this as a setting? IMatch makes that easy from apps.
Many of the sample apps store settings inside the IMatch settings database, e.g. the tiles app or the MP3 app.
If you not really need a local text file that is. Else use the methods provided by the Application class.

That is why I must have a break, Mario.
I am not able to do so.

I have tried with the Application class, I have tried it with the store inside the settings, like you did with VarToy or John did it.
I had no success, though I tried really a lot.
VarToys for example stores the variables, what the user did write (wenn das ├╝berhaupt stimmt).
Even to copy this and replaces the settings with a name, I am not able to do.  :-[ :-\

Sometimes I think really, I am simply not clever enough to do so.  :-[
I will make a pause (break) and try again, if I have again the energy to do so.
Maybe I understand something completely wrong, so that a pause does maybe clear my head (you wrote sometimes something like that for you, but of course on a much much higher level  ;D).
Best wishes from Switzerland! :-)
Markus

Mario

  • IMatch Developer
  • Administrator
  • *****
  • Posts: 31580
Re: Write and Read a file local with Javascript (App)
« Reply #12 on: March 01, 2016, 08:34:40 AM »
Learning to program is never easy. Heck, some people even make a living out of it ;)

It's actually pretty easy:

To save your name or whatever text:

IMatchLib.getScriptSettings().Set("setting_name", "Markus");

'setting_name' is the name under which you store the setting.

Note: You should use a unique name so your script does not mess with settings used by other scripts, e.g. use "sinus_name" instead.

To get a saved setting back later:

var name = IMatchLib.getScriptSettings().Get("setting_name", "default value");

Show it in a message box:

alert(name);

Documentation for ScriptSettings is in the IMatch help.

You can store any number of settings, and any text length.

If you work with JavaScript objects, you can use JSON.stringify to convert them into text before storing them, and later JSON.parse to convert the text loaded from the setting back into a real JavaScript object.
« Last Edit: March 01, 2016, 08:40:21 AM by Mario »

sinus

  • Global Moderator
  • *****
  • Posts: 4707
  • IMatch-User since 2001 (IMatch 3.6)
Re: Write and Read a file local with Javascript (App)
« Reply #13 on: March 01, 2016, 05:09:04 PM »
Learning to program is never easy. Heck, some people even make a living out of it ;)

It's actually pretty easy:

Tsss, it is pretty curious: Mario, with 3 lines of code you have finnished my break!!! ;D :o :D

Thanks a lot, it works now exactly with your lines of code.
I have really tried a lot but apparently always with one or more errors, hence it worked not.
Now it works perfectly.

Now I can see, how it works and do what I want.

Thanks a lot for your help!


I think, there are not  a lot of programs out there, where the chief-programer helps so much and quickly.
But Mario does and this since years! Respect, really!

Best wishes from Switzerland! :-)
Markus