Macのopenコマンドは便利

ターミナルからアプリケーションを起動させたい場合にめっちゃ便利なのがopenコマンド。ちなみにこれはMac独自のものなので他のunix系列では使えません。たぶん似たようなのがあると思いますが。

このコマンドの挙動を簡単に説明するならば「そのファイルをダブルクリックするのと同じ」といったところでしょうか(たぶん)。具体例を挙げると、

#image.jpegをプレビューで開く
open image.jpeg
#.jpegの画像を全部プレビューで開く
open *.jpeg
#directoryをFinderで開く
open directory

これと、「.」が現在のディレクトリを指すことを利用して

#現在いるディレクトリをFinderで開く
open .

htmlを編集していて便利なのがコレ

#fileをクロームで開く
open -a /Applications/Google\ Chrome.app file
#aliasしておくなど
alias chrome="open -a /Applications/Google\ Chrome.app"

「-a」オプションは開くアプリケーションを指定するのに使います。「-a」を指定しないとその拡張子に割り当てられたデフォルトのアプリケーションで開きます。

同一アプリケーションを複数個起動(?)させたいときは「-n」オプションを使います

#Eclipseを複数起動
open -n /Applications/eclipse/Eclipse.app 

私がたまに使うのはこんなところでしょうか、他にも使い道が沢山あるのでいろいろやってみるといいと思います。

MacでRails環境構築する手順

※インストールする際、参考にしたサイトのメモ書きみたいなもの

まずパッケージ管理ツールであるHomebrewをインストール
インストールする際のスクリプトは公式ページのをコピペ
Homebrew — OS X用パッケージマネージャー

$ ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
==> This script will install:
/usr/local/bin/brew
/usr/local/Library/...
/usr/local/share/man/man1/brew.1
==> The following directories will be made group writable:
/usr/local/.
==> The following directories will have their group set to admin:
/usr/local/.

Press RETURN to continue or any other key to abort
==> /usr/bin/sudo /bin/chmod g+rwx /usr/local/.
Password:
==> /usr/bin/sudo /usr/bin/chgrp admin /usr/local/.
==> /usr/bin/sudo /bin/mkdir /Library/Caches/Homebrew
==> /usr/bin/sudo /bin/chmod g+rwx /Library/Caches/Homebrew
==> Downloading and installing Homebrew...
remote: Counting objects: 230117, done.
remote: Compressing objects: 100% (60326/60326), done.
remote: Total 230117 (delta 168531), reused 230117 (delta 168531)
Receiving objects: 100% (230117/230117), 52.85 MiB | 354.00 KiB/s, done.
Resolving deltas: 100% (168531/168531), done.
From https://github.com/Homebrew/homebrew
 * [new branch]      master     -> origin/master
HEAD is now at 6484348 rakudo-star: update 2015.01 bottle.
==> Installation successful!
==> Next steps
Run `brew doctor` before you install anything
Run `brew help` to get started

次に、Run `brew doctor` before you install anythingとあるのでbrew doctorを実行し、問題ないようであれば/usr/local/binと/usr/local/sbinにパスを通す。私の環境では/etc/pathsに/usr/local/binが既に書いてあったので、/usr/local/sbinのみ.bash_profileに追加。その後、.bash_profileを再読み込みする。

ターミナルにて
$ brew doctor

.bash_profileにて
export PATH=/usr/local/sbin:$PATH

ターミナルにて(.bash_profileを再読み込み)
$ source .bash_profile


.bash_profileの話は以下のサイト

.bash_profile ? .bashrc ? いろいろあるけどこいつらなにもの? - Qiita


本当に正しい .bashrc と .bash_profile の使ひ分け - Qiita


次にHomebrewの動作確認がてらGitを入れる(結果はメモり忘れた)

$ brew install git

ここまでで参照したサイト:

Git をインストールする手順[ 2 ] – Homebrew からインストールする | memocarilog


Mac - homebrewとは何者か。仕組みについて調べてみた - Qiita

次にRubyをインストールする前に、Rubyが利用する「readline」と「OpensSSL」の2つのライブラリを先にインストールする

