Thursday, November 11, 2010

Automate Terminal.app ~/.login.sh Script on Login in OS X 10.6 to Mount TrueCrypt Volume, etc.

TrueCrypt is nice, but it doesn't currently have a way in OS X to automatically ask for just your password at login. I first wrote a simple Applescript in AppleScript Editor:
do script "/Applications/TrueCrypt.app/Contents/MacOS/TrueCrypt /path/to/volume/file/volume.tc /path/to/dir/my_encrypted_dir"
then File > Save As... > File Format: Application and saved to the Applications directory. Then I went to System Preferences -> Accounts -> (choose user) -> Login Items and added that new script (application) there, so it would start automatically after Login.

But, this pops up a UI that gives you the option to use password or key files. I only wanted to provide a password. I also thought it would be cool to have a shell script to run on OS X login in Terminal.app that I could keep in my home directory and modify at will with a text editor rather than AppleScript Editor.

So, I wrote the following AppleScript in AppleScript Editor and again used File > Save As... > File Format: Application to the Applications directory. Then went to System Preferences -> Accounts -> (choose user) -> Login Items and added that new script (application) there, so it would start automatically after Login. (I also removed the other one I'd created.)

tell application "System Events"
 set terminal_is_open to (exists process "Terminal")
end tell

tell application "Terminal"
 activate
 if terminal_is_open then
  tell application "System Events" to tell process "Terminal" to keystroke "n" using command down
 end if
 do script with command "~/.login.sh" in selected tab of the front window
end tell

Then I created a file ~/.login.sh containing:

#/usr/sh

# Things to run on login...
clear

# Mount encrypted volume
/Applications/TrueCrypt.app/Contents/MacOS/TrueCrypt -t -k "" --protect-hidden=no /path/to/volume/file/volume.tc /path/to/dir/my_encrypted_dir
Then I did:
chmod +x ~/.login.sh

Logout, login, and it should display a Terminal.app window prompting you for your password to the TrueCrypt volume. After that, I just leave the window open in case there is an error.

Note that to rename the TrueCrypt volume mount on the desktop, just rename it like any other file.

No comments: