Is it possible to open an Ubuntu app from HTML?

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!


Solution 1:

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://

Solution 2:

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>