4

Can anybody give any working example of how to read/write Unicode text files using Javascript and Gjs, possibly using Gio and/or Glib? I'm experienced in Javascript, but these APIs are still alien to me and being quite new technologies, googling is not helping much.

I've seen this one: https://live.gnome.org/Seed/Tutorial/Simple_file_io , but it is Seed based (not Gjs), and seems quite old. I'm getting errors (like too few arguments on Gio.read on line 9) and thus cannot test using Unicode text.

A working snippet will be much helpful. Thanks!

Update: I'm testing on Ubuntu 12.04 64bit.

gpoo
  • 8,408
  • 3
  • 38
  • 53
Mehdi
  • 1,075
  • 1
  • 11
  • 24

2 Answers2

2

To get a general idea, you can check the example gio-cat.js from gjs source code repository.

You might also check the answer for "How to start writing Gnome Shell extensions", which has some links that can help you.

Community
  • 1
  • 1
gpoo
  • 8,408
  • 3
  • 38
  • 53
1

If you don't mind reading the text file synchronously, you can use GLib's helper function GLib.file_get_contents(String fileName):

const GLib = imports.gi.GLib;
//...
let fileContents = String(GLib.file_get_contents("/path/to/yourFile")[1]);
Daniel
  • 8,655
  • 5
  • 60
  • 87