Advertisement
rowrz

for alexis

Oct 17th, 2014
266
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.77 KB | None | 0 0
  1. #!/usr/bin/python
  2.  
  3. #this part imports all the required libraries for accessing the preference bundles in os x
  4. from Foundation import NSMutableArray, NSMutableDictionary
  5. from Foundation import CFPreferencesSynchronize, CFPreferencesCopyValue, CFPreferencesCopyKeyList, CFPreferencesSetValue, CFPreferencesCopyMultiple, CFPreferencesSetMultiple, kCFPreferencesCurrentUser, kCFPreferencesAnyHost
  6. import os, sys
  7.  
  8. # We only handle Yosemite's spotlight for now
  9.  
  10. #this part checks to make sure that u are running the right os x
  11. majorRelease = int(os.uname()[2].split(".")[0])
  12. if majorRelease < 14:
  13.   print "Good news! This version of Mac OS X's Spotlight is not known to invade your privacy."
  14.   sys.exit(0)
  15. #this part is just a big list of things to write to the preference bundle, in our case we're turning off bing & suggestions
  16. def fixSpotlight ():
  17.   DISABLED_ITEMS=set(["MENU_WEBSEARCH", "MENU_SPOTLIGHT_SUGGESTIONS"])
  18.   REQUIRED_ITEM_KEYS=set(["enabled", "name"])
  19.   BUNDLE_ID="com.apple.Spotlight"
  20.   PREF_NAME="orderedItems"
  21.   DEFAULT_VALUE=[
  22.     {'enabled' : True, 'name' : 'APPLICATIONS'},
  23.     {'enabled' : False, 'name' : 'MENU_SPOTLIGHT_SUGGESTIONS'},
  24.     {'enabled' : True, 'name' : 'MENU_CONVERSION'},
  25.     {'enabled' : True, 'name' : 'MENU_EXPRESSION'},
  26.     {'enabled' : True, 'name' : 'MENU_DEFINITION'},
  27.     {'enabled' : True, 'name' : 'SYSTEM_PREFS'},
  28.     {'enabled' : True, 'name' : 'DOCUMENTS'},
  29.     {'enabled' : True, 'name' : 'DIRECTORIES'},
  30.     {'enabled' : True, 'name' : 'PRESENTATIONS'},
  31.     {'enabled' : True, 'name' : 'SPREADSHEETS'},
  32.     {'enabled' : True, 'name' : 'PDF'},
  33.     {'enabled' : True, 'name' : 'MESSAGES'},
  34.     {'enabled' : True, 'name' : 'CONTACT'},
  35.     {'enabled' : True, 'name' : 'EVENT_TODO'},
  36.     {'enabled' : True, 'name' : 'IMAGES'},
  37.     {'enabled' : True, 'name' : 'BOOKMARKS'},
  38.     {'enabled' : True, 'name' : 'MUSIC'},
  39.     {'enabled' : True, 'name' : 'MOVIES'},
  40.     {'enabled' : True, 'name' : 'FONTS'},
  41.     {'enabled' : True, 'name' : 'MENU_OTHER'},
  42.     {'enabled' : False, 'name' : 'MENU_WEBSEARCH'}
  43.   ]
  44. #this part begins the writing preference bundle proccess, we pass in the items from the list before and all the other junk we brought in
  45.   items = CFPreferencesCopyValue(PREF_NAME, BUNDLE_ID, kCFPreferencesCurrentUser, kCFPreferencesAnyHost)
  46.   newItems = None
  47.   if items is None or len(items) is 0:
  48.     # Actual preference values are populated on demand; if the user
  49.     # hasn't previously configured Spotlight, the preference value
  50.     # will be unavailable
  51.     newItems = DEFAULT_VALUE
  52.   else:
  53.   #this part handles errors if things havent been set up yet
  54.     newItems = NSMutableArray.new()
  55.     for item in items:
  56.       missing_keys = []
  57.       for key in REQUIRED_ITEM_KEYS:
  58.         if not item.has_key(key):
  59.           missing_keys.append(key)
  60.          #more error hadnling
  61.       if len(missing_keys) is not 0:
  62.         print "Preference item %s is missing expected keys (%s), skipping" % (item, missing_keys)
  63.         newItems.append(item)
  64.         continue
  65.      #little bit more
  66.       if not item["name"] in DISABLED_ITEMS:
  67.         newItems.append(item)
  68.         continue
  69.         #and here we go with some of the writing
  70.       newItem = NSMutableDictionary.dictionaryWithDictionary_(item)
  71.       newItem.setObject_forKey_(0, "enabled")
  72.       newItems.append(newItem)
  73.       #blah mor writing "set value" with all our things from the previous
  74.   CFPreferencesSetValue(PREF_NAME, newItems, BUNDLE_ID, kCFPreferencesCurrentUser, kCFPreferencesAnyHost)
  75.   CFPreferencesSynchronize(BUNDLE_ID, kCFPreferencesCurrentUser, kCFPreferencesAnyHost)
  76. #run teh whole program with the classic ()
  77. fixSpotlight()
  78. #tell the user it ran
  79. print "All done. Make sure to log out (and back in) for the changes to take effect."
  80.  
  81.  
  82. ##there u go alexis i broke down the python script :P
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement