Sunday, January 10, 2016

Using Coffee Script for Gnome Desktop Scripting

The Cinnamon desktop uses javascript for scripting applets and desklets. Specifially, it uses an older version of spidermonkey. I'm not sure which, but it includes only 2 ES6 features - let and const. No other new, more useful ES6 features seem to work. This is troubling, since the semantics for let and const have changed - only slightly, but enough to break some plugins.

So basically, desktop scripting in Gnome, and therefore Cinnamon, is stuck with a deprecated version of javascript. Since most of my javascript is for the browser, I don't really want to learn how to use it the wrong way. But we know a fix for this - Coffee Script!

Applet = imports.ui.applet
GLib = imports.gi.GLib
Util = imports.misc.util
Gettext = imports.gettext.domain('cinnamon-applets')
_ = Gettext.gettext

class MyApplet
    __proto__: base = Applet.IconApplet.prototype
  
    name: 'Gracie'

    constructor:(orientation)->
        base._init.call(this, orientation);
        try 
            @set_applet_icon_name("gnome-info")
            @set_applet_tooltip(_("Say Hello, #{@name}"))
            return
        
        catch e
            global.logError(e)
            return
            
    on_applet_clicked:(evtdata) =>
        notification = "notify-send \"Hello #{@name}\"  -a TEST -t 10 -u low"
        Util.spawnCommandLine(notification)
        return
        

main = (metadata, orientation)-> new MyApplet(orientation)


Note there is now no need for Lang.Class and Lang.Bind. Insted of coffee's extend, I've set the __proto__ property. You can still use extends for your own class hierarchy, but for a GObject based hierarchy, you'll need to use __proto__.

Compile with the bare flag:

$ coffee -bc applet.coffee

And breath some new life into that stale interface!

No comments:

Post a Comment