20

I'm creating a webpage right now and am wondering if it is possible to input a command in HTML that will open an installed Ubuntu app like Chromium, or a terminal window, or Nautilus.

Is something like this possible? Thanks!

Radu Rădeanu
  • 169,590
  • In Windows it was, but only if you ran IExplorer, but due to the fact that a web page is text only with a bit of server side magic, this limits what a browser is capable of. I'm sure someone will come up with something. Java app may be? – Simon Aug 10 '13 at 14:56
  • 1
    You perhaps should clarify in what environment you are running since it is very broad at the moment. For example - you could be running a html page within webkit-webview which could be connected to a python based app etc etc. – fossfreedom Aug 13 '13 at 19:07
  • Hmmm... I assumed pure HTML @fossfreedom >:-D – Rinzwind Aug 13 '13 at 19:26
  • Yes, this is just a basic HTML webpage. No databases or anything. – I Heart Ubuntu Aug 14 '13 at 00:05
  • If its any help, the site will be for linux users. Anyone could use it naturally, but I'm designing this strictly for linux/Ubuntu users.

    Seems simple enough. Launch an Ubuntu app like the terminal or calculator from a link on a webpage. For example if I do "apt://chromium-browser" it opens the Ubuntu Software Centers Chromium Page. So we know we can launch at least the USC from HTML. What about Ubuntu/linux apps?

    – I Heart Ubuntu Aug 14 '13 at 00:24

2 Answers2

24

Yes, you can by adding a new protocol handler in your Ubuntu. The following method will show you how to register the process of opening an application as app://[application_name] protocol handler in Ubuntu.

1. Create application launcher script

  • In a terminal run:

    mkdir -p bin
    

    This command will make a bin directory in your home folder if you don't already have it.

  • After run:

    gedit ~/bin/open_app.sh
    

    This will create the new file open_app.sh in gedit.

  • Copy and paste the following script in the new created file:

    #!/bin/bash
    
    if [[ "$1" != "app://" ]]; then 
        app=${1#app://}
        nohup "$app" &>/dev/null &
    else 
        nohup gnome-terminal &>/dev/null &
    fi
    
  • Save the file and close it.

  • Go back into terminal and run:

    chmod +x ~/bin/open_app.sh
    

    to grant execute access for the script.

2. Create .desktop file for application launcher

Now you must create a .desktop launcher for the above script, and tell Ubuntu to use this launcher as app:// protocol handler. Create /usr/share/applications/appurl.desktop file using the following command:

sudo -H gedit /usr/share/applications/appurl.desktop

and add the following content:

[Desktop Entry]
Name=TerminalURL
Exec=/home/radu/bin/open_app.sh %u
Type=Application
NoDisplay=true
Categories=System;
MimeType=x-scheme-handler/app;

Save the file and close it.

3. Refresh mime types database

In the file above, the line MimeType=x-scheme-handler/app; register app:// scheme handler, but to make it work we should update mime types database cache by executing command:

sudo update-desktop-database 

4. Test from terminal

Now everything should work. To test that it works from terminal, run for example this command:

xdg-open 'app://gedit'

4. Test from browser using HTML

You can test from browser by using for example the following HTML web page:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>
    <title>Open some applications</title>
    <meta http-equiv="content-type" content="text/html;charset=utf-8" />
</head>

<body>
        <h3>Open some applications in Ubuntu from HTML</h3>
        <p>Open terminal: <a title="Open" href="app://">app://</a>
        (equivalent with: <a title="Open" href="app://gnome-terminal">app://gnome-terminal</a>)</p>
        <p>Open Nautilus: <a title="Open" href="app://nautilus">app://nautilus</a></p>
        <p>Open Chromium: <a title="Open" href="app://chromium-browser">app://chromium-browser</a></p>
        <p>Open Ubuntu Software Center: <a title="Open" href="app://software-center">app://software-center</a>
        (equivalent with: <a title="Open" href="apt://">apt://</a>)</p>
        <p>...and so on</p>
</body>

</html>

The result:

app://

muru
  • 197,895
  • 55
  • 485
  • 740
Radu Rădeanu
  • 169,590
  • 3
    Very very nice. 1 slight problem: the users using the website will not have those scrips, launchers and mime types. So you also need to supply them in some manner from the website. With navigator.registerProtocolHandler it should be possible to do w/o installing software. But it does look very very nice :-D – Rinzwind Aug 14 '13 at 08:47
  • @Rinzwind Well, a script that automate the steps 1, 2 and 3 can be provided to the users using the website before to use app:// scheme handler. Using exec() function from PHP, for example, that script can be made to run in browser. – Radu Rădeanu Aug 14 '13 at 09:30
  • exec() will run server side, not client side. If you try client side, you will find browsers security will prevent the scripts running – NGRhodes Nov 02 '13 at 14:31
  • 1
    Cool! Remember to change radu to your name in appurl.desktop. As like: Exec=/home/{YOUR NAME HERE}/bin/open_app.sh %u –  May 03 '14 at 02:33
  • @RaduRădeanu Is it possible to launch an script instead of an app? For example app://shell-exec myscript.sh where shell-exec is just an another script (#!/bin/sh "$@" exec "$SHELL") – Khurshid Alam Sep 18 '16 at 06:06
  • Nice. Is there a way to pass params to the local app? – AlikElzin-kilaka Apr 23 '18 at 15:29
  • not working at all for ubuntu 20.0 – Yellow Digital Labs Jan 06 '22 at 10:54
6

Yes, it is called "Web-based protocol handlers". You need Chrome 13 or Firefox 3.0 or higher. I have seen it used to open LibreOffice.

Both Mozilla and updates.html5rocks have an explanation about how this works. (Open in Chrome/Chromium chrome://settings/handlers and it will show a list of current handlers. Firefox will list them in about:config.)

Parts from the 1st link:

Registering

Setting up a web application as a protocol handler is not a difficult process. Basically, the web application uses registerProtocolHandler() to register itself with the browser as a potential handler for a given protocol. For example:

navigator.registerProtocolHandler("mailto",
                              "https://www.example.com/?uri=%s",
                              "Example Mail");

Where the parameters are:

  • The protocol.
  • The URL template, used as the handler. The "%s" is replaced with the href of the link and a GET is executed on the resultant URL.
  • The user friendly name for the protocol handler.

When a browser executes this code, it should display a prompt to the user, asking permission to allow the web application to register as a handler for the protocol. Firefox displays a prompt in the notification bar area.

Example

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html lang="en">
<head>
    <title>Web Protocol Handler Sample - Register</title>
    <script type="text/javascript">
navigator.registerProtocolHandler("fake", "http://starkravingfinkle.org/projects/wph/handler.php?value=%s", "Fake Protocol");
    </script>
</head>
<body>
    <h1>Web Protocol Handler Sample</h1>
    <p>This web page will install a web protocol handler for the <code>fake:</code> protocol.</p>
</body>
</html>
Rinzwind
  • 299,756