In order to write to a file from the gnome extension, I use replace_contents_bytes_async
, as in the example here https://gjs.guide/guides/gio/file-operations.html
extension.js:
import { Extension } from 'resource:///org/gnome/shell/extensions/extension.js';
import GLib from 'gi://GLib';
import Gio from 'gi://Gio';
export default class ExampleExtension extends Extension {
async enable() {
const file = Gio.File.new_for_path('/tmp/test-file.txt');
const bytes = new GLib.Bytes('some file contents');
const [etag] = await file.replace_contents_bytes_async(bytes, null, false,
Gio.FileCreateFlags.REPLACE_DESTINATION, null);
}
disable() {
}
}
Judging by the documentation, replace_contents_bytes_async
should also accept a callback https://docs.gtk.org/gio/method.File.replace_contents_bytes_async.html :
const [etag] = await file.replace_contents_bytes_async(bytes, null, false,
Gio.FileCreateFlags.REPLACE_DESTINATION, null, function(source_object, res, data){});
Check the bit at the top of the documentation where it says you have to wrap the methods with Gio._promisify()
: https://gjs.guide/guides/gio/file-operations.html#promise-wrappers. The callback argument is mandatory unless you opt into the promise mode in this way.
(You can also work around it by passing the callback argument, as you guessed; but then you can't await
it. The callback form doesn't return anything, so you got the "Undefined has no properties" error because of the destructuring const [etag] = undefined
.)