Skip to content

Instantly share code, notes, and snippets.

@Luiji
Created May 23, 2014 15:18
Show Gist options
  • Save Luiji/cdc5aa5df7dc9398e67e to your computer and use it in GitHub Desktop.
Save Luiji/cdc5aa5df7dc9398e67e to your computer and use it in GitHub Desktop.
Generate the ~/.icewm/menu file from .desktop files.
#!/usr/bin/env ruby
# Generate the ~/.icewm/menu file from .desktop files.
#
# Written by Luiji Maryo in 2013, updated 2014
#
# Released under the CC0 license linked below, which is just a public domain
# release that handles countries that don't recognize the public domain or the
# ability of authors to release content directly into it.
#
# CC0 License: https://creativecommons.org/publicdomain/zero/1.0/
require 'fileutils'
Encoding.default_internal = 'UTF-8'
Encoding.default_external = 'UTF-8'
CATEGORIES = %w{
AudioVideo Audio Video Development Education Game Graphics Network Office
Settings System Utility
}
menus = {}
data_dirs = ENV['XDG_DATA_DIRS']
if data_dirs.nil? or data_dirs.empty?
data_dirs = '/usr/local/share:/usr/share'
end
data_dirs.split(':').each do |dir|
Dir[dir + '/applications/*.desktop'].each do |filename|
puts filename
File.open(filename) do |file|
name = ''
icon = ''
exec = ''
categories = ''
file.each_line do |line|
next if not line.include? '='
key, value = line.chomp.split('=')
value = (value or '')
case key.downcase
when 'name' then name = value if name.empty?
when 'icon' then icon = value if icon.empty?
when 'exec' then exec = value if exec.empty?
when 'categories' then categories = value if categories.empty?
end
end
exec.gsub! /%./, ''
app = " prog \"#{name}\" \"#{icon}\" #{exec}\n"
in_category = false
categories.split(';').each do |category|
next unless CATEGORIES.include? category
menus[category] = {} unless menus.include? category
menus[category][name] = app
in_category = true
end
unless in_category
menus['Other'] = {} unless menus.include? 'Other'
menus['Other'][name] = app
end
end
end
end
unless Dir.exist? ENV['HOME'] + '/.icewm'
FileUtils.mkdir_p ENV['HOME'] + '/.icewm'
end
File.open(ENV['HOME'] + '/.icewm/menu', 'w') do |output|
menus.sort.each do |name, menu|
output.write "menu \"#{name}\" \"\" {\n"
menu.sort.each do |name, app|
output.write app
end
output.write "}\n\n"
end
end
# vim:set ts=8 sts=2 sw=2 noet:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment