Friday, January 31, 2014

Use 'sips' to manipulate images in CLI.

sips (scriptable image processing system) is a prebundled command tool which can used to manipulate images. There are two advantages:

- it is prebundled, you don't need to download and install it.
- it uses CoreImage so it is very fast.

Here are some examples:

1. Get Image size
$sips --getProperty pixelWidth  xxx.jpg
  pixelWidth: 3264

$sips --getProperty pixelHeight xxx.jpg
  pixelHeight: 2448

2. Resize an image
$sips -z 100 100 xxx.jpg
  resize xxx.jpg to 100x100

$sips --resampleWidth 100 xxx.jpg
  resize xxx.jpg 's width to 100, and resize the height to keep its ratio.

$sips --resampleHeight 100 xxx.jpg
  resize xxx.jpg's height to 100, and resize its width to keep the original ratio.

You can check its man page here: https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man1/sips.1.html

src

Monday, January 20, 2014

Something about proxy.

First, you'll need to purchase a VPS (or something else) as your proxy server.

1. Using SSH tunnel.

1.1 Prepare your ssh keys (on your computer).
1.2 Log in your VPS (with user name, for example, tom), in folder ~/.ssh/, edit the file: authorized_keys, add your public key there.
1.3 On your computer, use:

ssh -N -D 1080 tom@(IPAddress Of your VPS)

Now you get a sock5 proxy on port 1080 and you can use it on your browser.


2. Use shadowsocks

1. How to install shadowsocks on your VPS is simple: https://github.com/clowwindy/shadowsocks
2. Configure shadowsocks with Supervisor, so it will restart if crash, very useful: https://github.com/clowwindy/shadowsocks/wiki/Configure-Shadowsocks-with-Supervisor
3. However, on Mac OS, python 'pip' may have problems. You may need to use:

sudo easy_install --upgrade pip

to upgrade pip to the latest version.

4. Now you can use (on your computer):

sslocal -c [path to your config.json]

to open the SOCK5 proxy.

3. Convert SOCK5 proxy to HTTP proxy.

1. Use brew to install privoxy:

brew install privoxy

2. The config file is on /usr/local/etc/privoxy:

vim /usr/local/etc/privoxy/config

uncomment this line:

forward-socks5     127.0.0.1:8008

Here, change 8008 to the actual port your SOCKS5  uses.

Saturday, January 18, 2014

(Node.js) Use --harmony with supervisor

Currently I'm working on a web project and I'm using Node.js for it.

When testing it on my local computer,  I use supervisor (npm install -g supervisor) instead of using node directly. That is, to start my script, I use:
supervisor app.js
instead of:
node app.js
However, since I'm using the harmony mode (for keyword such as 'let'), supervisor seems can't start node with the '--harmony' flags, I must modify the script myself.

  • Using 'find . | grep supervisor' to find the location of the script: /usr/local/share/npm/lib/node_modules/supervisor
  • Edit ./lib/supervisor.js.
  • In 'function run (args)' :
Add:
function run (args) {
  var arg, next, watch, ignore, program, extensions, executor, poll_interval, debugFlag, debugBrkFlag, debugBrkFlagArg, harmony;
  while (arg = args.shift()) {
    if (arg === "--help" || arg === "-h" || arg === "-?") {
      return help();
    } else if (arg === "--quiet" || arg === "-q") {
      debug = false;
      util.debug = function(){};
      util.puts = function(){};
    } else if (arg == '--harmony') {
      harmony = true;

    } else if (arg === "--verbose" || arg === "-V") {
      verbose = true;
And:
  if (debugBrkFlag) {
    program.unshift(debugBrkFlagArg);
  }
  if (harmony) {
    program.unshift('--harmony');
  }
Save, and that's all.


Now I can use:







supervisor --harmony app.js

to start my project :)


Saturday, August 17, 2013

How to install 'brew' packages manually.

Currently when I use my 'brew' to upgrade, I got some errors that I can't download the packages from github. 

The error messages are something like this:

==> Upgrading 1 outdated package, with result:
macvim 7.4-70
==> Upgrading macvim
==> Downloading https://github.com/b4winckler/macvim/archive/snapshot-70.tar.gz
####################################                                      50.0%
curl: (33) HTTP server doesn't seem to support byte ranges. Cannot resume.
==> Trying a full download
##                                                                         3.7%
curl: (56) SSL read: error:00000000:lib(0):func(0):reason(0), errno 54
Error: Download failed: https://github.com/b4winckler/macvim/archive/snapshot-70.tar.gz


I don't know why but it seems that 'curl' just can't download the packages from github.

So I want to install the package manually:

1) Download 'https://github.com/b4winckler/macvim/archive/snapshot-70.tar.gz' manually. So now I have '~/Download/macvim-snapshot-70.tar.gz'.

2) Verify it is the right one (this step is optional, but highly recommended):
 - Open '/usr/local/Library/Formula/macvim.rb' and we can see that the files's sha1 checksum should be '66432ae0fe81b2787b23343b6c99ef81f6b52c3e'
 - Use 'shasum' (a command line tool comes with the system) to check the downloaded file, it should output the same checksum.

3) Now open 'usr/local/Library/Homebrew/download_strategy.rb', add a line:
 (it should start at line 62)
  # Private method, can be overridden if needed.
  def _fetch
    ohai "download to #{@temporary_path}"
    curl @url, '-C', downloaded_size, '-o', @temporary_path
  end


4) Run 'brew upgrade' again. It will fail, but it will also print more information we need:
==> Upgrading 1 outdated package, with result:
macvim 7.4-70
==> Upgrading macvim
==> Downloading https://github.com/b4winckler/macvim/archive/snapshot-70.tar.gz
==> download to /Library/Caches/Homebrew/macvim-7.4-70.tar.gz.incomplete
##                                                                         3.6%
curl: (33) HTTP server doesn't seem to support byte ranges. Cannot resume.
==> Trying a full download
==> download to /Library/Caches/Homebrew/macvim-7.4-70.tar.gz.incomplete
####                                                                       6.2%
curl: (56) SSL read: error:00000000:lib(0):func(0):reason(0), errno 54
Error: Download failed: https://github.com/b4winckler/macvim/archive/snapshot-70.tar.gz


Note the red line, now we know the file name should be 'macvim-7.4-70.tar.gz.incomplete'. So we just copy the file here.

5) cp ~/Download/macvim-snapshot-70.tar.gz /Library/Caches/Homebrew/macvim-7.4-70.tar.gz.incomplete

6) edit 'usr/local/Library/Homebrew/download_strategy.rb' again, comment the line after when added:
  # Private method, can be overridden if needed.
  def _fetch
    ohai "download to #{@temporary_path}"
    # curl @url, '-C', downloaded_size, '-o', @temporary_path
  end


7) Run 'brew upgrade' again.  Succeed!
==> Upgrading 1 outdated package, with result:
macvim 7.4-70
==> Upgrading macvim
==> Downloading https://github.com/b4winckler/macvim/archive/snapshot-70.tar.gz
==> download to /Library/Caches/Homebrew/macvim-7.4-70.tar.gz.incomplete
==> ./configure --with-features=huge --enable-multibyte --with-macarchs=x86_64 --enable-perlinterp --enable-rub
==> make
==> Caveats
MacVim.app installed to:
  /usr/local/Cellar/macvim/7.4-70

To link the application to a normal Mac OS X location:
    brew linkapps
or:
    ln -s /usr/local/Cellar/macvim/7.4-70/MacVim.app /Applications
==> Summary
🍺  /usr/local/Cellar/macvim/7.4-70: 1796 files, 28M, built in 74 seconds




Thursday, July 11, 2013

How to free the memory 'finder' use.

If you like me, that keep your mac run for several days, you may find that the 'finder' uses a lot of memory (sometimes more than 1GB).

Fortunately, you can 'relaunch' the finder to make it free the memory. Here is how to:

- hold the option key (alt)
- right click on the finder icon (on the dock)
- click 'relaunch'

The finder will remember how many windows it has, and their position and restore them after the relaunch.

Very usefuly :)

Sunday, December 2, 2012

How to disable 'developer mode' for xcode?

It is simple, just type:

DevToolsSecurity -disable

in a terminal window

it is from: here

Monday, August 27, 2012

rm -R with wildcard

Unfortunately, it seems the shell of Mac OSX doesn't support wildcard in 'rm -R' command. So if you want to delete, for example, some "*.swp" files which locates on many sub-directories, it will be very difficult.

However, there is a workaround by using the command "xargs". Here is the method:

$ find . | grep *.swp | xargs rm

What's the meaning?

First, "find ." will list all the files / directories in the current directory (.).
Then, the "|" will send the results to "grep", and grep will filter all the files/directories which don't meet "*.swp", that is, all the *.swp will be listed by "grep".
At last,  "xargs" will use the results returned by "grep" as argument for "rm", so that the file will be removed.