nix, shell, perl, php, mysql and mac os x tips and tricks

Wednesday, December 28, 2011

Setting a specific proxy for Google Chrome

Like Safari, Google Chrome uses OS-level proxy settings. But what if you need your browser on a different proxy? Answer: you can pass proxy info to Chrome by opening it on the command line like this:
open -a /Applications/Google\ --args --proxy-server=[host]:[port]
And if you want the convenience of launching from your dock, create a wrapper script and icon using one of these methods (copied from here because it's a flaky link):

Option 1: Use Automator to create an application that in effect launches Chrome with command line arguments.

Start Automator and select to create an Application. Double-click Run Shell Script in the Library/Utilities folder and replace the text content — cat — with the following:
open -a /Applications/Google\ --args --proxy-server=[host]:[port]
Save anywhere you like.

To replace this application's icon, Get Info on your real Google Chrome, click on the icon on the top left, press Cmd-C, Get Info on your Chrome Automator app, click the icon, and press Cmd-V.

Since it's a different application, the Dock will display two Chrome applications when it's running: Chrome, and your Chrome launcher.

Option 2: Edit your application bundle to launch a script instead. This script will start the actual application, adding the command line argument.

Right-click Google and select Show Package Contents. Go to Contents/ and open Info.plist in Property List Editor/Xcode (Apple's developer tools), or a third party plist editor.

Look for the entry CFBundleExecutable or Executable File. Remember its value (e.g. firefox-bin for Firefox). Replace it with

Open Terminal and enter the following:
touch /Applications/
open /Applications/

An editor for the .sh file will open. Set the contents of the file to:
#!/usr/bin/env bash
/Applications/ -ProfileManager

(using the actual executable's name you removed from Info.plist, adding the desired command-line arguments)

Save and close. In Terminal, enter the following:
chmod +x /Applications/

Now, close Terminal and move your application (which must not be running right now) to a different folder and back again. This will update Launch Services, otherwise your changes will be ignored and irritate you immensely.

Now, when you open your application, it will actually execute the .sh file, which will in turn launch the actual executable file, sending the command line args along.

It will look and behave like you expect it to, but you will need to repeat this whenever you update your application, as this will generally replace the application bundle and all the changes you made.

Friday, October 28, 2011

Shell Script to re-name file extensions

for file in *.jpg ; do mv $file `echo $file | sed 's/\(.*\.\)jpg/\1png/'` ; done

Monday, July 18, 2011

Remove multiple instances of a character (or a pattern) from a URL, then do a 301 redirect, with mod_rewrite

There was actually some html tags appearing in some of my URLs. This hack strips tags and does a search-engine-friendly redirect.
RewriteCond %{REQUEST_URI} ^(.*)<.*?>(.*)$
RewriteRule ^.*$ %1%2 [E=replacer:%1%2]
RewriteCond %{ENV:replacer} !^$
RewriteCond %{ENV:replacer} !^.*<.*?>.*$
RewriteRule ^.*$ %{ENV:replacer} [R=301,L]

This part matches the tags: "<.*?>". Replace that with your character or pattern.

Thursday, July 14, 2011

Check line endings of a file from command line

type 'od -c [filename]' and look for either \n (indicating LF) or \r \n (indicating CRLF)

Search bash history

Not sure why I never new this, but hitting ^r in bash will put you into history search mode. Type the beginning of the command, then just keep hitting ^R to go back in history. Helps when you want to find a long command that you entered previously.

Friday, July 1, 2011

grep pattern to match e-mail address

grep -Eiorh '([[:alnum:]_.]+@[[:alnum:]_]+?\.[[:alpha:].]{2,6})' file.txt

Tuesday, February 1, 2011

Workaround for Snow Leopard (10.6) issue with osascript command

If you see this bizarre error when using the osascript command:
2011-02-01 14:11:28.727 osascript[11990:903] Error loading /Library/ScriptingAdditions/Adobe Unit Types.osax/Contents/MacOS/Adobe Unit Types:  dlopen(/Library/ScriptingAdditions/Adobe Unit Types.osax/Contents/MacOS/Adobe Unit Types, 262): no suitable image found.  Did find:
        /Library/ScriptingAdditions/Adobe Unit Types.osax/Contents/MacOS/Adobe Unit Types: no matching architecture in universal wrapper
it apparently has to do with the mixing of 32 and 64-bit architectures. I don't know what the real fix is, but the workaround is to force osascript to use 32-bit like this:
arch -i386 /usr/bin/osascript -e '[your applescript]'