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.




Sunday, July 1, 2012

How to change the default App for a file?

I just find that I can change the default App for a file by:

- Right click the file, for example, a jpg image, then click "Get Info"
- On the file Info dialog, you will be able to see an "Open with:".
- Select any App you want to open the file by default.
- Click "Change All..." to make it take effect for every file with ".jpg".

As the screenshot:


Sunday, March 25, 2012

Update ctags with brew

Update ctags with brew

The "ctags" coming with Mac OS is really a very old one (1993). We can update it with brew, and I think this Article is very useful.

The point is:

  • Install latest ctags with brew: brew install ctags. This should be OK
  • Replace "ctags" with:
    1. alias ctags="`brew --prefix`/bin/ctags"
    2. alias ctags >> ~/.bash_profile
    3. Please note that the original post uses ~/.bashrc but it won't work in Mac OS. So use ~/.bash_profile instead

`brew --prefix` will display the install path of brew, and append it with /bin/ctags will get the new ctags pathname. Please note that "`" is the character with the key "~" on your keyboard.

Friday, March 23, 2012

xpidl.py runs error on Mac OS

xpidl.py runs error on Mac OS X

I write firefox extension so I want to write it under Mac OS. However, I have trouble in generating the .xpt file from a .idl.

I use typelib.py which included in Gecko SDK to generate the xpt file, when I specify the command looks like this:

../../xulrunner-sdk/sdk/bin/typelib.py --cachedir=./ -I ../../xulrunner-sdk/idl ./components/xxx.idl -o ./components/xxx.xpt

I get error looks like this:

Traceback (most recent call last):
  File "../../xulrunner-sdk/sdk/bin/typelib.py", line 317, in 
    idl = p.parse(text, filename=file)
  File "/Users/xx/xulrunner-sdk/sdk/bin/xpidl.py", line 1475, in parse
    idl = self.parser.parse(lexer=self)
  File "/Users/xx/xulrunner-sdk/sdk/bin/ply/yacc.py", line 265, in parse
    return self.parseopt_notrack(input,lexer,debug,tracking,tokenfunc)
  File "/Users/xx/xulrunner-sdk/sdk/bin/ply/yacc.py", line 921, in parseopt_notrack
    lookahead = get_token()     # Get the next token
  File "/Users/xx/xulrunner-sdk/sdk/bin/xpidl.py", line 1464, in token
    t = self.lexer.token()
  File "/Users/xx/xulrunner-sdk/sdk/bin/ply/lex.py", line 384, in token
    newtok = self.lexerrorf(tok)
  File "/Users/xx/xulrunner-sdk/sdk/bin/xpidl.py", line 1150, in t_ANY_error
    lexpos=self.lexer.lexpos))
xpidl.IDLError: error: unrecognized input, ./components/xxx.idl line 1:26
#include "nsISupports.idl"
                          ^

I searched the web but can't get any hint, so I research it myself, and found out that it is caused the "newline" on Mac is "\r" but the python code used to parse the idl file is using "\n".

So it will be easy to fix this problem:

  # xulrunner-sdk/sdk/sdk/bin/xpidl.py
  # line: 1475
  data = data.replace('\r', '\n') # add this line
  self.lexer.input(data)

So the problem is solved :)

Sunday, February 12, 2012

Use JavaScript in command line

Get from this link, there is a executable "jsc" in /System/Library/Frameworks/JavaScriptCore.framework/Versions/Current/Resources/. However, which is not in your path by default, so you need to create a link in your /bin:

sudo ln /System/Library/Frameworks/JavaScriptCore.framework/Versions/Current/Resources/jsc /bin/jsc

Then you can just type "jsc" in any command line to execute javascript statements. You can use "quit();" to exit.

ddh$ jsc
> [] + []

> [] + {}
[object Object]
> {} + []
0
> {} + {}
NaN
> quit();
ddh$ 

Monday, January 9, 2012

.gitignore for xcode project

Copy from this link
*.DS_Store

# Xcode
*.pbxuser
*.mode1v3
*.mode2v3
*.perspectivev3
*.xcuserstate
*.pbxuser
*.perspective
project.xcworkspace/
xcuserdata/

# Generated files
*.o
*.pyc


#Python modules
MANIFEST
dist/
build/

# Backup files
*~.nib

# vim
*.swp
*~ 
 
-- update Oct 29 2012 --
I think the official gitignore (github) should be better :) 
link: https://github.com/GitHub/gitignore

Thursday, January 5, 2012

How to let iTunes manage data of your app?

I just read this Q&A from Apple: link.

Starting with iOS 3.2, applications can choose to allow users to add files to their app documents directory by adding the key UIFileSharingEnabled to their Info.plist. When that key is set, the contents of the directory <Application Home>/Documents will be accessible in iTunes where the user can add or remove files at will.

However, since I'm a newbie for developing iOS apps, and I don't have been a paid member of Apple, so I haven't tested this method yet :p

