LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Blogs > goumba
User Name
Password

Notices


Rate this Entry

A Python OpenBox pipe menu for recently used documents

Posted 11-15-2016 at 09:58 AM by goumba
Updated 11-16-2016 at 09:10 AM by goumba

I threw this one together in a few minutes as the others I've been able to find on the 'Net were shell scripts. This looks nicer, and can be expanded upon easier (I'm thinking icons in the future)...

Pass True for the parameter show_path if you want the path displayed in the menu (shown in parens following the short file name), otherwise it will only display the short file name.

Code:
#!/usr/bin/env python
#
# Copyright (c) who am I kidding, this is open to the F/OSS community.
# Written in 2016, A.T., LQ username: goumba
# All rights reserved.
# 
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#     * Redistributions of source code must retain the above copyright
#       notice, this list of conditions and the following disclaimer.
#     * Redistributions in binary form must reproduce the above copyright
#       notice, this list of conditions and the following disclaimer in the
#       documentation and/or other materials provided with the distribution.
#     * Neither the name of this website nor the
#       names of its contributors may be used to endorse or promote products
#       derived from this software without specific prior written permission.
# 
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

import os
import xml.etree.ElementTree as et

show_path = True
recent_fie = "~/.local/share/recently-used.xbel"

def walk_items(elem: et.Element) -> None:
    for _child in elem:
        try:
            _tag = elem.tag
            if 'bookmark' == _tag:
                _ref = elem.attrib['href']
                # Strip off the protocol for display in the menu
                _prefix = _ref.find('://') + 7
                _path, _file = os.path.split(_ref[_prefix:])
                if show_path:
                    _path = ' ({})'.format(_path)
                print(
"""  <item label="{}{}">
      <action name="Execute">
          <command>xdg-open "{}"</command>
      </action>
  </item>""".format(_file, _path, _ref)
                )
        except KeyError:
            pass
        walk_items(_child, show_path)
    
def make_menu() -> None:
    tree = et.parse(recent_file)
    root = tree.getroot()
    print("<openbox_pipe_menu>")
    walk_items(root)
    print("  <separator />")
    print(
        """  <item label="Clear Recent Documents">
      <action name="Execute">
          <command>rm {}</command>
      </action>
  </item>
</openbox_pipe_menu>
""".format(recent_file)
    )

if '__main__' == __name__:
    make_menu()
Posted in Desktop
Views 2359 Comments 0
« Prev     Main     Next »
Total Comments 0

Comments

 

  



All times are GMT -5. The time now is 01:29 PM.

Main Menu
Advertisement
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration