Creating a Hello, World! desklet for Cinnamon desktop

Write tutorials for Linux Mint here
More tutorials on https://github.com/orgs/linuxmint/discu ... /tutorials and (archive) on https://community.linuxmint.com/tutorial
Forum rules
Don't add support questions to tutorials; start your own topic in the appropriate sub-forum instead. Before you post read forum rules
Post Reply
OrangeShark

Creating a Hello, World! desklet for Cinnamon desktop

Post by OrangeShark »

I created a tutorial on making a desklet for Cinnamon in JavaScript. I wrote it based on my experience while making the reddit desklet. The tutorial provides plenty of links to documentation or even source code. It requires some knowledge of JavaScript.

http://www.erikedrosa.com/2014/12/31/he ... orial.html

Please tell me what you think, I plan on creating more tutorials on desklets.
JosephM
Level 6
Level 6
Posts: 1459
Joined: Sun May 26, 2013 6:25 pm

Re: Creating a Hello, World! desklet for Cinnamon desktop

Post by JosephM »

Nice. This is something I have seen quite a few requests for. Hopefully people find it useful and provide you with some good feedback so you feel like doing some more.
When I give opinions, they are my own. Not necessarily those of any other Linux Mint developer or the Linux Mint project as a whole.
OrangeShark

Re: Creating a Hello, World! desklet for Cinnamon desktop

Post by OrangeShark »

JosephM wrote:Nice. This is something I have seen quite a few requests for. Hopefully people find it useful and provide you with some good feedback so you feel like doing some more.
I have noticed some of those requests as well when I was looking up how to make desklets. Hopefully the tutorial will help people out.
KokoC
Level 1
Level 1
Posts: 21
Joined: Sun Aug 26, 2018 2:05 pm
Contact:

Re: Creating a Hello, World! desklet for Cinnamon desktop

Post by KokoC »

Thanks for the tutorial!
It's still working fine in 2022.


I wanted to ask for a help with simple modification on it.

Instead of showing Hello World! I would like to get the sunrise and sunset as text.

Sunrise: 7:46
Sunset: 17:31


1) JavaScript code

I'm using API call - 'https://api.sunrise-sunset.org/json?lat ... ormatted=0'

Working with browser the code below is working but not as a desklet.
I found out that XMLHttpRequest is included in browsers only.

2) using - imports.gi.Gio

I was trying to get the logic from the other desklets - without success as well.



Code


JavaScript code:

Code: Select all

function Get(yourUrl){
    var Httpreq = new XMLHttpRequest(); // a new request
    Httpreq.open("GET",yourUrl,false);
    Httpreq.send(null);
    return Httpreq.responseText;          
}

var json_obj = JSON.parse(Get('https://api.sunrise-sunset.org/json?lat=42.6977&lng=23.3219&date=today&formatted=0'));
results = json_obj.results;

var sunrise = new Date(results.sunrise);
var sunset = new Date(results.sunset);

console.log("Sunrise: "+ sunrise.getHours() + ':' + sunrise.getMinutes());
console.log("Sunset: "+ sunset.getHours() + ':' + sunset.getMinutes());

Desklet code:

Code: Select all

        const urlcatch = Gio.file_new_for_uri('https://api.sunrise-sunset.org/json?lat=42.6977&lng=23.3219&date=today&formatted=0');
        let response;

        do {
            try {

                let [successful, contents, etag_out] = urlcatch.load_contents(null);
                if (successful) {
                    response = contents.toString();
                    retries = maxRetries;
                } else {
                    response = errorBegin + _(service not available!") + errorEnd;
                }
            } catch (err) {
                response = errorBegin + err + errorEnd;
            }

            retries++;
        } while (retries < 3);
P.S. If I need I'll delete my post and will create new theme.
Knowing is not enough, we must apply. Willing is not enough, we must do.
https://softhints.com/ - simple Linux Mint guides
http://datascientyst.com/
User avatar
Drugwash
Level 5
Level 5
Posts: 733
Joined: Fri Jun 07, 2019 6:40 am
Location: Ploieşti, RO
Contact:

Re: Creating a Hello, World! desklet for Cinnamon desktop

Post by Drugwash »

This may be more than you need: link. 8)
Post Reply

Return to “Tutorials”