생각하는 족족 고.따.구 냐..

Posted
Filed under About Knowledge/OS_Mac
~/Library 폴더로 이동해서 KeyBindings 폴더를 추가
    ~/Library/KeyBindings 폴더에 DefaultkeyBinding.dict 파일 생성
    DefaultkeyBinding.dict 파일에 추가

    {
        "₩" = ("insertText:", "`");
    }


or



cd ~/Library
mkdir KeyBindings
touch DefaultkeyBinding.dict
echo '{"₩" = ("insertText:", "`");}' > DefaultkeyBinding.dict
2018/09/04 10:07 2018/09/04 10:07
Posted
Filed under About Knowledge/OS_Mac
If you're an old-time Unix or Linux user like me, you know that one of the best parts of Mac OS X is that it includes a full command line world based on NetBSD under the hood. In fact, I always have the Terminal app open and it's a rare day that I'm not writing scripts or otherwise experimenting on the command line, even as my screen is covered with pretty graphical user interfaces and drag-and-drop features.

You can get any of a zillion books on Unix and Linux to learn more about "vi", "emacs", "ls", "grep", "find", etc, including my O'Reilly title "Learning Unix for Mac OS X ," but I want to talk more about what Apple has added to the standard list of commands available, because some of them are darn cool.

The problem is there's no central list of these command line additions and some are still included even though they've been obsoleted years ago. You can try something fancy while parsing the man pages like this:

$ grep .TH * | grep -E "(Apple Computer|Mac OS X)" | cut -d: -f1 | fmt
DeRez.1 Rez.1 RezDet.1 ant.1 certtool.1 cmpdylib.1 crlrefresh.1
gprof
.1 ld_classic.1 lipo.1 mig.1 migcom.1 pbcopy.1 redo_prebinding.1
segedit
.1 size.1 srm.1 strings.1 strip.1 tops.1


But the fact is it misses some of the most helpful additions from the team at 1 Infinity Way. All the more reason to read this article, right! So let's jump in …


Open

It's not the most exciting, but it's darn useful to be able to use the command line to quickly zip through lots of subdirectories and find files of various types buried in your system. Ready to see what's inside using the program that created the file? Simply specify "open _filename_" and your Mac will automatically show that file in the appropriate app.

For example:

$ open *png


will open up all the PNG graphic files in the current directory using whatever app you have specified as the default. Or, even more useful, "open http://www.nrp.org/" will feed that URL to your favorite Web browser.

Don't like the default app? "Open" has a ton of options to make it even more useful, including the ability to specify what app should be used to open the file, but here are the few that I think are most helpful:

    -a — Opens with the specified application.

    -e — Opens with TextEdit.

    -t — Opens with default text editor.

    -f — Reads input from standard input and opens with TextEdit.

    -R, -reveal — Selects in the Finder instead of opening.

    -g, -background — Does not bring the application to the foreground.


To put these together, I have written shell scripts that extract a URL from a text file, then opens it in Google Chrome (my default browser) in the background, rather than force my attention away from the Terminal window. Like this:

$ open -a chrome -g $URL


Very helpful when you need to jump back and forth between the GUI apps and Terminal.


2. PMSET

It may not seem very glamorous, but there's a lot you can do to fine-tune your Mac's behavior by spending some time digging into the "pmset" power management utility. I don't use it that frequently, but particularly with always-on servers, it's great to be able to fine-tune how it works.

You'll immediately see all of its cool options after using the '-g' flag to get your own power settings:

$ pmset -g
Active Profiles:
Battery Power           -1*
AC
Power                -1
Currently in use:
 halfdim              
1
 sms                  
1
 panicrestart        
15
 gpuswitch            
2
 hibernatefile        
/var/vm/sleepimage
 disksleep            
10
 sleep                
90 (sleep prevented by )
 hibernatemode        
3
 ttyskeepawake        
1
 displaysleep        
5
 acwake              
0
 lidwake              
1


Here's where you can change settings that are otherwise crazy hard to find in the System Preferences windows, like whether you want your MacBook to wake up when you open the lid. Or, perhaps you'd like to have the computer wake up when it's plugged into the wall, even if it's closed? That's helpful if you like to leave a laptop docked, of course.

To change settings, specify the appropriate verb and add the parameter you want. For example, to switch to the display powering down after five idle minutes:

$ pmset -b displaysleep 5


The pmset command can also work with an attached uninterruptible power supply (UPS) if compatible. In that instance, you can specify details like how long after a power fail the system should fully shut down:

$ pmset -u haltlevel 20 haltremain 15


The above specifies that the system should perform an emergency shutdown if either the battery drains to less than 20% or the UPS reports less than 15min of battery remains. A bit more fancy than I use on a daily basis, but for a server, very helpful.

