Never been to CodeSnippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world (or not, you can keep them private!)

enablesocksproxy & disablesocksproxy

For how to set up ssh & sshd to get a local SOCKS proxy on Mac OS X see:
- Local SOCKS Proxy for Safari
- AppleScript to enable SOCKS proxy on Mac OS X


# systemsetup & networksetup (Mac OS X 10.4),
# http://codesnippets.joyent.com/posts/show/1691

ls -l /usr/sbin/{networksetup,systemsetup}

/usr/bin/sudo /bin/ln -is /System/Library/CoreServices/RemoteManagement/ARDAgent.app/Contents/Support/systemsetup-panther /usr/sbin/systemsetup

/usr/bin/sudo /bin/ln -is /System/Library/CoreServices/RemoteManagement/ARDAgent.app/Contents/Support/networksetup-panther /usr/sbin/networksetup


# Get network service information on Mac OS X,
# http://codesnippets.joyent.com/posts/show/1819

function network_service_name() {

   SERVICE_GUID="$(printf "open\nget State:/Network/Global/IPv4\nd.show" | \
      /usr/sbin/scutil | /usr/bin/awk '/PrimaryService/{print $3}')"

   SERVICE_NAME="$(printf "open\nget Setup:/Network/Service/${SERVICE_GUID}\nd.show" | \
      /usr/sbin/scutil | /usr/bin/awk -F': ' '/UserDefinedName/{print $2}')"

   echo "${SERVICE_NAME}"

   return 0

}


function enablesocksproxy() {
   /usr/sbin/systemsetup -setremotelogin on 2>/dev/null
   # off is for authentication
   /usr/bin/sudo /usr/sbin/networksetup -setsocksfirewallproxy "$(network_service_name)" 127.0.0.1 8080 off
   if [[ -z "$(/usr/bin/sudo /usr/sbin/networksetup -getinfo "$(network_service_name)" | /usr/bin/egrep -i 'IP address:')" ]]; then
      echo '... not connected to the internet ...'
      return 1
   fi
   /usr/bin/ssh -q -D 8080 -f -C -N -x NAME@IPADDRESS    # customize
   /usr/bin/sudo -k
   return 0
}


function disablesocksproxy() {
   /usr/bin/sudo /usr/sbin/systemsetup -f -setremotelogin off 2>/dev/null    # note position of -f
   /usr/bin/sudo /usr/sbin/networksetup -setsocksfirewallproxystate "$(network_service_name)" off
   /usr/bin/sudo -k
   return 0
}


systemsetup -getremotelogin

sudo networksetup -getinfo "$(network_service_name)"

sudo networksetup -getsocksfirewallproxy "$(network_service_name)"

disablesocksproxy

enablesocksproxy

List & delete Safari history

# for PlistBuddy see: http://codesnippets.joyent.com/posts/show/1484

# list Safari history
function lsh() {
   /usr/libexec/PlistBuddy -c Print ~/Library/Safari/History.plist | \
      /usr/bin/awk '/^[[:space:]]+= /{sub( /^[[:space:]]+=[[:space:]]+/,""); print}' | \
      sed -E -n -e 's/^(.{1,100}).*$/\1/' -e '1!G;h;$p' | /usr/bin/nl
      #/usr/bin/sed -n '1!G;h;$p' | /usr/bin/nl
      #/usr/bin/awk '!x[$0]++' | /usr/bin/sed -n '1!G;h;$p' | /usr/bin/nl
      #/usr/bin/sort -u | /usr/bin/nl
   return 0
}

lsh


# delete Safari history except ...
# ... for those domains specified by an egrep regex

function dshe() {

   declare array_indices historyplistfile index regex

   regex="${1:-$'\777'}"   # delete entire history if there is no argument $1; for \777 see man ruby
   historyplistfile="${HOME}/Library/Safari/History.plist"

   array_indices="$(/usr/libexec/PlistBuddy -c print "${historyplistfile}" | /usr/bin/awk '/^[[:space:]]+= /{x++;print x-1,$0}' | \
       /usr/bin/egrep -iv "${regex}" | /usr/bin/awk '{print $1}' | /usr/bin/sort -rn)"

:<<-'COMMENT'
   # test
   for index in ${array_indices}; do 
      echo $index; 
      #/usr/libexec/PlistBuddy -c "Print :WebHistoryDates:'${index}'" "${historyplistfile}" 
      /usr/libexec/PlistBuddy -c "Print :WebHistoryDates:'${index}':title" "${historyplistfile}" 
   done
COMMENT

   for index in ${array_indices}; do 
      /usr/libexec/PlistBuddy -c "Delete :WebHistoryDates:'${index}'" "${historyplistfile}" 
   done

   /usr/libexec/PlistBuddy -c save "${historyplistfile}" &>/dev/null

   return 0

}


lsh
dshe unix
lsh
dshe
lsh

List & delete Safari cookies

# for PlistBuddy see: http://codesnippets.joyent.com/posts/show/1484

# list Safari cookies
function lsc() {
   /usr/libexec/PlistBuddy -c print ~/Library/Cookies/Cookies.plist | \
      /usr/bin/awk '/^[[:space:]]+Domain = /{sub( /^[[:space:]]+Domain =[[:space:]]+/,""); print}' | \
      /usr/bin/sort -u | /usr/bin/nl
      #/usr/bin/awk '!x[$0]++' | /usr/bin/sed -n '1!G;h;$p' | /usr/bin/nl
   return 0
}

lsc


# delete Safari cookies except ...
# ... for those domains specified by an egrep regex

/usr/libexec/PlistBuddy -c Print ~/Library/Cookies/Cookies.plist
/usr/libexec/PlistBuddy -c 'Print :0' ~/Library/Cookies/Cookies.plist
/usr/libexec/PlistBuddy -c 'Print :1' ~/Library/Cookies/Cookies.plist


function dsce() {

   declare array_indices cookiesplistfile index regex

   regex="${1:-$'\777'}"   # delete all cookies if there is no argument $1; for \777 see man ruby
   cookiesplistfile="${HOME}/Library/Cookies/Cookies.plist"

   array_indices="$(/usr/libexec/PlistBuddy -c print "${cookiesplistfile}" | /usr/bin/awk '/Domain = /{x++;print x-1,$0}' | \
       /usr/bin/egrep -iv "${regex}" | /usr/bin/awk '{print $1}' | /usr/bin/sort -rn)"

:<<-'COMMENT'
   # test
   for index in ${array_indices}; do 
      echo $index; 
      #/usr/libexec/PlistBuddy -c "Print :'${index}'" "${cookiesplistfile}" 
      /usr/libexec/PlistBuddy -c "Print :'${index}':Domain" "${cookiesplistfile}" 
   done
COMMENT

   for index in ${array_indices}; do 
      /usr/libexec/PlistBuddy -c "Delete :'${index}'" "${cookiesplistfile}" 
   done

   /usr/libexec/PlistBuddy -c save "${cookiesplistfile}" &>/dev/null

   return 0

}


lsc

dsce 'xxx.com|xxx.org'

lsc

Delete WebKit search fields


# defaults read com.apple.Safari | grep -i com.apple.WebKit.searchField:
# open $HOME/Library/Preferences/com.apple.Safari.plist
# put the following function into ~/.bash_login

function dws() { 

if [[ ! -e "$HOME/Library/Preferences/com.apple.Safari.plist" ]]; then
   printf "There is no com.apple.Safari.plist file in $HOME/Library/Preferences\x21\n"
   return 1
fi

defaults read com.apple.Safari | sed -En -e "s/^[[:space:]]*\"(com.apple.WebKit.searchField:.*)\" = .*$/\1/p" | while read item; do 
   #printf -- "$item\n"
   defaults delete com.apple.Safari "$item" 2>/dev/null
done

return 0

}     


Enable debug menu in Safari 3

To enable the debug menu in Safari 3, do the following
defaults write com.apple.Safari IncludeDebugMenu 1

And then enjoy the Web Inspector.

Safari <label> fix

if (navigator.userAgent.indexOf('Safari') != -1) {
	window.addEventListener('load', function() {
		var lock = false;
		var labels = document.getElementsByTagName('label');
		for (var i = 0; i < labels.length; i++)
			labels[i].addEventListener('click', function() {
				var input = (this.htmlFor ? document.getElementById(this.htmlFor) : this.getElementsByTagName('input')[0]);
				if (input && !lock) {
					input.focus();
					lock = true;
					input.click();
					lock = false;
				}
			});
	});
}

Remove favicons from Safari's cache

Safari caches the favicon for a site the first time it views it, even if the site doesn't have a favicon. This means if one is added later, or it is changed, you'll need to manually remove the cached file. This is pretty simple for a single site:

grep -r  "domain.tld" ~/Library/Safari/Icons/


Then, just remove all the files that match.