Tips and Tricks of OSX makes you smile

I love to work with Mac OSX. Few days before, I came across the Tips and Tricks of OSX in Stack Exchange answers. It is mind blowing and has lot more stuff to learn. I like to share tips and tricks that I like and using regularly.

Quick Look in Finder

When you are in Finder and wanted to check the content of a file, you typically double click the item. Instead you can hit the Spacebar to have quick look. It is much faster than opening the file.

Spotlight Search and its Calculator

Spotlight Search can be opened by hitting ⌘ + space.

The Spotlight search can also handle simple mathematical expressions. You can type any simple math expression, it will give you the answer.

Show Hidden files in Finder

By default, The Finder hide the hidden files which starts with .(dot). To set Finder to show hidden files, run following command in the terminal

defaults write com.apple.finder AppleShowAllFiles TRUE

killall Finder

Reopen Finder. Now it will show hidden files.

You can use ⌘ + ⇧ + . that will show hidden files in any file-open dialog box.

Finder Window Shortcuts

In Finder window or Open/Save dialog,

 +  + G - Get a location bar from which you can type
 + I     - Info window shows for the selected item.
 + F     - Cursor jumps to the Find text field
 + R     - Reveals the selected item in a new Finder window.

In Open/Save dialog,

 + D     - Selects the Desktop folder as a destination

Show and Hide Desktop Icons

When you do presentation or in meeting, you do not want others to see your desktop. You can use following commands to hide and show desktop icons.

To Hide Desktop icons

defaults write com.apple.finder CreateDesktop -bool false && killall Finder

To Show Desktop icons

defaults write com.apple.finder CreateDesktop -bool true && killall Finder

Screenshot shortcuts

You can use following shortcuts to take screenshots. Kudos to screenshot-secret

⌘+⇧+3 - Capture entire screen and save as a file

⌘+ctrl+⇧+3 - Capture entire screen and copy to the clipboard

⌘+⇧+4 - Capture dragged area and save as a file

⌘+ctrl+⇧+4 - Capture dragged area and copy to the clipboard

⌘+⇧+4 then space - Capture a window, menu, desktop icon, or the menu bar and save as a file

⌘+ctrl+⇧+4 then space - Capture a window, menu, desktop icon, or the menu bar and copy to the clipboard

Clipboard copy and paste from Terminal

You may want to copy output of some command to clipboard, then use pbcopy

echo "I am going to clipboard" | pbcopy

Save clipboard contents to file

pbpaste > myfile.txt

Start Quick Web server

Start a quick web server by

python -m SimpleHTTPServer

It will start web server port 8000 and serve files in the current directory. To start in a specific port

python -m SimpleHTTPServer 8080

Useful command on Terminal

You can use open command to open any application and any file.

open .            # Open current folder in the Finder.
open -a Firefox   # To open application. Use -a option
open ticket.pdf   # To open a file. It will open PDF in default application.

For Non-English speaker like me, it always find difficulties in pronouncing the word :) You can use say to know how to pronounce

say Hello World

View More information on Menu bar icons

Holding while clicking menu bar icon will give you additional menu items.

Hold Option ⌥ + Click Battery Menulet, it will show battery condition.

Have a nice day.

View comments

What I learned from other's shell scripts

I am big fan of shell scripts and so love to learn interesting stuff from other's shell scripts. Recently I came across the authy-ssh scripts which eases two-factor authentication for ssh servers. When I walk through scripts, I learned lot of cool things that I am going to share it with you.

Colors your echo

Lots of time, you want to color your echo output like green for success, red for failure, yellow for warning.

NORMAL=$(tput sgr0)
GREEN=$(tput setaf 2; tput bold)
YELLOW=$(tput setaf 3)
RED=$(tput setaf 1)

function red() {
    echo -e "$RED$*$NORMAL"
}

function green() {
    echo -e "$GREEN$*$NORMAL"
}

function yellow() {
    echo -e "$YELLOW$*$NORMAL"
}

# To print success
green "Task has been completed"

# To print error
red "The configuration file does not exist"

# To print warning
yellow "You have to use higher version."

It uses tput to set a colors and place the text and reset the color to normal. To know more about tput, refer prompt-color-using-tput

To print debug information

Print debug information only if debug flag is set.

function debug() {
    if [[ $DEBUG ]]
    then
        echo ">>> $*"
    fi
}

# For any debug message
debug "Trying to find config file"