One more really neat example of pmset to whet your appetite for this powerful command: The follow command specifies that the Mac should wake up every morning at 7am:

$ pmset repeat wakeorpoweron MTWRF 7:00:00


Sure you can spend hours figuring out the fancy graphical way to set up the power management on your Mac server or laptop for your specific needs, but isn't this easier?


3. Size

You've probably read about how Mac applications include both Intel and universal binaries, but did you ever wonder how much space they take up? Once you know a bit about how Mac OS X actually uses a subfolder organization for applications on your Mac, you'll find you can use the "size" command to get more information about these than a regular Linux command.

"Pages," a part of the Apple iWorks suite, has its actual executable code located in a folder called "Contents", within a subfolder called "MacOS". Knowing that, here's how the program itself breaks down:

$ size /Applications/iWorks/Pages.app/Contents/MacOS/Pages 
__TEXT  __DATA  __OBJC  others  dec     hex
3432448 102400  262144  159744  3956736 3c6000  /Applications/iWorks/Pages.app/Contents/MacOS/Pages (for architecture ppc7400)
3428352 98304   290816  163840  3981312 3cc000  /Applications/iWorks/Pages.app/Contents/MacOS/Pages (for architecture i386)


At this point in 2012 I have to wonder why I still have PowerPC binaries, but fortunately disk space has become cheap, so while it's taking up space on my 500GB drive, it's not much. In fact, it's summarized in the "dec" column, meaning that the PPC and Intel versions each take up 3.95 MB. That breaks down into text, data, object code, and "other" as displayed.


4. Textutil

Leaving administrative tasks behind, it's an unfortunate reality of working on the command line that line endings and other character encoding sequences can get messed up as you travel from one operating system to another. If you've ever seen ^M on the end of each line of a text file, you know what I mean.

Fortunately, Apple's included a surprisingly helpful utility that can clean up a lot of these messes: textutil. Easiest way to learn about it is to simply invoke it with no arguments, which produces a raft of options.

Let's start by using it to peek into files, however. Here's what it can tell us about a Word .doc file:

$ textutil -info Column-94.doc
File:  Column-94.doc
 
Type:  Word format
 
Size:  23040 bytes
 
Length:  3197 characters
 
Created:  2012-04-13 14:11:00 +0000
 
Last Modified:  2012-04-13 14:11:00 +0000
 
Contents:  PUBLICATION: THE DAILY CAMERA...


Way more interesting is that it can also actually convert files from one format to another. For example, let's extract all the text from the above Microsoft Word document and save it as a text file, suitable for further command-line manipulation. Easy:

$ textutil -convert txt Column-94.doc


The new '.txt' file is saved as Column-94.txt. Using this, we can quickly use command line tools like "grep" to search through Word archives without hitting all the weird unprintable characters and gobbledygook of the Word file formats:

for file in *doc
do
   textutil
-convert txt $file
done
grep _string we seek_
*txt


Fast and easy. The full set of formats suggests even more uses for this command: txt, html, rtf, rtfd, doc, docx, wordml, odt, or webarchive.

One more example: convert the output of "ls" into an HTML page:

$ ls -C | textutil -convert html -stdin


In this case the result is saved as "out.html". I'll let you figure out additional uses for this handy little utility.


5. pbcopy | pbpaste

I'll end with two commands that I use quite a bit in my scripts: pbcopy and pbpaste. They let you interact with the Mac clipboard, the place where things are saved when you use the File -> Copy and File -> Paste commands within GUI apps like Pages and TextEdit.

Why is that useful? Because if you're going to select and copy the output of a command line interaction, then paste it into an email message or document anyway, why not save the selection dragging and keystrokes?

Here's how I use it:

$ ls -C | pbcopy


Now the result of the "ls" command is in the Clipboard, so I can switch to Gmail and with a Cmd-V paste it directly into the message I'm composing.

If you want to go in the other direction, you could figure out how many words are in the current Clipboard selection by using something like this:

$ pbpaste | wc -w
   
223


That's a lot. Wonder what it is. Hmm ...

[code]$ pbpaste | head -1[code]

pbcopy takes the standard input and places it in the specified pasteboard. If no pasteboard is specified, the general pasteboard will be

Ah, that's right, it's a section from the pbcopy main page itself.
2013/04/04 20:46 2013/04/04 20:46
Posted
Filed under About Knowledge/OS_Mac

Prevent Screen Savers and Sleep with “caffeinate”

New to OS X Mountain Lion, caffeinate is like a command line version of everyones favorite Caffeine utility. Usage is simple, with caffeinate running the Mac will not sleep, and screen savers will not activate. At it’s simplest, it can be run alone, but it’s probably best used with a time limit attached to it like so:

caffeinate -t 3600

The -t flag specifies the time in seconds, the example above runs caffeinate for an hour.

Extract PKG Files with “pkgutil”

Need to grab a file out of a .pkg file? Maybe you want to see what’s inside of a pkg without installing it? No sweat, pkgutil does the job:

pkgutil --expand sample.pkg ~/Desktop/

This will dump the entire pkg contents into the specified directory, without installing it.

Use “purge” to Free Up Memory

The purge command forcibly flushes the disk and memory caches, having an effect similar to when you reboot a Mac. Though some say that purge only offers a placebo effect, it absolutely does work to send system memory from the “Inactive” category back to the freely available RAM, and in situations where you are running low on real memory, it can provide a speed boost.

Using purge is simple, type the following at a command prompt:

purge

Wait a minute or so for changes to take effect, the process is usually much faster on Macs with SSD drives.

Launch Multiple Instances of Apps with “open”

You may already know that you can open applications in the OS X GUI from the command line with the ‘open’ command, but did you know that you can run multiple instances of apps by attaching the -n flag to the open command? It’s easy to use, here’s all you have to do:

open -n /Applications/Safari.app/

The example runs another instance of Safari. Change the app name accordingly, and don’t forget to include the .app extension.

Updating OS X without the App Store

Want to install system software and updates without bothering with the Mac App Store? You can do that directly from the command line instead with the help of the softwareupdate command. To install every update that is available, just run the following:

sudo softwareupdate -i -a

You can read more about softwareupdate command here, it has been bundled in OS X for years and works the same regardless of which version you’re using.

List Everything You’ve Ever Downloaded

We’ve all been there; you downloaded something a while ago from a domain you sort of remember, but you can’t quite remember what or from where. You’re in luck, because Quarantine Services keeps a database of everything that has ever been downloaded, and you can query that database to find what you were looking for. Use the sqlite3 command as follows to see everything:

sqlite3 ~/Library/Preferences/com.apple.LaunchServices.QuarantineEventsV* 'select LSQuarantineDataURLString from LSQuarantineEvent' |more

Of course you can also delete that list if the existence bothers you.

Hide Files or Folders from Finder with “chflags”

Got a secret file or folder you want to keep hidden from the Finder? Use chflags to turn any file invisible from the OS X GUI file system, it works the same whether you’re pointing it at a file or a directory:

chflags hidden /path/to/file/or/folder/

Lucky (or unlucky) for us command line folks, the file will still be visible with ls, but it will remain hidden in the Finder until the “nohidden” flag is attached like so:

chflags nohidden /path/to/unhide/

Changes are immediate in either event.

Automatically Type Long Paths with a Drag & Drop

Did you know you can drag and drop any file from the Finder into the command line and the entire path to that file will be automatically printed? This isn’t exclusively a command line tip, but it’s so useful that it has to be included. This is probably best used in conjunction with a command to prefix the path, like so:

sudo vi (drag file here to print the full path)

This works anywhere in the command line, even when you’re already in an app.

Create a Password Protected Zip Archive

If you’re sending a file through an unsecured medium or hosting it publicly, yet want to provide some level of protection, you can create a password protected zip archive with the -e flag:

zip -e protected.zip /file/to/protect/

Without the -e flag you’ll just be creating a standard zip file without a password.

2013/04/04 20:45 2013/04/04 20:45
Posted
Filed under About Knowledge/OS_Mac

In this short tutorial I document how I installed and configured Nginx on my MacBook running Mac OSX Leopard. Of course, I can’t possibly guarantee that the exact same set of steps will work for you, but it may help you troubleshoot any problems that you might have as you get started with this fantastic little piece of software.

If you haven’t yet discovered Nginx you’re in for a treat. The benefits of using a web server like Nginx over its competitors have already been thoroughly covered elsewhere, so I won’t really address that topic here. However I will say that I’ve been using the software for about 6 months now and it’s been a real pleasure to work with. It’s extremely fast, dependable, and easy to configure. Previous to working with Nginx I’ve only worked with two other web servers — Apache and LightTPD — and Nginx is by far my favorite. If you’re curious to know how Apache and Nginx compare performance-wise, the hard work has already been done for you. Let’s get started.

For the purposes of this tutorial we’ll be using the excellent MacPorts package manager for Mac OSX. Of course we could just compile our own Nginx from source, but the MacPorts repositories are kept very up-to-date so there’s really no need. If you haven’t already done so, head on over to macports.org and grab yourself a copy of the MacPorts disk image from the downloads page. The installer is very self explanatory and should complete in just a few minutes.

