Skip to content

Instantly share code, notes, and snippets.

@geeksunny
Created June 21, 2012 05:56
Show Gist options
  • Save geeksunny/2964107 to your computer and use it in GitHub Desktop.
Save geeksunny/2964107 to your computer and use it in GitHub Desktop.
osxTweaks: A simple Python script for easily toggling a few options in OSX.
#!/usr/bin/python
__author__ = 'Justin Swanson'
__version__ = '0.2'
import os # For running system commands & getting directory contents.
# Configuration dictionary variable: Will store the live OS's config status
configuration = {}
### Clear screen function... clears the command prompt window.
def cls():
os.system(['clear','cls'][os.name == 'nt'])
### String to Boolean function. Parse a string into a bool true/false
def str2bool(v):
return v.lower() in ("yes", "true", "t", "1")
### "Get Status" function. Returns "enabled" / "disabled" based on a boolean.
def getStatus(bool):
return "enabled" if bool is True else "disabled"
### Toggle boolean setting
def toggleBooleanSetting(domain, key, app, setting):
# invert the setting
newSetting = not setting
# run the command
os.system('defaults write '+domain+' '+key+' -bool '+str(newSetting))
# restart the app
os.system('killall '+app)
# return the new setting
return newSetting
### Get configuration value
def getConfigValue(domain, key):
f = os.popen('defaults read '+domain+' '+key)
return str2bool(f.read().strip())
### Read System Settings
def readSystemSettings():
global configuration
configuration['dashDisabled'] = getConfigValue('com.apple.dashboard', 'mcx-disabled')
configuration['showHiddenFiles'] = getConfigValue('com.apple.Finder', 'AppleShowAllFiles')
configuration['showAllExtensions'] = getConfigValue('NSGlobalDomain', 'AppleShowAllExtensions')
configuration['autoCorrectEnabled'] = getConfigValue('NSGlobalDomain', 'NSAutomaticSpellingCorrectionEnabled')
configuration['pressAndHoldText'] = getConfigValue('NSGlobalDomain', 'ApplePressAndHoldEnabled')
### Main menu of the program.
def mainMenu():
readSystemSettings() # Do an initial reading of the current config settings.
reprint = True
while True:
# This will only clear screen and print the menu if reprint is equal to true.
if reprint == True:
cls()
print\
"-------------------------------------------------\n",\
" osxTweaks v" + __version__ + "\n",\
"-------------------------------------------------\n\n",\
" 1. Toggle the dashboard\n",\
" [Currently "+getStatus(not configuration['dashDisabled'])+"]\n",\
" 2. Toggle hidden files\n",\
" [Currently "+getStatus(configuration['showHiddenFiles'])+"]\n",\
" 3. Toggle file extensions\n",\
" [Currently "+getStatus(configuration['showAllExtensions'])+"]\n",\
" 4. Toggle text auto-correction (RESTART REQUIRED)\n",\
" [Currently "+getStatus(not configuration['autoCorrectEnabled'])+"]\n",\
" 5. Toggle press-and-hold special keybard characters (RESTART REQUIRED!)\n",\
" [Currently "+getStatus(not configuration['pressAndHoldText'])+"]\n",\
"\n",\
"\n",\
" 9. Exit program\n\n",\
"-------------------------------------------------\n"
entry = raw_input("Please enter a number: ")
reprint = True # reset's reprint to its default value
if entry == '1':
configuration['dashDisabled'] = toggleBooleanSetting('com.apple.dashboard','mcx-disabled','Dock',configuration['dashDisabled'])
elif entry == '2':
configuration['showHiddenFiles'] = toggleBooleanSetting('com.apple.Finder','AppleShowAllFiles','Finder',configuration['showHiddenFiles'])
elif entry == '3':
configuration['showAllExtensions'] = toggleBooleanSetting('NSGlobalDomain','AppleShowAllExtensions','Finder',configuration['showAllExtensions'])
elif entry == '4':
configuration['autoCorrectEnabled'] = toggleBooleanSetting('NSGlobalDomain','NSAutomaticSpellingCorrectionEnabled','SystemUIServer',configuration['autoCorrectEnabled'])
elif entry == '5':
configuration['pressAndHoldText'] = toggleBooleanSetting('NSGlobalDomain','ApplePressAndHoldEnabled','SystemUIServer',configuration['pressAndHoldText'])
elif entry in ('9','q','Q'):
return True
else:
reprint = False
print "Invalid selection!"
### Program initialization code.
if __name__ == "__main__":
mainMenu()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment