Recent files script – for fluxbox menu

Forum Forums General Software Recent files script – for fluxbox menu

  • This topic has 5 replies, 3 voices, and was last updated Mar 17-8:30 am by Girafenaine.
Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #55715
    Member
    Girafenaine

      Hello,

      I have worked on a recent files scripts for fluxbox. I already tested it somewhat, and shared it on MX linux forum (for MX-fluxbox “flavor” of course). I want to thank antiX team that let me learn about fluxbox and linux. AntiX-fluxbox is my main OS at office (frugal install to avoid mess with Win10), and MXFB at home.

      I like a recent files submenu integrated in fluxbox menu. And I like to see the folder in which is the file. That’s what I have done, mostly thanks to the BobC script for iceWM as a starting point. So a special thanks here for BobC work on that script. I adapted it for fluxbox and my liking, then added some options, especially a -s/–sort option to sort personal files and other files (because it may speed up file finding). I speeded up things by replacing some “sed” functions with bash internal functions to remove or replace text (execution time reduced by half). There could be margin for further speed improvements… for a next version 🙂

      There is a -h option to display some quick explanations you can read in the script too. You can customize of few parameters through options (max folder name length, max file name length, number of recent files to display, and even icontheme). The “special files” to be opened with geany or your chosen editor are defined as the files with “shell” or “octet” in their mime types. It’s rather experimental, even if it was OK during my test days. If you think we could define such files in another way through mimetypes or file extension, let’s improve it.

      You may add following lines in you fluxbox menu file in order to use this recent files submenu :

      
      [submenu] (Recent files) </usr/share/icons/Papirus/48x48/places/folder-recent.png>
      [include] (~/.fluxbox/recent_files_menu)
      [separator]
      [exec] (List update) {~/.fluxbox/scripts/recentfiles-fbmenu (and options if you want, eg. -s -n 25}
      [end]
      

      To update the recent files submenu I suggest (see -h option) a line in your fluxbox startup file, and then a crontab line for regular updates. However, we may change the fluxbox keys file with such a line to refresh the submenu every time you call fluxbox menu :

      
      OnDesktop Mouse3 :MacroCmd {Rootmenu} {ExecCommand ~/.fluxbox/scripts/recentfiles-fbmenu}
      

      (IceWM is better in this respect, since its submenu is generated every time you need it by calling a script and displaying it “on the fly”). On fluxbox this “MacroCmd” induces a delay in the root menu display, which is the reason why I don’t put it forward as first option.

      And the script to store in ~/.fluxbox/scripts/recentfiles-fbmenu with execution permission :

      
      #!/bin/bash
      
      ### Recent files menu for fluxbox
      ### Based on "icewm-menu-recentfiles.sh - dynamic menu of recent files" by BobC 09/04/19 for antiX-IceWM
      ### Modified by Girafenaine for use on fluxbox and especially MF-Fluxbox, with added options 13/03/21
      
      ### to initialize options and vars
      #########################################################################
      
      #recent files xml path
      RECENTUSXML=~/.local/share/recently-used.xbel
      RECENTUSDSP=~/.config/recently-used-dsp.conf
      #default command to use to open scripts and other special files
      editor=geany
      #default file to write recent files menu
      menupath=~/.fluxbox/recent_files_menu
      #file to write temporary submenu
      menupathtemp=~/.fluxbox/recent_files_menu_TEMP
      sortswitch=0
      indicator=0
      #nb of recent files to display. if not configured, default to 25 max displayed
      limitlines=25
      filenb=0
      #max length of file and folder name (to limit submenu size on the screen)
      filelength=45
      folderlength=25
      
      ### to get conf file
      #########################################################################
      
      #source ~/.fluxbox/conf/recentfiles.conf
      
      if [ -f $RECENTUSDSP ]; then
          limitlines=$( cat $RECENTUSDSP )
      else
          echo $limitlines > $RECENTUSDSP
      fi
      
      #to catch used icons path
      icontheme=$( cat ~/.config/gtk-3.0/settings.ini | grep icon-theme-name | sed 's/.*=//g' )
      
      ### to catch options
      #########################################################################
      
      while [ $# -gt 0 ] ; do
      
      if [[ $1 = "-h" || $1 = "--help" ]] ; then
      echo '#########################################################
      RECENTFILES-FBMENU SCRIPT - RECENT FILES MENU FOR FLUXBOX
      #########################################################
      
      This script generates a menu for use in fluxbox menu. It uses ~/local/share/recently-used.xbel file as the data source.
      It can display icons. For this feature to work you need to create .png icons in the icons folders of the icons
      theme that you use (fluxbox menu uses only .png, and most icons are in .svg). You can do that with "sudo nomacs" (and then use
      batch treatment in chosen folders, and save as .png).
      
      Generated menu default path is ~/.fluxbox/recent_files_menu. You may change this with the -m option. The script can sort files
      between personal files (in /home/ and not hidden) and other files (hidden or outside /home/) with the -s option.
      
      #################################
      You may use following options :
      
      		-s (or --sort)		enable sorting of files between personal files and other files
      		-n (or --number) number-of-files		overwrite system limit for number of recent files to display
      		-m (or --menupath) /path/to/generated/menu		modify default path to write generated menu
      		-i (or --iconthem) chosen-icontheme		modify default icon theme
      		-e (or --editor) command-to-execute		modify default command (geany) to open files whose mime types contains "octet" or "shell"
      		-l (or --file-length) length-of-files-names	modify default max length for files names (default : 45 characters)
      		-f (or --folder-length) length-of-files-names	modify default max length for folders names (default : 25 characters)
      
      #################################
      To use this script, you could :
      
      	1. Add this script in ~/.fluxbox/scripts/ folder with the name recentfiles-fbmenu, and give it execution permission. 
      
      	2. Add in you fluxbox menu following lines :
      		[submenu] (Recent files) </usr/share/icons/Papirus/48x48/places/folder-recent.png>
      			[include] (~/.fluxbox/recent_files_menu)
      			[separator]
      			[exec] (List update) {~/.fluxbox/scripts/recentfiles-fbmenu}
      		[end]
      
      	3.a Add this script to your fluxbox startup file, for your recent files menu to be updated at the session beginning. Just add the line : ~/.fluxbox/scripts/recentfiles-fbmenu (with needed options)
       
      	3.b Use cron (needs cron service to be on) to execute it every eg 15 minutes : "crontab -e", then add a line such as "*/15 * * * * ~/.fluxbox/scripts/recentfiles-fbmenu"
      
      #################################
      --- output examples ---
      [exec] ([scripts/]   mxfb_recent_files_menu.sh) { geany /home/username/.fluxbox/scripts/mxfb_recent_files_menu.sh } </usr/share/icons/Papirus/48x48/mimetypes/application-x-shellscript.png>
      [exec] ([gtk-3.0/]   gtk.css) { xdg-open "file:///home/username/.config/gtk-3.0/gtk.css" } </usr/share/icons/Papirus/48x48/mimetypes/text-css.png>
      ' | less
      
      exit
      
      elif [[ $1 = "-s" || $1 = "--sort" ]] ; then sortswitch=1 ; echo '[separator]' > $menupathtemp ; shift
      
      elif [[ $1 = "-e" || $1 = "--editor" ]] ; then editor=$2 ; shift 2
      
      elif [[ $1 = "-n" || $1 = "--number" ]] ; then limitlines=$2 ; shift 2
      
      elif [[ $1 = "-i" || $1 = "--icontheme" ]] ; then icontheme=$2 ; shift 2
      
      elif [[ $1 = "-m" || $1 = "--menupath" ]] ; then menupath=$2 ; shift 2
      
      elif [[ $1 = "-l" || $1 = "--file-length" ]] ; then filelength=$2 ; shift 2
      
      elif [[ $1 = "-f" || $1 = "--folder-length" ]] ; then folderlength=$2 ; shift 2
      
      else echo "unkown option \"$1\" ignored" ; shift
      
      fi
      
      done
      
      ### main function
      #########################################################################
      
      xmllines=$(( limitlines * 20 ))
      
      #function to decode ugly URL signs, using the echo -e option
      #function urldecode() { : "${*//+/ }" ; : "${_//\&apos\;/\'}" ; echo -e "${_//%/\\x}" ; } # can't see the point about "+" to replace with " "
      function urldecode() { : "${*//\&apos\;/\'}" ; echo -e "${_//%/\\x}" ; }
      
      #function to extract one line for each recent file from RECENTUSXML, used as an entry for the "while" loop
      #function extractlines() { tail -n $xmllines $1 | grep -e "<bookmark href=" -e "<mime:mime-type type=" | tail -n $limitlines2 | sed '1!b ; /<mime:mime-type/d' | sed 's/\<bookmark href\=//g' | sed 's/mime\:mime\-type type\=//g' | sed 's/\" .*/\"/' | sed 's/<//g' | sed 's/\/>//g' | sed 'N;s/\n/ /g' | sed 's/  */ /g' | tac ; }
      
      #write first line of the recent-files menu
      echo "[begin] (Recent files)" > $menupath
      
      #line-by-line reading of the RECENTUSXML file to extract last files names and data
      while read recentfile mimetype; do
          #printf '%s\n' "DEBUG recentfile: ${recentfile}  mimetype: ${mimetype} "
          localfile=$(urldecode $( : "${recentfile##*file:\/\/}" ; echo "${_//\"/}" ) )
          dispname=$( : "${localfile##*/}" ; echo "${_//\)/\\\)}" ) #last command to deal with ")" which makes fluxbox menu parsing to bug, puts "\)" instead
          length1=${#dispname}
          foldername=$( : "${localfile%/*}" ; echo "${_##*/}" )
          length2=${#foldername}
          mimeicon=$( : ${mimetype//\//-} ; echo ${_//\"/} )
          command="xdg-open"
          iconpath="/usr/share/icons/$icontheme/48x48/mimetypes"
          pathtowrite=$menupath
          #printf '%s\n' "DEBUG  dispname: ${dispname} foldername: ${foldername} localfile: ${localfile}  mimeicon: ${mimeicon} "
          #printf '%s\n' "DEBUG $dispname $length1 $length2"
      	
      	#max length we want to give to files names, taking into account folder name length 
      	if [[ $length2 -ge $folderlength ]] ; then length3=$filelength ; else length3=$(( $folderlength + $filelength - $length2 )) ; fi
      	
      	#if the file does exist and is not equal to root, then we will print a line in the menu
      	if [[ ( -e "$localfile" ) && ( "$localfile" != "/" ) ]]; then
      
      		#to exit "while read" loop when reaching the fixed number of recent files
      		filenb=$((filenb+1))
      		if [[ $filenb -ge $((limitlines+1)) ]] ; then break ; fi 
      
      		#to sort personal and other files, if --sort option is enabled
      		if [[ ( $sortswitch -eq 1 ) && ( "$localfile" != "/home/"* || "$localfile" = *"/."* ) ]] ; then pathtowrite=$menupathtemp ; indicator=1 ; fi
      
      		if [ -z "$mimeicon" ]; then mimeicon="application-x-executable" ; fi
      
      		#if the file is actually a folder
      		if [ -d "$localfile" ]; then
      			mimeicon="folder"
      			iconpath="/usr/share/icons/$icontheme/48x48/places"
      			dispname=$dispname/
      		fi
      
      		#management of some files that are better opened with geany or other text editor           
              if [[ $mimeicon == *"octet"* || $mimeicon == *"shell"* ]] ; then command=$editor ; fi
              
      		#write the line of menu for the file, with adapted lengths of folder (max $folderlength) and file (max $length3 wich is $filelength plus the place left by the folder name if folder name is not too long).
      		printf '%s\n' "[exec] ([${foldername:0:$folderlength}/]   ${dispname:0:$length3} ) { $command \"$localfile\" } <$iconpath/$mimeicon.png>" >> $pathtowrite
              
          fi
          
      done < <( tail -n $xmllines $RECENTUSXML | grep -e "<bookmark href=" -e "<mime:mime-type type=" | sed '1!b ; /<mime:mime-type/d' | sed 's/\<bookmark href\=//g' | sed 's/mime\:mime\-type type\=//g' | sed 's/\" .*/\"/' | sed 's/<//g' | sed 's/\/>//g' | sed 'N;s/\n/ /g' | sed 's/  */ /g' | tac )
      # above command extract from RECENTUSXML file only one line for each recent file, with file name followed by application to use to open the file, and send it into the while loop
      
      if [[ $sortswitch -eq 1 && $indicator -eq 1 ]] ; then cat $menupathtemp >> $menupath ; fi
      
      echo [end] >> $menupath
      
      echo "Menu generated in "$menupath" "
      
      rm -f $menupathtemp
      
      exit 0
      

      Girafenaine
      ----
      Antix 19 - Fluxbox - Live USB stick and frugal / MX 19 - Fluxbox - Dell XPS 7590

      #55721
      Anonymous
        Helpful
        Up
        0
        ::

        .

        #55720
        Anonymous
          Helpful
          Up
          0
          ::

          prior related antixforum discussions:

          https://www.antixforum.com/forums/topic/recent-files-non-latin-wrong-symbols/

          .
          https://www.antixforum.com/forums/topic/one-liner-gui-to-open-recent-files/
          .

          https://www.antixforum.com/forums/topic/antix-not-pretty-but-highly-functional/page/4/#post-47866

          “Recent” aka “Recent Files”
          Many programs keep their own, internal, list of recently used without consulting or writing to the communal “recently-used.xbel” file.
          Some do not maintain an internal list and STILL do not write/consult the communal file. This problem, this discrepancy, cannot
          be adequately solved via reactionary intervention by antiX distroteers (distribution maintainers).

          .

          https://www.antixforum.com/forums/topic/an-idea-icewm-dynamic-desktop-menu/

          [..]
          recent files:
          1. I can make it a 2 level menu so it doesn’t delay for calculating until you actually click on the 2nd level menu.
          A. on that menu i can put an option to change the default number of files if you make a yad script to actually do it. It should save permanently and get loaded again when the menu is clicked, and if empty then created with default of 10.
          B. also put on that menu your YAD script to clear recent files.
          2. The option I put at the top works if you have a program installed to bring up the .xbel files. I will just remove it since be default it isn’t part of a default antiX install.
          3. I will change the initial default to be 10 to make everyone happy.
          4. I looked at doing the load 10, then next 10 if clicked, and it was going to be quite tricky. As soon as you click one, the menu goes away and you have to start over.
          5. Something that could be done would be to create links to all the icons in your ~/.icewm/icons folder if not already there to make the icons get found quickly the next time . Just an idea, not sure if it would really help.
          [..]

          .

          https://www.antixforum.com/forums/topic/new-applauncher-py/page/3/#post-24339
          [..]
          https://www.antixforum.com/forums/topic/new-applauncher-py/page/3/#post-24295

          Unless you are operating from within a walled-garden desktop environment, relying on that is hopeless ~~ it will always be woefully incomplete
          because many applications maintain their own individual “recent” list rather than the “wants to be a universal” list.

          #55743
          Member
          Girafenaine
            Helpful
            Up
            0
            ::

            Hello Skidoo,

            Thanks for all this background about recent files and their management in antiX Fluxbox. I was not aware that you already saw my script on MX forum, neither that uxer wrote some script for same use (but it’s for icewm isn’t it ?)

            I know about apps not using xbel file, which is only a “half-recent files list”. But it may be still useful for some people.
            I don’t like very much the “one-liner” script with yad, which opens a external window you have to close manually (and I like better a fluxbox submenu).
            About delay in menu displaying, I avoid this with a simple crontab line. Recent files submenu is updated as a “background task” and does not delay fluxbox menu displaying.

            As a matter of fact, this BobC script I adapted for fluxbox and speeded up a little is available for those who find it helpful ! Since it is integrated in antiX Icewm, it could be useful in antiX Fluxbox as well.

            Regards

            • This reply was modified 2 years, 1 month ago by Girafenaine.
            • This reply was modified 2 years, 1 month ago by Girafenaine.

            Girafenaine
            ----
            Antix 19 - Fluxbox - Live USB stick and frugal / MX 19 - Fluxbox - Dell XPS 7590

            #55850
            Moderator
            BobC
              Helpful
              Up
              0
              ::

              I’m glad you were able to make use of and improve the code.

              I’m self taught, so I have always learned from others mentoring and code examples, and by doing, and by creating or enhancing the things I’ve found or missed over the years on the various systems I’ve worked on.

              It’s always good for people that know the language better to look at code to find better ways to make it work. It looks like you’re having fun at it. I’ll have to try to figure out the differences.

              #55893
              Member
              Girafenaine
                Helpful
                Up
                0
                ::

                Hello BobC,

                Here you were the teacher ! Your script was the model I’ve followed, and then urged me to learn about bash scripting. I spend time on the web to learn some basis with you script opened on half the screen. It’s my first attempt to make a useful script, and without you I would have delayed this much longer…

                So : thank you very much, to you and all antiX team.

                I would be pleased if you may figure out the differences 🙂 As stated, there are some added options, and some speed thanks to a few internal bash functions instead of “sed” or other external functions (execution time assessed with “time” function in the terminal).

                Regards

                Girafenaine
                ----
                Antix 19 - Fluxbox - Live USB stick and frugal / MX 19 - Fluxbox - Dell XPS 7590

              Viewing 6 posts - 1 through 6 (of 6 total)
              • You must be logged in to reply to this topic.