Sublime Forum

[SCRIPT] Batch Update : File Associations & Icons

#1

I just wiped my HDD for a clean OS install & made this script to avoid the whole Open with... > More Apps > Look for another app on this PC > etc... process for setting application defaults in Windows.

 
Benefits:

  • define a ton of associations simultaneously
  • you can use the same script for quick setup of associations on clean OS installations, multiple systems, etc.
  • easily define icons per file-type
     

Note:
 
If you notice that your icons don’t change after running the script, you can force an update with:
TenForums > Instant_Clear_and_Reset_Thumbnail_Cache.bat
 



###:warning:  Warning  :warning:

 
This script will overwrite registry values, so maybe back up your registry or just don’t use this if you’re not comfortable working with the registry.

( I personally use unique file-type values only, so that only the extension's file-type reference is overwritten.  The script is limited to HKEY_CURRENT_USER/SOFTWARE/Classes/, so there’s not much risk - but still: use at your own discretion. )
 

It will also dissociate any custom context menu entries that a particular extension might have, unless you match the original file-type.

( EG: .bat > Run As Administrator )
 



Script:

 

import winreg

def get_AssociationData():

	data = []

	# data.append( ( __EXTENSION__, __FILE_TYPE__, __APPLICATION_PATH__, __ICON_PATH__ ) )

#▓▓▓▓▓║     File Association Data     ║▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓⌠¦g1⌡#
	
	data.append( ( "sublime-build",         "SublimeText.Build",        "C:\\Program Files\\Sublime Text 3\\sublime_text.exe", None ) )
	data.append( ( "sublime-commands",      "SublimeText.Commands",     "C:\\Program Files\\Sublime Text 3\\sublime_text.exe", None ) )
	data.append( ( "sublime-completions",   "SublimeText.Completions",  "C:\\Program Files\\Sublime Text 3\\sublime_text.exe", None ) )
	data.append( ( "sublime-keymap",        "SublimeText.KeyMap",       "C:\\Program Files\\Sublime Text 3\\sublime_text.exe", None ) )
	data.append( ( "sublime-macro",         "SublimeText.Macro",        "C:\\Program Files\\Sublime Text 3\\sublime_text.exe", None ) )
	data.append( ( "sublime-menu",          "SublimeText.Menu",         "C:\\Program Files\\Sublime Text 3\\sublime_text.exe", None ) )
	data.append( ( "sublime-menu.template", "SublimeText.MenuTemplate", "C:\\Program Files\\Sublime Text 3\\sublime_text.exe", None ) )
	data.append( ( "sublime-mousemap",      "SublimeText.MouseMap",     "C:\\Program Files\\Sublime Text 3\\sublime_text.exe", None ) )
	data.append( ( "sublime-package",       "SublimeText.Package",      "C:\\Program Files\\Sublime Text 3\\sublime_text.exe", None ) )
	data.append( ( "sublime-project",       "SublimeText.Project",      "C:\\Program Files\\Sublime Text 3\\sublime_text.exe", None ) )
	data.append( ( "sublime-settings",      "SublimeText.Settings",     "C:\\Program Files\\Sublime Text 3\\sublime_text.exe", None ) )
	data.append( ( "sublime-snippet",       "SublimeText.Snippet",      "C:\\Program Files\\Sublime Text 3\\sublime_text.exe", None ) )
	data.append( ( "sublime-syntax",        "SublimeText.Syntax",       "C:\\Program Files\\Sublime Text 3\\sublime_text.exe", None ) )
	data.append( ( "sublime-theme",         "SublimeText.Theme",        "C:\\Program Files\\Sublime Text 3\\sublime_text.exe", None ) )
	data.append( ( "sublime-workspace",     "SublimeText.Workspace",    "C:\\Program Files\\Sublime Text 3\\sublime_text.exe", None ) )
	data.append( ( "sublime_session",       "SublimeText.Session",      "C:\\Program Files\\Sublime Text 3\\sublime_text.exe", None ) )

	data.append( ( "css",  "File.CSS",            "C:\\Program Files\\Sublime Text 3\\sublime_text.exe", None ) )
	data.append( ( "java", "File.Java",           "C:\\Program Files\\Sublime Text 3\\sublime_text.exe", None ) )
	data.append( ( "json", "File.JSON",           "C:\\Program Files\\Sublime Text 3\\sublime_text.exe", None ) )
	data.append( ( "md",   "File.MarkDown",       "C:\\Program Files\\Sublime Text 3\\sublime_text.exe", None ) )
	data.append( ( "pde",  "File.Processing",     "C:\\Program Files\\Sublime Text 3\\sublime_text.exe", None ) )
	data.append( ( "py",   "File.Python",         "C:\\Program Files\\Sublime Text 3\\sublime_text.exe", None ) )
	data.append( ( "xml",  "File.XML",            "C:\\Program Files\\Sublime Text 3\\sublime_text.exe", None ) )
	data.append( ( "xys",  "File.XYplorerScript", "C:\\Program Files\\Sublime Text 3\\sublime_text.exe", None ) )
	data.append( ( "yaml", "File.YAML",           "C:\\Program Files\\Sublime Text 3\\sublime_text.exe", None ) )
	
#▓▓▓▓▓║     /File Association Data     ║▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓⌠¦g1⌡#
	
	return( data )

def update_RegistryEntry( path, key, value ):
	try:
		winreg.CreateKey( HKEY_CURRENT_USER, path )
		registryEntry = winreg.OpenKey( HKEY_CURRENT_USER, path, 0, winreg.KEY_WRITE )
		winreg.SetValueEx( registryEntry, key, 0, winreg.REG_SZ, value )
		winreg.CloseKey( registryEntry )
		return True
	except WindowsError:
		return False

def add_FileAssociations():

	failCount = 0
	associationData = get_AssociationData()

	for dataSet in associationData:

		extension   = "." + dataSet[ 0 ]
		fileType    = dataSet[ 1 ]
		application = "\"" + dataSet[ 2 ] + "\" \"%1\""
		icon        = dataSet[ 3 ]
		extension_Path = SOFTWARE_PATH + extension
		fileType_Path  = SOFTWARE_PATH + fileType + FILETYPE_SUFFIX
		icon_Path      = SOFTWARE_PATH + fileType + "\\DefaultIcon"

		extension_WasWritten = update_RegistryEntry( extension_Path, DEFAULT_KEY, fileType    )
		fileType_WasWritten  = update_RegistryEntry( fileType_Path,  DEFAULT_KEY, application )
		icon_WasWritten      = update_RegistryEntry( icon_Path,      DEFAULT_KEY, icon        )

		if  extension_WasWritten \
		and fileType_WasWritten  \
		and icon_WasWritten:
			print( "   SUCCESS:  " + extension )
		else:
			print( "!!! FAIL !!! " + extension )
			failCount += 1

	if failCount > 0:
		print ( "\n" + str( failCount ) + " FAILED ASSOCIATIONS" )
	else:
		print ( "\n" + "ALL ASSOCIATIONS WERE SUCCESSFUL" )


HKEY_CURRENT_USER = winreg.HKEY_CURRENT_USER
SOFTWARE_PATH     = "SOFTWARE\\Classes\\"
FILETYPE_SUFFIX   = "\\shell\\open\\command"
DEFAULT_KEY       = None

add_FileAssociations()
3 Likes

Windows Explore - Context Menu
#2

On a related note, I had to reinstall windows some days ago, so many registry hacks I had to google… last time I installed windows was 2012… so well here is what I collected https://dl.dropboxusercontent.com/u/9303546/SublimeText/reg%20hacks.zip

A relevant note is that I installed sublime text in “C:\Program Files\Sublime Text\sublime_text.exe” skipping the version number… so if I need to update… to Sublime Text 4 or anything else, I don’t need to retweak the hacks…

1 Like

#3

 
Good call!  Doing that now.
 



 

 
I just did a full sweep of Explorer & Context Menu removals, here are the Registry Files.
( Most are current versions from TenForums )

Also, Default Programs Editor is pretty useful for adding custom context menu entries per extension.

1 Like

#4

thanks for sharing pretty much the same hacks, so annoying :stuck_out_tongue:

Here also some useful stuff on this context:
http://www.listary.com/ on any folder press keys and filter files quickly
http://ejie.me/ - clover - adds tabs to regular explorer and it does look like chrome
http://lockhunter.com/ - hunts what is blocking a file
https://github.com/bmatzelle/gow/releases - unixes for the windowses
https://nssm.cc/ make any program to run as a service
http://rammichael.com/7-taskbar-tweaker non fancy taskbar
https://www.hardcoded.net/dupeguru/ deduplicate stuff

2 Likes

#5

And here the missing script to add “Open with Sublime Text (admin)” /open a file with admin permissions /
https://dl.dropboxusercontent.com/u/9303546/SublimeText/SublimeAsAdmin.INF

the bad thing is that the application does not elevate, needs to be closed to work
You install with “right click” - “install” check the path to the application is the same you have…

0 Likes

#6

@tito

Good call on Listary, I had tried it a while back but didn’t like it for some reason.  Gave it another shot & it is AWESOME this time around  :grin:

I just wrote an AutoHotkey Script to launch its commands via [ single, double, long ] press of CapsLock.

1 Like

#7

#Edit:

 
I just fixed a file/application not found error caused by lack of quotations around the application & file argument ( in the event that the application or file path contains spaces )

application = dataSet[ 2 ] + " %1"
is now
application = "\"" + dataSet[ 2 ] + "\" \"%1\""

( updated @ first post )

1 Like