Skip to content

Instantly share code, notes, and snippets.

@xgvargas
Last active March 13, 2016 02:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xgvargas/34514b32e6c1eb842d19 to your computer and use it in GitHub Desktop.
Save xgvargas/34514b32e6c1eb842d19 to your computer and use it in GitHub Desktop.
Windows batch file association

wicon

Generates two windows .reg file to install and uninstall a collection of associations for your prefered software.

Also, you can configure it to include:

  • create new file (for any file of your collection)
  • Open with... when right click a file
  • Open with... when right click a folder
  • Open with... when right click an empty area inside a opened folder.

And for each association you can define the icon and description of each file type.

⚠️ To put it clear: It will not touch your Windows registry, all it does is to create a .reg file with your configuration. Is up to you to check everything and then apply the changes by double clicking the .reg file or by importing it into regedit.exe.

# the base path for all .ico files
iconpath: D:\Projetos\small-tools\XIcon\ico\
programs: # a list of programs
- exec: C:\Users\Gustavo\AppData\Local\atom\app-1.5.4\atom.exe # full path to executable
# optional to create Open with...
context:
# mandatory
name: atom # unique name for this program
# optional (remove declaration to suppress creation of registry)
file: Open File in Atom # text for "Open with..." for files
folder: Open Folder in Atom # text for "Open with..." for folders
background: Open Folder in Atom # text for "Open with..." for background area
extensions:
# list of file types:
# [extension, description, iconfile.ico, newContext] the last one is optional
- [txt, Texto, txt.ico, True]
- [h, Header, h.ico]
- [hpp, Header, hpp.ico]
- [c, C, c.ico]
- [cpp, C++, cpp.ico]
- [vhd, VHDL, vhd.ico]
- [vhdl, VHDL, vhd.ico]
- [asm, Assembly, asm.ico]
- [java, Java, java.ico]
- [php, PHP, php.ico]
- [js, Javascript, js.ico]
- [css, Estilos, css.ico]
- [less, Estilos, less.ico]
- [sass, Estilos, sass.ico]
- [scss, Estilos, scss.ico]
- [ts, Typescript, ts.ico]
- [xml, XML, xml.ico]
- [json, JSON, json.ico]
- [yaml, YAML, yaml.ico]
- [sql, SQL, geral.ico]
# - [lst, Listagem, lst.ico]
- [rst, reStructuredText, rst.ico]
- [md, Markdown, md.ico, True]
#! /usr/bin/env python
# -*- coding: utf-8 -*-
template_intro = '''Windows Registry Editor Version 5.00
\n;--------------------------------------------------------------------
; Please review all this file BEFORE apply changes to your system.
; I'm NOT responsable for any damage caused to your system.
;--------------------------------------------------------------------'''
template_reg = '''
\n; cria associacao de {descr} <.{ext}>
[HKEY_CLASSES_ROOT\\.{ext}]
@="file_{ext}"
"PerceivedType"="text"
"Content Type"="text/plain"
\n[HKEY_CLASSES_ROOT\\file_{ext}]
@="{descr}"
"EditFlags"=dword:00040000
\n[HKEY_CLASSES_ROOT\\file_{ext}\\DefaultIcon]
@="{iconpath}{icon}"
\n[HKEY_CLASSES_ROOT\\file_{ext}\\shell\\open\\command]
@="{executable} \\"%1\\""'''
template_new = '''
\n; inclue <.{ext}> na lista de opcao do menu "Novo" do botao direito
[HKEY_CLASSES_ROOT\\.{ext}\\ShellNew]
"NullFile"=""'''
template_unreg = '''
\n; remove associacao de <.{ext}>
[-HKEY_CLASSES_ROOT\\.{ext}]
[-HKEY_CLASSES_ROOT\\file_{ext}]'''
template_context_file = '''
\n; quando clica botao direito num arquivo
[HKEY_CLASSES_ROOT\*\shell\{name}]
@="{caption}"
"Icon"="{executable}"
\n[HKEY_CLASSES_ROOT\*\shell\{name}\command]
@="{executable} \"%1\""'''
template_context_file_unreg = '''
\n; remove clique direito num arquivo
[-HKEY_CLASSES_ROOT\*\shell\{name}]'''
template_context_folder = '''
\n; quando clica botao direito numa pasta
[HKEY_CLASSES_ROOT\Directory\shell\{name}]
@="{caption}"
"Icon"="{executable}"
\n[HKEY_CLASSES_ROOT\Directory\shell\{name}\command]
@="{executable} \"%1\""'''
template_context_folder_unreg = '''
\n; remove clique direito num folder
[-HKEY_CLASSES_ROOT\Directory\shell\{name}]'''
template_context_background = '''
\n; quando clica botao direito numa parte vazia de uma pasta aberta
[HKEY_CLASSES_ROOT\LibraryFolder\Background\shell\{name}]
@="{caption}"
"Icon"="{executable}"
\n[HKEY_CLASSES_ROOT\LibraryFolder\Background\shell\{name}\command]
@="{executable} \"%V\""'''
template_context_background_unreg = '''
\n; remove clique direito numa parte vazia da pasta
[-HKEY_CLASSES_ROOT\LibraryFolder\Background\shell\{name}]'''
# --------------------------------------------------------------------------------------
import yaml
data = yaml.load(open('assoc.yaml', 'r'))
reg = open('register.reg', 'wt')
unreg = open('unregister.reg', 'wt')
reg.write(template_intro)
unreg.write(template_intro)
iconpath = repr(data['iconpath'])[1:-1]
for prog in data['programs']:
executable = repr(prog['exec'])[1:-1]
if 'context' in prog:
if 'name' in prog['context']:
if 'file' in prog['context']:
r = template_context_file.format(name=prog['context']['name'], caption=prog['context']['file'], executable=executable)
reg.write(r)
u = template_context_file_unreg.format(name=prog['context']['name'])
unreg.write(u)
if 'folder' in prog['context']:
r = template_context_folder.format(name=prog['context']['name'], caption=prog['context']['folder'], executable=executable)
reg.write(r)
u = template_context_folder_unreg.format(name=prog['context']['name'])
unreg.write(u)
if 'background' in prog['context']:
r = template_context_background.format(name=prog['context']['name'], caption=prog['context']['background'], executable=executable)
reg.write(r)
u = template_context_background_unreg.format(name=prog['context']['name'])
unreg.write(u)
for ext in prog['extensions']:
r = template_reg.format(ext=ext[0], descr=ext[1], iconpath=iconpath, icon=ext[2], executable=executable)
reg.write(r)
if len(ext) == 4:
if ext[3]:
n = template_new.format(ext=ext[0])
reg.write(n)
u = template_unreg.format(ext=ext[0])
unreg.write(u)
reg.close()
unreg.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment