Mac Goodness (3)

Follow-up to Mac Goodness from the NISOx blog.

#15 More Terminal Bliss: Killing Terminal’s Quit

As should be clear from my other entries I depend on the Mac Terminal program extensively, often having 20 or more windows/tabs open at once. Then you can understand the horror when you accidentally hit CMD-Q, and all those precious Shell sessions die!

The solution was to remove the CMD-Q shortcut, with this gem:

defaults write NSUserKeyEquivalents -dict-add "Quit Terminal" nil

Mac Goodness (2)

Follow-up to Mac Goodness from the NISOx blog.

14. Maximising Terminal Bliss in Mavericks.

For some reason with the Mavericks update, the terminal now copies text to the clipboard with the format of your current terminal font/colors, etc. This is absurd, because the terminal is text, plain and simple. Moreover, I often use the terminal as a clipboard, pasting in some text to get rid of some formatting.

Thankfully I found this defaults mod to change Terminal’s behaviour, so that when you copy to the clipboard all that goes is the text and no formatting:

defaults write CopyAttributesProfile

Apple Notes Backup

Apple products are amazing when they work as intended. They are so well designed that they become integrated into your life and then you become dependent upon them. So when they fail to work it is source of enormous frustration.

Having lost the ability to sync Notes when iOS 7 came out (using my not-newest iphone 4S), when Notes functionality returned I suddenly had growing number of duplicate copies of essential (i.e. most frequently accessed & edited) note items. I eventually figured out some were “On My Mac”, but I couldn’t figure out what I was missing in the other versions until I could run diff on the command line, or ediff in emacs. Here is the solution I found…

  1. Prepare Notes. The back-up proceedure described next saves each note by its title, and notes with identical titles are over-written. Of course the source of the trouble is the duplicates, hence first carefully find duplicates and give them unique names (e.g. with the last modification time, as shown in Apple Notes).
  2. Backup your Notes with Notes Exporter, a Mac application written to complement a Notes app Write. Notes Exporter creates a directory filled with text files, one per note. (Note, previously I had recommended Notes Export for HTML export, but this is now (March 2016) no longer available.)
  3. Convert backed up HTML files to TXT (or other formats, like RTF). I never knew about textutil, a Mac utility that converts between document file formats. Run it as
    textutil -convert txt *.html
  4. Then run diff between the duplicates and see what’s changed. In my case, may of the duplicates were just that, exact duplicates, but some reflected changes that I would have lost.

Hope this helps someone!

Make Word act like Emacs

Follow-up to Mac Goodness from the NISOx blog (formerly Neuroimaging Statistics Tips & Tools)

I’ve gone on about how great it is to change keyboard shortcuts with System Preferences -> Keyboards, but not all Word commands are accessible through that facility. In particular, it is possible to make Word respond to Emacs editing commands!

In Word, go to Tools -> Customize Keyboard… . Under Categories, select “All Commands” to show everything. In the Commands box find a command, and then add the corresponding key as follows:

  • Command StartOfLine – Shortcut Control+A
  • Command EndOfLine – Shortcut Control+E
  • Command EditCut – Shortcut Control+W
  • Command EditPaste – Shortcut Control+Y
  • Command EditFind – Shortcut Control+S

Mac Tweaks

This is just a rambling list of the tweaks I’ve made to my Mac, mostly to help me remember them if my Mac ever dies, but others might find them useful too.

Disable Spotlight arithmetic

Due to editorial work, I’m constantly using Spotlight to find filenames consisting of numbers like 2010-0021, for which Spotlight helpfully returns the top answer as 1989 (I prefer to use bc on the command line for such arithmetic). Since this is the top answer, if you press return it opens the Calculator program instead of the file you wanted. (ug).

Instead, you can just tell Spotlight not to do any such arithmetic by using this command in the terminal:

defaults write CalculationEnabled NO

You need to kill Spotlight and have it restart to take effect. (While you’re supposed to be able to sudo killall ... to get it to restart, it didn’t work for me, and I just logged out and logged back in).

Force Preview to print at 100% (no scaling) by default

By default, when you print a document in Preview it will ‘scale to fit’. While this seems like a sensible thing, it bases the default scaling on the full size of what you’re printing and the printable area of the paper used. While this is sensible for a JPEG photo, it is idiotic for PDF document.

Finally I discovered that this behavior is easy to change. Just issue this command in a Terminal window

defaults write PVImagePrintingScaleMode 0

There is only one down side: If you then change “Images per page” to 2 or greater, the scaling is 100% and the image will be cropped. You just need to select “Scale to fit” (the previous default) and you’ll get the anticipated (appropriately reduced) result.

Prevent tar from saving extended information (resource fork) in ._ files

Set this in your environment:

Mac Goodness

Essential tips to increase my Mac love

Mac is the unification of Unix and an amazingly elegant user interface. Still, there are some keyboard-centric tricks that are not well documented. These are the Mac tips/tricks that bring me happiness.

  1. Change directory from file selection dialog box: Press ~ to pop-up a text box to enter a filepath.
  2. Cycle through an application’s windows with the same shortcut, CMD-~.
  3. I never, never use that yellow minimize button. Instead, I always hide with CMD-h.
  4. If you’re an emacs user, you know the vital importance of the control key. Thus, I’ve used System Preferences -> Keyboard -> Keyboard -> Modifier Keys to remap CAPS LOCK to be an additional CONTROL key. Magic!
    Now if I could only remap the stupid UK-keyboard’s plus/minus/section(!) key to be ESCAPE. Sigh.
  5. Add those missing keyboard short cuts with System Preferences -> Keyboard -> Keyboard Shortcuts. Some of my favorites
    • MS Office apps: CMD-SHIFT-V for Paste Special…
    • MS Office Powerpoint: CTRL-= for Subscript, CTRL-+ for Superscript, though first you need to add these as menu items with View -> Customise Toolbars and Menus; it doesn’t matter which menu you add them to (though Format is the obvious one).
    • MS Office aps: Auto Correct common LaTeX notation into symbols. One time, type the symbol you want (e.g. β), copy it. Under Tools->Autocorrect, enter $beta$ in the “Replace:” field, and paste the symbol into the “With:” field… et voila! The next type you type ”$beta$” it will automagically be replaced with a ”β”. Add other commonly used symbols as needed. (This also works on PC Office apps).
    • Terminal Program: CMD-LEFT and CMD-RIGHT for Select Next Tab, Select Previous Tab. (And, if you haven’t discovered the magic of Tabs in Terminal program, you’re just a CMD-t away from bliss.)
  6. Having trouble with the keyboard short cuts not working? The text of the menu item must be identical to what you enter in the keyboard shortcut dialog; note that an ellipsis (…) is not three periods (…); get an ellipses on the Mac with OPTION-;
  7. Terminal bliss. I almost never use the Finder, as the Terminal can do everything I need and faster. There are, on occasions, though, things that the Finder is better at. On those occasions I just open the directory, like open ., which gives me a finder window for the current directory in the shell, or open ~/Talks/Mytalk for another directory.
  8. Terminal bliss. I find ejecting a volume cumbersome… you have to mouse around, find the volume, click on it, hit CMD-e. Ug. Better, is just to issue the command hdiutil eject /Volume/<VolumeToEject>. Better yet, define the following alias: alias eject hdiutil eject. Then you can intuitively just type eject /Volume/<VolumeToEject>. Magic!
  9. Terminal bliss. I have a “sleepnow” alias so I don’t have to fumble for the Apple->Sleep menu nor remember to eject my backup volume:
    ejectbackup; osascript -e 'tell application "System Events" to sleep'; echo Going to sleep, please wait...
    The command ejectbackup is yet another alias for:
    if (-d '/Volumes/My Backup') hdiutil eject '/Volumes/My Backup'
    which is c-shell specific, though a bash user would want
    if [ -d '/Volumes/My Backup' ] hdiutil eject '/Volumes/My Backup'

    While this ejectbackup bit is optional, it’s handy since it ejects my Time Machine backup volume so that I can yank the USB cable just before it sleeps; otherwise, without it, you’ll wake up the Mac when you pull the cable and get pesky messages telling you to eject the USB properly. (Of course, replace ‘My Backup’ for the name of your backup volume.)

  10. Remove Apple Extended Attributes. MacOS looks like Unix, but one difference is that Apple has created extensions to record arbitrary key-value pair attributes for each file. For example, the way that your Mac knows you downloaded something from the internet is because it has added attributes with keys like “” to flag the origin of the file. To view such attributes you need the xattr program (from e.g. here), as in
    xattr -l file.txt
    To clear one particular attributes use
    xattr -d <key> file.txt
    where <key> is “” or whatever you want to delete

    To wipe out all attributes you have to do it one by one, ug. Here’s a one-liner to do it

    xattr --list-parseable file.txt | grep = | awk -F= '{print $1}' | xargs -I %% xattr -d %% file.txt

    and, specifically, my csh alias

    alias xattr_del_all 'xattr --list-parseable \!:1 | grep = | awk -F= '\''{print $1}'\'' | xargs -I %% xattr -d %% \!:1'

  11. Sparse Bundles for extra security. I find it handy to keep confidential information on sparse bundles protected with a password. To create one use this command

    hdiutil create -size 100m -volname PatientInfo -encryption -type SPARSEBUNDLE -fs HFS+J ~/Project/PatientInfo

    You’ll be prompted for a password, and then you’ll see that this will create a directory “PatientInfo.sparsebundle” in the Project directory. In order to open this directory you’ll have to enter the password.

    If you ever need to increase the size later, use this command

    hdiutil resize -size 200m ~/Project/PatientInfo.sparsebundle

    To later change the password, use

    hdiutil chpass ~/Project/PatientInfo.sparsebundle

  12. Reunite resource fork on un-archiving, OR, how to avoid __MACOS folders. Mac OS files have a data and resource fork, where the resource fork has information metadata like file creation and modification time. Whenever you unzip a .zip file on the command line that was created on the Mac, you’ll find a __MACOS folder gets created which which contains resource for information in separate ._* files, and they can generally be safely deleted. But if you want to reunite the resource fork with each file, use ditto, like ditto -xk <<ARCHIVE.ZIP>> <<TargetDirectory>>, for example
    ditto -xk .
  13. Resize images. Modern digital cameras create gigantic image files, not great for sharing. While there are variety of ways to resize images on the command line (e.g. with ImageMagick) I was amazed to learn that there’s a tool built right in to Mac OS to do this. The sips command is a “scriptable image processing system”, and to resize images just do

    sips -Z 1024 *.JPG

    but note that this resizes in place, i.e. writes over the existing image. Here, 1024 specifies the maximal dimension the resized images have.