$ brew install readline
==> Downloading https://downloads.sf.net/project/machomebrew/Bottles/readline-6.3.8.yosemite.bottle.tar.gz
######################################################################## 100.0%
==> Pouring readline-6.3.8.yosemite.bottle.tar.gz
==> Caveats
This formula is keg-only, which means it was not symlinked into /usr/local.

Mac OS X provides similar software, and installing this software in
parallel can cause all kinds of trouble.

OS X provides the BSD libedit library, which shadows libreadline.
In order to prevent conflicts when programs look for libreadline we are
defaulting this GNU Readline installation to keg-only.

Generally there are no consequences of this for you. If you build your
own software and it requires this formula, you'll need to add to your
build variables:

    LDFLAGS:  -L/usr/local/opt/readline/lib
    CPPFLAGS: -I/usr/local/opt/readline/include

==> Summary
🍺  /usr/local/Cellar/readline/6.3.8: 40 files, 2.1M


$ brew install openssl
==> Downloading https://downloads.sf.net/project/machomebrew/Bottles/openssl-1.0.2.yosemite.bottle.tar.gz
######################################################################## 100.0%
==> Pouring openssl-1.0.2.yosemite.bottle.tar.gz
==> Caveats
A CA file has been bootstrapped using certificates from the system
keychain. To add additional certificates, place .pem files in
  /usr/local/etc/openssl/certs

and run
  /usr/local/opt/openssl/bin/c_rehash

This formula is keg-only, which means it was not symlinked into /usr/local.

Mac OS X already provides this software and installing another version in
parallel can cause all kinds of trouble.

Apple has deprecated use of OpenSSL in favor of its own TLS and crypto libraries

Generally there are no consequences of this for you. If you build your
own software and it requires this formula, you'll need to add to your
build variables:

    LDFLAGS:  -L/usr/local/opt/openssl/lib
    CPPFLAGS: -I/usr/local/opt/openssl/include

==> Summary
🍺  /usr/local/Cellar/openssl/1.0.2: 459 files, 18M

ちなみに"Mac OS X already provides this software and installing another version in parallel can cause all kinds of trouble."とあることからわかるようにOS Xには元からOpenSSLが入っています。readlineでのsimilar softwareが何を指しているのかはわかりません。

$ find /usr/bin -name openssl
/usr/bin/openssl

OpenSSLとreadlineでの

Generally there are no consequences of this for you. If you build your
own software and it requires this formula, you'll need to add to your
build variables:

    LDFLAGS:  -L/usr/local/opt/openssl/lib
    CPPFLAGS: -I/usr/local/opt/openssl/include

この部分の意味は、"ソフトウェアをビルドするとき、今インストールしたライブラリを必要とする場合、ビルドするときに指定する「ライブラリの指定」に「LDFLAGS」のパスを、「includeファイルの指定」に「CPPFLAGS」のパスを追加してください"みたいな感じです。

ビルドってなにって場合

仕事で使える魔法のLAMP(6):「ビルド」という作業は何を指しているのか - @IT



LinuxにPHPをインストールする方法は大きく2通りあると思います… - 人力検索はてな



一応インストールした二つのライブラリが参照できるように、リンクを貼っておく(/usr/local/binにシンボリックリンクを作る)
参考サイト:
積んメモ: keg-onlyなパッケージはシンボリックリンクが作られない

$ brew link readline --force
Linking /usr/local/Cellar/readline/6.3.8... 18 symlinks created
$ brew link openssl --force
Linking /usr/local/Cellar/openssl/1.0.2... 1510 symlinks created

ちなみにシンボリックリンクのを作る際、 * has multiple installed versionsと出た時の対応は以下のサイト参考。この段階で出るわけないけど、後学のため。

brew link を実行したら * has multiple installed versions と出たときの対応 - Programming log - Shindo200



次に、Rubyのバージョン管理のためrbenvをインストールする。rbenvについては以下のサイトを参考。
rbenvつかってrailsチュートリアルやってみよう! | niwatako$