Some Cool geeks give one line debug function

# From cool geeks at hacker news
function debug() { ((DEBUG)) && echo ">>> $*"; }
function debug() { [ "$DEBUG" ] && echo ">>> $*"; }

To check specific executable exists or not

OK=0
FAIL=1

function require_curl() {
    which curl &>/dev/null
    if [ $? -eq 0 ]
    then
      return $OK
    fi

    return $FAIL
}

It uses which command to find the path of curl executable. If it succeeds, then the executable exists, Otherwise not. The &>/dev/null puts both output stream and error stream to /dev/null (which means nothing printed on console).

Some Cool geeks suggest me that we can directly returns the which return code

# From cool geeks at hacker news
function require_curl() { which "curl" &>/dev/null; }
function require_curl() { which -s "curl"; }

To print usage of scripts

When I start writing shell scripts, I used echo commands to print the usage of the scripts. The echo commands becomes messy when we have large text for usage. Then I found cat command used to print usage.

cat << EOF

Usage: myscript <command> <arguments>

VERSION: 1.0

Available Commands

    install - Install package

    uninstall - Uninstall package

    update - Update package

    list - List packages

EOF

The << is called as here document. It takes string between two EOF.

User configured value vs Default value

Sometime, we want to use default value if user does not set the value.

URL=${URL:-http://localhost:8080}

It checks for URL environment variable. If not exists, then it is assigned to localhost.

To check the length of the string

if [ ${#authy_api_key} != 32 ]
then
  red "you have entered a wrong API key"
  return $FAIL
fi

The ${#VARIABLE_NAME} gives the length of the value of the variable.

To read inputs with timeout

READ_TIMEOUT=60
read -t "$READ_TIMEOUT" input

# if you do not want quotes, then escape it
input=$(sed "s/[;\`\"\$\' ]//g" <<< $input)

# For reading number, then you can escape other characters
input=$(sed 's/[^0-9]*//g' <<< $input)

To get directory name and file name

# To find base directory
APP_ROOT=`dirname "$0"`

# To find the file name
filename=`basename "$filepath"`

# To find the file name without extension
filename=`basename "$filepath" .html`

Happy Scripting and Have a nice day.

View comments

Use your browser as Notepad

We all uses notepad or sticky note or some editor to take immediate notes on something like code snippet, ideas, blog content, todo. Recently I came to know that we can use our browser just like notepad. The trick hacks around Data URI scheme and html contenteditable attribute.

All you need to do is type the following code into the browser's URL bar:

data:text/html, <html contenteditable>

It will make your page as editable just like notepad. If you want to save your content, do the usual browser save(CMD+S for OSX). It will save your content as html file. You can also bookmark above data url to make it easier.

After I share this post in Hacker News, I got cool new things from cool guys.

Editor with little bit styles by bichiliad

data:text/html, <html contenteditable><style>body {color: #333; width: 960px; margin: 0 auto; display: block; height: 100%; font-size: 36px; padding: 20px;}</style></html>
reply

Theme that changes the background color as you type by bgrins

data:text/html, <html><head><link href='http://fonts.googleapis.com/css?family=Open+Sans' rel='stylesheet' type='text/css'><style type="text/css"> html { font-family: "Open Sans" } * { -webkit-transition: all linear 1s; }</style><script>window.onload=function(){var e=false;var t=0;setInterval(function(){if(!e){t=Math.round(Math.max(0,t-Math.max(t/3,1)))}var n=(255-t*2).toString(16);document.body.style.backgroundColor="#ff"+n+""+n},1e3);var n=null;document.onkeydown=function(){t=Math.min(128,t+2);e=true;clearTimeout(n);n=setTimeout(function(){e=false},1500)}}</script></head><body contenteditable style="font-size:2rem;line-height:1.4;max-width:60rem;margin:0 auto;padding:4rem;">

I love this. Sublime Text Flavor with Ace by thinkxl

data:text/html,<title>DoJS</title><style type="text/css">#e{font-size: 16px; position:absolute;top:0;right:0;bottom:0;left:0;}</style><div id="e"></div><script src="http://d1n0x3qji82z53.cloudfront.net/src-min-noconflict/ace.js" type="text/javascript" charset="utf-8"></script><script>var e=ace.edit("e");e.setTheme("ace/theme/monokai");e.getSession().setMode("ace/mode/javascript");</script>

Web is awesome. Have a nice day.

View comments