Sunday, December 25, 2011

NSNotification

It seems every NSNotification has a method named "object" returns the real UI element fires this notification. For example, in table view, when user click one item in the table view, a "NSTableViewSelectionDidChangeNotification" is fired, and the delegate will receive it. Then the delegate can use [aNotification object] to get the real table view. So it won't need to save a outlet of the table view.

Monday, December 5, 2011

How to import Xcode project into bitbucket

BitBucket supports private repository for free users, so it is very suitable for hosting our Xcode projects :)

When you signup your free account, and create a new project for yourself, the problem is coming, how to import your xcode project into the bitbucket?

1. Create your SSH key

First, you need to update your SSH key(s) into bitbucket, as we're OS X users, so it is very :)

1.1 Check your SSH key

In console, type:

$ls ~/.ssh<enter>

If there are id_rsa and id_rsa.pub exist, you've already got your SSH keys, so skip the next step.

1.2 Create your SSH key

In console, type:

$ssh_keygen<enter>

The program will ask you for a passphrase, you can just take it as a "password". And when the program finishes, there will be two files in your ~/.ssh

  • id_rsa - which is your private key, you need to protect this file, don't be touched by others.
  • id_ras.pub - which is the pub key, you can upload / send this file to others, such as bitbucket.

So we have our SSH key, then we upload it to bitbucket

2. Update your SSH key to bitbucket

Go to Bitbucket settings, click the "choose key", and browser to your id_rsa.pub (you may need to copy it to some directory that the finder can locate it, for example, in console, use: cp ~/.ssh/id_rsa.pub ~/ ), then click the same button, it should be "update key", after that, you public is uploaded successfully.

3. Upload your repository

I refer to this link, you need to:

  1. Create a repository in bitbucket
  2. Note the URL you should use is: git@bitbucket.org:youruser/yourepo.git
  3. in console, go to your locale repository: $cd /path/to/my/repo
  4. git push --mirror git@bitbucket.org:youruser/progbuenos.git

Then the local code will be uploaded to bitbucket, you can use git clone to fetch it back

Tuesday, November 22, 2011

How to update xcode via app store?

I just update xcode from 4.2 to 4.2.1 via App Store. However, after downloaded the latest version of xcode, there is nothing happens!
I searched the internet, and finally found that I must run /Applications/install xcode.app.... it means App Store only download the install package, and place it into /Applications, but doesn't run it for you.

You can click "install xcode" from launchpad, and follow the instructions.

Wednesday, November 16, 2011

For windows users, we all know that we can edit c:\windows\system32\drivers\etc\hosts to specify an IP address to a host name. But where is the "hosts" in Mac?

I searched the internet, and find out that this file is on: /private/etc/ so the file is:

/private/etc/hosts

And the format is the same as other systems.

Saturday, October 29, 2011

Error in .vimrc

So I copy my .vimrc from my PC to Mac. I put it into ~/. But when start MacVim, there is error message like this:

E488: Trailing characters: nocompatible^M

So the vimrc can't be loaded into MacVim. I googled but their solution doesn't work for me. There is a very simple solution is....


1. In terminal:

$mv ~/.vimrc vimrc
$mvim ~/.gvimrc

2. Then in vim:
:tabe ~/vimrc
:gg
:ctrl+v
:G
:y
:tabp
:p


That means, copy the whole content from "vimrc" to ".gvimrc" in MacVim. So the ".gvimrc" will work well.

Friday, October 28, 2011

Vim for Mac

I use vim for many years (on windows), so I also want to use vim on Mac. Because Mac OS is a type of UNIX, so there is a text based vim built-in it. But I want a GUI vim. It seems that MacVim is the best choise for vimer on OSX. You can download it from: https://github.com/b4winckler/macvim, or http://code.google.com/p/macvim/.

After download / install MacVim, there are two method to open it in console:
  • $open -a MacVim
  • $mvim
Let me explain:


1. open -a MacVim
Because MacVim is installed on /Application, so the system allow us to open the app by the name, the path is not needed. It is very handy, but it seems that if you want to edit a file with administrator privilege, "sudo open -a MacVim" can't work. So we need the second method.

2. mvim
mvim is included in the install package of MacVim, you can copy it to a dir, for example, I have a directory: ~/scripts, and I copy mvim into ~/scripts. Then I need to add ~/scripts into the PATH.
Because OSX read the follow configurations when terminal opens (via this):
  • /etc/profile
  • ~/.bash_profile
  • ~/.bash_login
  • ~/.profile
So we can edit ~/.profile and add this line:

export PATH=~/scripts:$PATH

into it. So ~/scripts is added into PATH whenever you open a terminal.
Then in terminal, you can just type:
$mvim <path to file>
or
$sudo mvim <path to system file>
to edit.

Why this blog

I just bought a Mac Mini and began to study the iOS dev. But I'm not familiar with Mac OS, so I write blog here, to recod every progress I get. And I hope this can be helpful to others :)