$ brew install rbenv
==> Downloading https://github.com/sstephenson/rbenv/archive/v0.4.0.tar.gz
######################################################################## 100.0%
==> Caveats
To use Homebrew's directories rather than ~/.rbenv add to your profile:
  export RBENV_ROOT=/usr/local/var/rbenv

To enable shims and autocompletion add to your profile:
  if which rbenv > /dev/null; then eval "$(rbenv init -)"; fi
==> Summary
🍺  /usr/local/Cellar/rbenv/0.4.0: 31 files, 152K, built in 6 seconds

"To enable shims and autocompletion add to your profile: if which rbenv > /dev/null; then eval "$(rbenv init -)"; fi"とあるので、.bash_profileに追加

.bash_profileにて
if which rbenv > /dev/null; then eval "$(rbenv init -)"; fi

次にrbenvを使ってRubyをインストールするためのruby-buildをインストール

$ brew install ruby-build
==> Installing dependencies for ruby-build: autoconf, pkg-config
==> Installing ruby-build dependency: autoconf
==> Downloading https://downloads.sf.net/project/machomebrew/Bottles/autoconf-2.69.yosemite.bottle.1.tar.gz
######################################################################## 100.0%
==> Pouring autoconf-2.69.yosemite.bottle.1.tar.gz
🍺  /usr/local/Cellar/autoconf/2.69: 70 files, 3.1M
==> Installing ruby-build dependency: pkg-config
==> Downloading https://downloads.sf.net/project/machomebrew/Bottles/pkg-config-0.28.yosemite.bottle.2.tar.gz
######################################################################## 100.0%
==> Pouring pkg-config-0.28.yosemite.bottle.2.tar.gz
🍺  /usr/local/Cellar/pkg-config/0.28: 10 files, 612K
==> Installing ruby-build
==> Downloading https://github.com/sstephenson/ruby-build/archive/v20150130.tar.gz
######################################################################## 100.0%
==> ./install.sh
🍺  /usr/local/Cellar/ruby-build/20150130: 154 files, 656K, built in 5 seconds

これでRubyをインストールする準備が整っったので、Rubyを早速インストール、今回は2.0.0-p481と1.9.3-p194を入れた。

$ RUBY_CONFIGURE_OPTS="--with-readline-dir=`brew --prefix readline` --with-openssl-dir=`brew --prefix openssl`" rbenv install 2.0.0-p481
Downloading ruby-2.0.0-p481.tar.gz...
-> http://dqw8nmjcqpjn7.cloudfront.net/00dd3d72435eb77f2bd94537c1738e5219ca42b6d68df3d4f20c183f4bd12d0f
Installing ruby-2.0.0-p481...
Installed ruby-2.0.0-p481 to /Users/ユーザ名/.rbenv/versions/2.0.0-p481

$ CONFIGURE_OPTS="--with-readline-dir=/usr/local/opt --with-openssl-dir=/usr/local/opt" rbenv install 1.9.3-p194
Downloading yaml-0.1.6.tar.gz...
-> http://dqw8nmjcqpjn7.cloudfront.net/7da6971b4bd08a986dd2a61353bc422362bd0edcc67d7ebaac68c95f74182749
Installing yaml-0.1.6...
Installed yaml-0.1.6 to /Users/ユーザ名/.rbenv/versions/1.9.3-p194

Downloading ruby-1.9.3-p194.tar.gz...
-> http://dqw8nmjcqpjn7.cloudfront.net/46e2fa80be7efed51bd9cdc529d1fe22ebc7567ee0f91db4ab855438cf4bd8bb
Installing ruby-1.9.3-p194...
Installed ruby-1.9.3-p194 to /Users/ユーザ名/.rbenv/versions/1.9.3-p194

次に、使用するRubyのバージョンを変える

$ ruby -v
ruby 2.0.0p481 (2014-05-08 revision 45883) [universal.x86_64-darwin14]

$ rbenv version(現在使用しているrubyのバージョン)
system (set by /Users/ユーザ名/.rbenv/version)