Once you’ve installed MacPorts, crack open a terminal window (found in /Applications/Utilities/Terminal.app) and type in the following command to make sure your MacPorts installation is the latest and greatest.

$ sudo port -d selfupdate $ port version Version: 1.710

Now let’s go ahead and install the Nginx port.

$ sudo port install nginx ---> Fetching nginx ... ---> Cleaning nginx

Let us pause and marvel at the beauty that is MacPorts. Whenever I type a little one-liner like that, I can’t help but think of all the poor saps who have come before me and made all the hard stuff about installing software and their dependencies extremely easy. In fact, let us take a second to remember just how spoiled we are while our software is installing. :)

Back? Great! Let’s make sure it installed correctly.

$ which nginx /opt/local/sbin/nginx $ nginx -v nginx version: nginx/0.7.59

During the installation process, there was a small section that looked something like this:

###########################################################
# A startup item has been generated that will aid in
# starting nginx with launchd. It is disabled
# by default. Execute the following command to start it,
# and to cause it to launch at startup:
#
# sudo launchctl load -w /Library/LaunchDaemons/org.macports.nginx.plist
###########################################################

What’s all this? It basically says that if we want Nginx to start up automatically when we start up our machine, we can do so using launchd (see this Developer Connection article from Apple for more information on launchd). I’m going to leave this step out at this point, simply because I prefer to not have Nginx constantly running on my machine (I’ll show you at the end of this article how to start and stop the Nginx server daemon at will). However, if you prefer to not have to manually start Nginx when you want to use it, go ahead and issue the following command:

$ sudo launchctl load -w /Library/LaunchDaemons/org.macports.nginx.plist

Now that we’ve got Nginx installed the way we want, it’s time to configure the thing. If we check out the Nginx Portfile, we can see that all of our configuration files were installed into /opt/local/etc/nginx (hint: look for the nginx_confdir directive). If you don’t like looking at Portfiles, just ignore that last bit and trust me. Let’s have a look inside the directory and see what’s there.

$ cd /opt/local/etc/nginx $ ls -1 fastcgi_params.example koi-utf koi-win mime.types.example nginx.conf.example win-utf

We can ignore the koi-utf, koi-win, and win-utf files for now. These files contain various character maps. The fastcgi_params.example file is only needed if we’re going to be setting up FastCGI, which is outside the scope of this particular article. The two files that we’re interested in at this point are the mime.types.example and nginx.conf.example files.

The mime.types.example file contains a map of Internet media types (more commonly referred to as MIME types) to various file extensions that represent files of that type. The defaults are pretty good, so we’ll just copy this file for now. If you need to register any custom MIME types later, you’ll know where to look.

$ sudo cp mime.types.example mime.types

Now on to the main Nginx configuration file. First, copy the example.

$ sudo cp nginx.conf.example nginx.conf

Now I’ll do a quick overview of the main configuration file and point out the relevant configuration directives. The example file only has the bare minimum enabled, but it’s already enough to start the server. Open up nginx.conf in your favorite text editor. If we strip out all the commented lines (which I only do here for conciseness in writing), we’ll get something close to the following:

worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; server { listen 80; server_name localhost; location / { root share/nginx/html; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root share/nginx/html; } } }

This file reads something like this: Nginx is configured to start up one worker process that may accept up to 1024 connections at once. In the http daemon configuration section, we include our mime.types directives and set a default type in case it cannot be automatically determined. Then we set up a server configuration. The concept of a “server” in Nginx is more or less equal to that of a “virtual host” under Apache. We tell our server to bind to port 80 on localhost and the root directory of our site. Then we tell this server what to do if it encounters an error.

Note that the file paths in the default configuration file are relative to /opt/local because that’s the directory we installed Nginx to. So the path share/nginx/html will be expanded to /opt/local/share/nginx/html. If we want to have our web root be somewhere else, like /var/www for example, just use the full path instead of relative paths.

Of course, that was a very high-level overview of the directives in this file. A more detailed description of all Nginx configuration directives can be found on the Nginx wiki.

So, let’s start up the server.

$ sudo nginx $ ps ax | grep nginx 69033 ?? Ss 0:00.00 nginx: master process nginx 69034 ?? S 0:00.00 nginx: worker process

We can see that Nginx started up one master process and one worker. If you navigate to http://localhost/, you should see a welcome message from Nginx.

Awesome! You’re now running Nginx. When the daemon is running, it stores its pid in a file at /opt/local/var/run/nginx/nginx.pid. So to stop the daemon, we can use the following command:

$ sudo kill `cat /opt/local/var/run/nginx/nginx.pid`
2012/08/20 11:56 2012/08/20 11:56