class AssociateExtension def initialize(xml_doc) @xml_doc = xml_doc end def associate(file, extension) file = file.gsub(/\//,'\\') file_elements = REXML::XPath.match(@xml_doc, "//File[@Source='.\\#{file}']") raise "Unable to find file '#{file}' to associate with extension '#{extension}'" if(file_elements.nil? || file_elements.size != 1) file_parent = file_elements[0].parent app=File.basename(file) # App Paths to support Start,Run -> "myapp" # file_parent.add_element 'RegistryValue', { 'Root' => 'HKLM', 'Key' => "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\App Paths\\#{app}", 'Value' => "[INSTALLDIR]#{file}", 'Type' => 'string' } # file_parent.add_element 'RegistryValue', { 'Root' => 'HKLM', 'Key' => "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\App Paths\\#{app}", 'Name' => 'Path', 'Value' => '[INSTALLDIR]', 'Type' => 'string' } # Extend to the "open with" list + Win7 jump menu pinning # file_parent.add_element 'RegistryValue', { 'Root' => 'HKLM', 'Key' => "SOFTWARE\\Classes\\Applications\\#{app}\\SupportedTypes", 'Name' => extension, 'Value' => '', 'Type' => 'string' } # file_parent.add_element 'RegistryValue', { 'Root' => 'HKLM', 'Key' => "SOFTWARE\\Classes\\Applications\\#{app}\\shell\\open\\command", 'Value' => "[INSTALLDIR]#{file} \"%1\"", 'Type' => 'string' } # MyApp.Document ProgID # file_parent.add_element 'RegistryValue', { 'Root' => 'HKLM', 'Key' => "SOFTWARE\\Classes\\#{File.basename(app, '.exe')}.Document", 'Name' => 'Aim Project File', 'Value' => "[INSTALLDIR]#{app} \"%1\"", 'Type' => 'string' } # # # # # # prog_id = file_parent.add_element 'ProgId', { 'Id' => "#{File.basename(app, '.exe')}.Document", 'Description' => "Aim project file", 'Advertise' => 'yes'} ext = prog_id.add_element 'Extension', { 'Id' => extension.gsub(/\./, '') } ext.add_element 'Verb', { 'Id' => 'open', 'Command' => "[INSTALLDIR]#{app}", 'Argument' => "\"%1\"" } end end