$ which ruby (/usr/bin以下のrubyを使用しているものの、.rbenv/shims以下のシンボリックリンク(?)から参照していることがわかる)
/Users/ユーザ名/.rbenv/shims/ruby

$ rbenv versions(先ほど入れた2.0.0-p481が入ってることがわかる)
* system (set by /Users/ユーザ名/.rbenv/version)
  2.0.0-p481

$ rbenv global 2.0.0-p481(2.0.0の方に切り替える)

$ rbenv rehash(忘れずにrehash)

$ rbenv version(ちゃんと変わってることが確認でkリウ)
2.0.0-p481 (set by /Users/ユーザ名/.rbenv/version)

$ rbenv -v(rbenvのバージョン確認 ちなみに-vはversionでなくverbose)
rbenv 0.4.0

続きはまた今度かく

label要素のイベントとinput要素のイベント

  jQueryの勉強をしていた時に、以下の様なコードに遭遇し、labelのイベント処理と対応するinput要素のイベント処理をちょっと考えた話。( コードは説明のため大幅に簡略化してあります)

<form action="どっか" method="get">		
  <input type="checkbox" name="html" value="HTML" id="html">
  <label for="html">HTML</label>
</form>
$("label, input[type=\"checkbox\"]").click(function(){
  $("label").css("background","");
  $(":checked").each(function(){
    $("label[for=\""+$(this).attr("id")+"\"]").css("background-color","#CCC")
  });
});		

上記のコードは、チェックボックスにチェックを入れる時、対応するラベルの背景色を変えてわかりやすくする、という意図で書かれたものです。下の画像の左がクリック前、右がクリック後です。
f:id:uniunimac:20150219142820p:plain

このコードで$("label, input[type=\"checkbox\"]")としている部分を見て、labelとinput要素のイベント処理は独立してるから2つともにイベントつけたのかなと思い、それならinputの方だけを消せば「ラベルを押した場合」だけ色がつくのかなと確認してみると
f:id:uniunimac:20150219150900p:plain
一回目のクリック(画像中央)でチェックボックスにチェックが入るも、色がつかない。二回目のクリック(画像右)で色がついたもののチェックがはずれました。

  なんでこんなことになったのか考えてみると、たぶんlabel要素のイベント処理は以下のように進むからだという結論に。

  1. labelにつけたユーザ定義のクリックイベント
  2. labelのデフォルトクリックイベント
  3. input要素のイベント(デフォルトとユーザ定義)

  上のコードでは、一回目のクリック時にまずlabel要素に定義したクリックイベントが起きる、この時まだチェックボックスのchecked属性がcheckedになっていない。よって$(":checked")には今クリックしたlabel要素は含まれないので、背景色が変わらない。次にlabel要素のデフォルトイベントで、inputのイベント処理が呼ばれる。ここでチェックボックスのchecked属性がcheckedになる。
  次に、二回目のクリック時、label要素に定義したクリックイベントが起きる段階では、まだchecked属性がcheckedのままなので、$(":checked")に今クリックしたlabel要素が含まれ、背景色が変わる。次にlabel要素のデフォルトイベントで、inputのイベント処理が呼ばれる。ここでチェックボックスのchecked属性が外され、チェックしてない状態になる。

  たぶんこんな感じのことが起きてるんだろうなと。ちなみにlabelがデフォルトの動作でinputのイベントを呼ぶことは、labelにつけたイベントの最後にデフォルトイベント抑止のreturn false;をつけて、input要素に適当なイベントをつけてみるとわかります。それと上記のlabel要素のイベント処理の進行で、「 input要素のイベント(デフォルトとユーザ定義)」と若干曖昧な表記になっているのは、最初はユーザ定義→デフォルトだと思っていたんですが、以下の様なコードを実行してみると

$("input[type=\"checkbox\"]").click(function(){
  alert($(":checked").attr("id"));
  return false;
});

