okubax

recent-files pipemenu

May 31st, 2013
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.21 KB | None | 0 0
  1. #!/bin/sh
  2. # cb-recent-files-pipemenu - a script to parse .recently-used.xbel
  3. # and generate openbox pipe menu
  4. # Copyright (C) 2010 John Crawley
  5. #
  6. # This program is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation, either version 3 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. # version 2012/07/01-cb
  16.  
  17. # Usage: add
  18. # <menu id="recent" label="Recent Files" execute="/path/to/cb-recent-files-pipemenu" />
  19. # to your .config/openbox/menu.xml, or use with cb-places-pipemenu (see comments there)
  20.  
  21. maximum_entries=15 # max. number of entries in menu
  22.  
  23. #######################################################################
  24.  
  25. # look for recently-used.xbel
  26. if [ $XDG_DATA_HOME ] && [ -r "${XDG_DATA_HOME}/recently-used.xbel" ]
  27. then
  28. file_path="${XDG_DATA_HOME}/recently-used.xbel"
  29. elif [ -r "${HOME}/.local/share/recently-used.xbel" ]
  30. then
  31. file_path="${HOME}/.local/share/recently-used.xbel"
  32. elif [ -r "${HOME}/.recently-used.xbel" ]
  33. then
  34. file_path="${HOME}/.recently-used.xbel"
  35. else
  36. echo "$0: cannot find a readable recently-used.xbel file" >&2
  37. echo '<openbox_pipe_menu>
  38. <separator label="No recently-used.xbel file found." />
  39. </openbox_pipe_menu>'
  40. exit 1
  41. fi
  42.  
  43. # if argument is --clear, empty .recently-used.xbel
  44. [ "$1" = '--clear' ] && {
  45. cat <<':EOF' > "${file_path}"
  46. <?xml version="1.0" encoding="UTF-8"?>
  47. <xbel version="1.0"
  48. xmlns:bookmark="http://www.freedesktop.org/standards/desktop-bookmarks"
  49. xmlns:mime="http://www.freedesktop.org/standards/shared-mime-info"
  50. >
  51. </xbel>
  52. :EOF
  53. exit
  54. }
  55.  
  56. maximum_entries=$((maximum_entries+2))
  57.  
  58. pre=' <item label="'
  59. mid='">
  60. <action name="Execute"><command>'
  61. post='</command></action>
  62. </item>'
  63.  
  64. files=$( tac "${file_path}" | awk -v MAX="$maximum_entries" -v PR="$pre" -v MI="$mid" -v PO="$post" 'BEGIN {
  65. RS="</bookmark>";
  66. FS="<info>";
  67. }
  68. (NR == MAX) {exit}
  69. !/<bookmark/ {next}
  70. !/href=[\"'\'']file:\/\// {next}
  71. # $1 is the command, $2 the file path
  72. {
  73. sub(/^.*exec=\"\&apos\;/,"",$1)
  74. sub(/\&apos\;.*$/,"",$1)
  75. sub(/ *%./,"",$1)
  76. sub(/^.*file:\/\//,"",$2)
  77. sub(/[\"'\''].*$/,"",$2)
  78. gsub(/%22/,"\\&quot;",$2)
  79. gsub(/%3C/,"\\&lt;",$2)
  80. gsub(/%3E/,"\\&gt;",$2)
  81. name=$2
  82. sub(/^.*\//,"",name)
  83. gsub(/_/,"__",name)
  84. gsub(/\&apos;/,"\\&apos;\\&quot;\\&apos;\\&quot;\\&apos;",$2)
  85. print (PR name MI $1 " '"'"'" $2 "'"'"'" PO)
  86. }' )
  87.  
  88. # use perl to decode urlencoded characters
  89. files=$(perl -MURI::Escape -e 'print uri_unescape($ARGV[0]);' "$files")
  90.  
  91. output='<openbox_pipe_menu>
  92. '"$files"'
  93. <separator />
  94. <item label="Clear Recent Files">
  95. <action name="Execute">
  96. <command>
  97. &apos;'"$0"'&apos; --clear
  98. </command>
  99. </action>
  100. </item>
  101. </openbox_pipe_menu>
  102. '
  103. printf '%s' "$output" # printf because echo sometimes eats backslashes
Add Comment
Please, Sign In to add comment