まずチェックボックスにチェックが入り、alertでクリックしたinput要素のidが表示され、そしてチェックが消えます。つまりユーザ定義のイベント処理の前にchecked="checked"になり、そのあとまたデフォルトの処理で"checked"じゃなくなったことがわかります。このことからreturn false;はデフォルトのイベントを呼ばなくするのではなく、デフォルトのイベントの挙動を変えていることもわかります。

  要するに「クリックする→ユーザ定義→デフォルト」だと最初にチェックが入るのはおかしいので(デフォルトでチェックが入るとすると)、ユーザ定義の前後にチェックに関する処理が入ってるのかなと思ったためです。

Vagrantのmountが失敗するやつ

起こった環境はこんな感じ、

$ sw_vers   
ProductName:	Mac OS X
ProductVersion:	10.10.2
BuildVersion:	14C109

$ vagrant --version
Vagrant 1.7.2
$ VBoxManage --version
4.3.22r98236

$ vagrant init chef/centos-6.5
$ vagrant up


==> default: Mounting shared folders...で

Failed to mount folders in Linux guest. This is usually because
the "vboxsf" file system is not available. Please verify that
the guest additions are properly installed in the guest and
can work properly. The command attempted was:

mount -t vboxsf -o uid=`id -u vagrant`,gid=`getent group vagrant | cut -d: -f3` vagrant /vagrant
mount -t vboxsf -o uid=`id -u vagrant`,gid=`id -g vagrant` vagrant /vagrant

The error output from the last command was:

/sbin/mount.vboxsf: mounting failed with the error: No such device

てな感じにエラーになった

とりあえず下にあげている参考サイトを読んだところvbguestプラグイン入れてると起こるのかなと思い、プラグイン一覧を見てみると

$vagrant plugin list
vagrant-share (1.1.3, system)

あれ?入ってない。そもそもそんなの入れてなかった。

次にvbox をリビルドすると云々という話が多々あったので、リビルドしてみる

[vagrant@localhost ~]$ sudo /etc/init.d/vboxadd setup
Removing existing VirtualBox non-DKMS kernel modules       [  OK  ]
Building the VirtualBox Guest Additions kernel modules
The headers for the current running kernel were not found. If the following
module compilation fails then this could be the reason.
The missing package can be probably installed with
yum install kernel-devel-2.6.32-504.8.1.el6.x86_64

Building the main Guest Additions module                   [失敗]
(Look at /var/log/vboxadd-install.log to find out what went wrong)
Doing non-kernel setup of the Guest Additions              [  OK  ]

The missing package can be probably installed with yum install kernel-devel-2.6.32-504.8.1.el6.x86_64.とあったので、

[vagrant@localhost ~]$ sudo yum install kernel-devel-2.6.32-504.8.1.el6.x86_64 -y
〜インストール完了したあと〜
[vagrant@localhost ~]$ sudo /etc/init.d/vboxadd setup
Removing existing VirtualBox non-DKMS kernel modules       [  OK  ]
Building the VirtualBox Guest Additions kernel modules
Building the main Guest Additions module                   [  OK  ]
Building the shared folder support module                  [  OK  ]
Building the OpenGL support module                         [  OK  ]
Doing non-kernel setup of the Guest Additions              [  OK  ]
Starting the VirtualBox Guest Additions                    [  OK  ]

うまくいったっぽい? 期待に胸膨らませながらvagrantを再起動してみる

==> default: Mounting shared folders...
    default: /vagrant => /Users/ユーザ名/MyVagrant/mycentos

無事エラー無しで起動、ssh接続してシェアされてるか確かめてみる

[vagrant@localhost ~]$ cd /
[vagrant@localhost /]$ ls
bin   dev  home  lib64       media  opt   root  selinux  sys  usr      var
boot  etc  lib   lost+found  mnt    proc  sbin  srv      tmp  vagrant
[vagrant@localhost /]$ cd vagrant
[vagrant@localhost vagrant]$ ls
Vagrantfile
[vagrant@localhost vagrant]$ touch uniuni.txt
[vagrant@localhost vagrant]$ ls
Vagrantfile  uniuni.txt
[vagrant@localhost vagrant]$
---------------Macの方から見てみると(Vagrantfile置いてある場所にて)--------------
$ ls
Vagrantfile	uniuni.txt 

無事うまくいきました。結局、Building the main Guest Additions moduleがうまくいってないからだったんですが、なんでそうなったのかがまだ良くわかんないので引き続き調べてみます。

参考サイト:

vagrant-vbguestプラグインがGuestAdditionsを無効にしてしまう - Qiita


VirtualBox - vagrant up 時に shared folder の mount でエラーが発生する - Qiita

Macに入れてあったMySQLをアンインストールした話

  MBAを買って以来全く触ってなかったMBPのファイル整理をした際、昔入れたMySQLをアンインストールしたので、そのメモ書き。たしか公式ホームページ
   MySQL :: 世界でもっとも普及している、オープン ソース データベース
からダウンロードしたものだったと思う。あの頃はまだHomebrewもMAMPも知らず、四苦八苦しながら環境設定した気がします。

以下は手順と、そもそもそのファイル(フォルダ)が何なのかの簡単な説明(というかメモ)

[注意] OSX( とかUNIX等 )に関してはすこーしかじった程度なので以下の記述は間違っている可能性があります。もしこれの通りに操作を行う場合は自己責任でお願いします。

1:/usr/local以下にある「mysql」「mysql-5.6.20-osx10.8-x86_64」を消去
  ちなみにこれがmysqlの本体になりますね

2:/Library/StartupItems以下にある「MySQLCOM」を消去

/Library/StartupItems

  このディレクトリには、実行時に自動的に実行させたいコマンドを書いたコマンド起動用のスクリプトファイルを置くところです。MySQLCOMは中身を覗く前に消しちゃったので推測ですが、たぶんMac起動時に一緒にMySQLを起動させる場合の処理とかが書いてあるんだと思います。中身は単なるシェルスクリプトだと思うので詳しくは各自見てください。
  
  このページがわかりやすいかも
  OSX - OS X で起動時に任意のコマンドを実行する - Qiita


3:Library/PreferencePanes以下にある「MySQL.prefPane」を 消去

 ・/Library/PreferencePanes  
 ・/System/Library/PreferencePanes
 ・/Users/ユーザ名/Library/PreferencePanes

  上記の3つのフォルダにはシステム環境設定にある環境設定パネルのモジュールが入っています。つまり◯◯.prefPaneがシステム環境設定に表示されてるプログラムの実体になります。これを消したいときは同じディレクトリにいってrm -rfするか環境設定パネル上でControl+右クリックをすれば大丈夫です。
  
  余談ですが、Preferenceはソフトウェアの設定情報や設定画面、設定ファイルなどを指し、Paneはウインドー内部を機能別にわけた領域を意味します。

4:パッケージの情報を消す
  Macではアプリケーションを配布する形式に「.app形式」「.pkg形式」の2つがあります。前者は該当アプリケーションをゴミ箱にポイーすれば大丈夫なんですが、後者はファイルがいくつかの場所に分散するので、ちょっとめんどくさい。(上記の1、2、3の処理がまさに分散してるファイルを片っ端から消す作業です、たぶん)
 
  パッケージの話に関してはこのページがわかりやすいかも
  orange/note: Mac OS Xのパッケージ管理
 
  話が若干それましたが、.pkg形式ではインストールした情報が残っちゃうのでそれも消す必要があります、消し方は以下の様になります。

sudo pkgutil --forget 消したいパッケージ名

また、mysqlに関するパッケージは 以下のコマンドで分かります。

pkgutil --pkgs | grep -i mysql 

それと一番大切なことですが、パッケージに含まれるファイルの確認方法は

pkgutil --files パッケージID

とすればすぐにわかります。自分はこのコマンドをすっかり忘れてて

sudo find / -name *mysql*

のように検索して人力で探して消去してました、アホですね。

以上でMySQLのアンインストール作業は多分終わりです。ここらへんの環境設定はあまり詳しくないので、なんか間違ってるかもしれません。ミスが見つかったら修正します。