Default Mail Accounts and Mountain Lion 10.8

Defining the default mail account in Apple’s Mail.app for Mountain Lion (10.8) is not intuitively obvious. Where you used to be able to sort the order of accounts within the accounts pane of the preferences, that is no longer possible. This can be particularly annoying if your default account should be your work account, but your personal account is first in the list.

Fortunately, it’s not that hard, just different.

If, like me, you keep the list of folders on the left hidden, go ahead and reveal them. Then, expand out the Inbox to show all of your various inboxes.

Now, simply click, and drag any of the inboxes into whatever order you want, making sure that the account you wish to be your default is on top.

From now on, if you do not have a specific message open, or are not looking inside of one specific mail folder, all new messages will be – by default – from the account at the top of that list.

Replies will still be from the account that received the original message, and if you’re looking at a folder that is only tied to one account, that account will also be the “from” account when creating a new email.

 Screenshot+112712+1010+PM

Switching iPhoto Libraries

It used to be that if you had lots and LOT of pictures, iPhoto would slow down, and you’d be told by some well-meaning soul to start a new iPhoto library.  The problem being that to switch from one library to another required you to either dig up an app like iPhoto Buddy, or to learn the timing of holding down the “option” key as you started up iPhoto, so that you could select an alternate library.

In short – while less of a pain than dealing with iPhoto taking forever to do anything, it was still painful to do it manually.

It turns out hte latest updates to iPhoto 11 now allow you to switch to another library from within iPhoto.

 

 While I still use iPhoto Buddy, it’s nice to be able to switch on the fly without restarting iPhoto.

Markdown and Writing

While major writing projects requiring lots of research, sometimes I just want to write up several paragraphs in an article without a lot of fussing about. Text editors are perfect for this, while also guaranteeing, unlike Word, Pages, or even more open formats, maximum future readablity and recoverability. Unfortunately, going back in to properly add links, italicize or quote text, etc., can be a pain.

Thus was born Markdown. Markdown is both a plain-text syntax for tagging text for web/formatted output and a program of the same name that converts text in the markdown syntax to valid HTML. Since the formatting and syntax use conventions already long common in emails, it’s very easy to mark up the text, and text formatted this way is highly readable even as plain text.

Since I don’t feel like breaking out a massive writing tool like Open/Libre Office or Scrivener every time I want to write up a few paragraphs, and since I absolutely hate going back after the fact to format everything in even the best WYSIWYG implementations when I could do it from the keyboard as I type, I really appreciate the ease of use.

I also appreciate the fact that it’s widely available. While blogger doesn’t directly support it, it’s fairly easily added to WordPress, and is available for Posterous and Squarespace.

So here’s my cheat sheet that I keep in Emacs when I need a reference. Most of it is paraphrased from the original, and further details or nuance are linked to on a section-by-section basis to Gruber’s original spec:

Markdown reference:

You’ll note the headers for each section link to the respective section in Gruber’s syntax page for more information on the available options and nuances.

Block Elements

A paragraph is simply one or more consecutive lines of text, separated
by one or more blank lines

This is a new paragraph.

To FORCE a
newline,
use two
spaces.

Headers

Two types.

This is H1 (with ===== underneath)
====================================

This is H1 (with ===== underneath)

This is H2 (With ------ underneath)
-------------------------------------

This is H2 (With —— underneath)

Alternately, you can use ‘#’

# One '#' for h1

One ‘#’ for h1

## Two '##''s for h2

Two ‘##”s for h2

Three '###' for h3, and so forth.

Three ‘###’ for h3, and so forth.

Blockquoting

Markdown uses email-style “>” characters for blockquoting. They can be
nested by adding additional levels of ‘>’. They can also contain other
markdown elements (headers, code blocks, etc…

> This is a blockquote with two paragraphs. Lorem ipsum dolor sit amet,
>
>> Inset second level.
>
> Donec sit amet nisl. Aliquam semper ipsum sit amet velit. Suspendisse

This is a blockquote with two paragraphs. Lorem ipsum dolor sit amet,

Inset second level.

Donec sit amet nisl. Aliquam semper ipsum sit amet velit. Suspendisse

Lists

Markdown supports ordered (numbered) and unordered (bulleted) lists.

Unordered Lists

Unordered lists use asterisks, pluses, and hyphens — interchangably — as list markers:

- Item a
- Item b
- Item c
  • Item a
  • Item b
  • Item c

Ordered Lists

Ordered lists use numbers followed by PERIODS. Order is irrelevant.

1. Dog
2. Cat
3. Kids
  1. Dog
  2. Cat
  3. Kids

List items separated by a space have the content of the <li> wrapped
in a <p> paragraph tag.

Code Blocks

To produce a code block in Markdown, simply indent every line of the
block by at least 4 spaces or 1 tab. For example:

This is a code block.

A code block continues until it reaches a line that is not indented
(or the end of the article).

HTML in a code block is converted to entities, and Markdown syntax is
NOT processed within a code block.

Horizontal Rules

You can produce a horizontal rule tag (<hr />) by placing three or
more hyphens, asterisks, or underscores on a line by themselves. If
you wish, you may use spaces between the hyphens or asterisks.

---
***
___

Span Elements

Links

Inline or reference. In both styles, the link text is delimited by
[square brackets]. See the original
syntax
for a
more thorough explanation of reference-style links.

This is [an example](http://example.com/ "Title") inline link.
[This link](http://example.net/) has no title attribute.

Reference-style links

[This is linked] [id1] to something
[id1]: http://www.midknightgallery.com/  "Midknight Gallery home Page"

This is linked to something

Also, a reference can be implicitly identified by the linked content

  [The Midknight Gallery][] is a great site.
[The Midknight Gallery]: http://www.midknightgallery.com/ "Home"

The Midknight Gallery is a great site.

Emphasis

**Bold**

Bold

*Emphasis*

Emphasis

Emphasis can be used in the middle of a word:

Crap*tac*ular.

Craptacular.

Backslash to escape literal asterix’s like this: \*.

Span of Code

Wrap a span of code within a paragraph with backtick quotes (`)

To include a literal backtick character within a code span, you can
use multiple backticks as the opening and closing delimiters:

``There is a literal backtick (`) here.``

Images

Inline image syntax looks like this:

![Alt text](/path/to/img.jpg)
![Alt text](/path/to/img.jpg "Optional title")

Reference-style image syntax looks like this:

![Alt text][id]

Followed by the same syntax as reference links:

[id]: url/to/image  "Optional title attribute"

Miscellaneous

Backslash Escapes

Markdown provides backslash escapes for the following characters:

\   backslash
`   backtick
*   asterisk
_   underscore
{}  curly braces
[]  square brackets
()  parentheses
#   hash mark
+   plus sign
-   minus sign (hyphen)
.   dot
!   exclamation mark

Automatic Links

Simply surround the URL or email address with angle brackets.

<http://example.com/>

Markdown will turn this into:

<a href="http://example.com/">http://example.com/</a>

Emacs Fixes: Issue 2 – Transmit Compatibility

Transmit is an excellent FTP client, and hands down my favorite for the Mac. One of its niftier features is that of opening up a file on the remote server in a local editor, and uploading the changes automatically as they are saved in the editor.

This didn’t work out of the box in emacs, but I found a fix (h/t to Justillilly.com). 

In your user directory, you’ll find a hidden file called “.emacs” Edit the file to add the following:

(setq backup-by-copying t)

Or, you can use the in-emacs preferences. If you type M-x (refer to the tutorial if you’re not sure what that means), and then “customize”, you’ll get to a customization screen. Select “Files”, then “Backup,” and toggle “Backup By Copying” on, and save the results. This will automatically add the entry to your ‘.emacs’ file.

Emacs Fixes: Issue 1 – Command Line Access

BBEdit and Textmate have a nifty tool they install so that when you’re in the command line, you can use a command like “mate” or “bbedit” to open a specified file. When the file opens in the editor, you can even close out the terminal window without crashing the graphical environment.

While it’s possible to open the GUI version of emacs from the command line with a specific file, it’s not quite so graceful if you forget and close out the terminal window – it crashes. I’ve tried several workarounds, but none of them are truly satisfactory, especially given that I don’t have to open up the terminal to see hidden files in the first place.

So of course you learn the basics well enough that opening up emacs in the terminal allows you to get done what you need, and then realize that to open a file anywhere on the drive, even hidden files and ones hidden from you by the computer, is drop dead simple. 

C-x C-f (Control -X followed by a Control-f) allows you to open up the file browser, starting at your home directory (shown by a “~”). Hitting return then changes the display – the buffer – to the specified file or the contents of the selected directory. From there you can navigate through the file system with much of the same ease as you can through the terminal. 

The lesson here is that you don’t even need the terminal to open up a hidden file. If you are in the terminal because you want to pipe the results of a command to emacs, you can either deal with the terminal – emacs is emacs – or bring up the graphical version. In the last case, just don’t close  out the terminal window before closing out the graphical one.

In short – this isn’t an issue so much as it just works different. 

Making Emacs Work the Way I Wanted it To.

Not all was sweetness and light. Changes mean things are different, and comfortable habits die hard. 
Several things I missed from BBedit and/or TextMate, that were my priorities for “fixing” were:
  1. Some things just are different. The graphical version doesn’t open from the command line without a few clunky integration issues, but it’s something that isn’t really needed either. 
  2. Out of the box, emacs didn’t save things back to disk so that Transmit, my FTP client of choice, would copy the changes up to the server.
  3. Out of the box, the gui version of emacs doesn’t do color themes beyond the stock syntax-coloring-on-white. 
  4. Emacs didn’t do syntax hilighting for web pages using several programming languages (php, Javascript, and of course HTML are common), out of the box.
  5. The syntaxt hilighting for multiple languages is still somewhat clunky due to a few bugs. Javascript is different from what I’m used to in BBedit, but just as good. 
  6. I am still learning how to implement “lint”-based debugging of javascript  – a module that was available for TextMate.
  7. I still need to learn how to program for emacs. I also haven’t learned to replace the convenient snippet structure that TextMate had, but the programming built into emacs is powerful enough to write entire games that run within emacs.
  8. I still haven’t learned how to do a diff well – BBedit and TextWrangler (the free version with less features) still rule here.
First of all – keep in mind why I’m making this change. I’m finding myself in a position where I need a tool I can use anywhere. So, I’ll address how to tackle each of the first five that I’ve already solved, and any other issues that I fix to my satisfaction. 

In Support of “Laziness”

Reminiscent of Lazarus Long’s story of “The Tale of the Man Who Was Too Lazy to Fail” from Time Enough To Love, I recently stumbled into this quote:

Code you don’t write is the easiest and fastest to debut, test, document, read and support.

This quote to me outlines several principles. 

First – while I strongly believe in learning by trying the hard, slow, painful way, by going through the basics, this doesn’t apply to when the priority is to actually produce. Also, the whole point of of doing it hard, slow, and from the basics is to gain an intuitive grasp of how to do it more efficiently, to improve the quality of your work, so that you get better at it, not to be a masochist.

This applies to code as to anything else. Brevity matters. As the quote attests, any line of code you don’t have to write is not only less time spent typing, but the time figuring out how to do it in fewer steps often pays itself back in better performance, and easier maintenance. Lastly, the next time a similar solution is needed, you don’t have to think about it anymore, and you still get the rest of the benefits.

Defining and Creating Objective-C Data Types

Learning Objective-C would be straightforward for anyone exposed to other languages, if one only wanted to learn Objective-C. You have your basic “C” data types, you have “objects.”

Further in, you have a library of “Foundation” objects that handle numbers, strings, and arrays. This is part of the standard “GNU” distribution and is not Mac-specific. The Root object is of the type “NSObject”

Even Further in, you have the Application Kit frameworks that handle UI elements and their needed features for programming on the Mac  (pasting, etc.). “Cocoa” includes both the Foundation and the Application Kit frameworks.

What I needed was a simple, clear-cut rule on when to use pointer nomenclature.

In C-based (and other statically typed) languages, you have to tell the program what kind of information is stored by a variable. So:

int myInteger;

myInteger = 1;

tells the computer that there will be a variable named “myInteger” used to access or manipulate data, to set aside room for that variable, that the variable will be an integer, and sets it to “1.” They can easily be combined into one statement:

int myInteger = 1;

Now, let’s look at objects. First of all, Objective-C, like Python and other object-oriented languages, refers to objects instead of accessing them directly, and so when you define a new variable for an object, you define it as a pointer. 

MyClass *myObject;

myObject = [MyClass alloc];

myObject = [myObject init]; 

This block of text creates a variable called “myString” that is a pointer to an object of the “MyClass” class. The second line calls the classes factory method to allocate the space for the class and create the basic structure of the object. The last line tells the object to initialize itself with any starting information needed so it can be used and referenced.

Why the curly braces? That requires a detour. Like all object oriented languages, objects have methods. For example, “car” objects have “drive” methods that do something when the car is driven. In Objective-C, you call these methods with the following syntax:

[object methodname]

so when I type:

myObject = [myObject init];

I am telling the “myObject” object to run its “init” method, and to assign the object it returns back to “myObject”. 

Also – name conventions. In Objective-C, Class, method, and variable names are written out in “camel case,” where instead of using underscores or other characters, descriptive names are put together with first letters capitalized. The very first letter of class names themselves are capitalized, while all other method and variable names, the first letter is lower case. Thus:

myObject = [MyClass alloc];

actually tells the object for the core class to create an instance of itself, and assign it to “myObject,” while:

myObject = [myObject init]; 

tells the actual object “myObject” to initialize itself, and repoint to its new self when done.

Also, messages can be nested. Since “alloc” returns a valid object, we don’t need separate lines. Instead, we can write:

MyClass *myObject = [[MyClass alloc] init];

Since [MyClass alloc] returns a valid instance of MyClass, it can then be passed a message to initialize itself (via “init”) before handing itself over to our new variable. This one-line methodology is the way you will almost universally see a new class object defined and created.

What about arguments? Arguments are delineated by colons. So, an init with one value passed in would look like:

MyClass *myObject = [[MyClass alloc] initWithArgument: argumentOne];

Simple enough for just one argument (I added italics to make it easier to tell which is which). “initWithArgument:” is the method, and “argumentOne” is the first value passed in. When dealing with multiple arguments, it can appear confusing. Each additional argument gets an additional label and colon. So, a message to implement a method with multiple arguments would look like:

[myObject doSomethingWith: firstArgument andWith: secondArgument andAlso: thirdArgument];

On one hand, this is almost completely different from anything you see in other languages – it reads almost like a normal sentence. That said, it allows you to create very descriptive method names that tell you exacly what it does, and what each value is needed for.

Also, the arguments can be standard “C” datatypes like integers, and they can also be other objects. As a result, if an argument for a method is an object, any method that returns the required type of object can be used as a parameter:

[myObject doSomethingWithObject: [myOtherObject returnAnObjectAsParameter]  ];

So, nesting works both for the object being passed a message, as well as for any objects being used as the parameters of that message.

All of the data types we’ve dealt with are statically assigned, yet, Objective-C is a “dynamically” typed language. This is because of one other data type I haven’t discussed yet: “id”. The “id” data type is a “generic” object pointer that can point to any type of object. It is most commonly seen when receiving actions from UI elements, or in other methods where the type of data is unknown, other than being an object. 

The “id” type also doesn’t use the pointer notation. As a result, unlike the examples we’ve seen above, a variable for an “id” is declared without the asterix:

id myIdObject;

Unlike static typing, any object of the “id” type can receive any message, and as long as the appropriate method exists, will do whatever the method instructs it to do. This is great for flexibility, but the downside is that you also can’t use key-value coded parameters, or any other convenience that relies on knowing the class type. You also don’t get error-checking when compiling the program. For the sake of speed and compiler error-checking, when possible, use statically assigned data types. You can often use a parent “super” class to allow some flexibility without sacrificing all of the error checking.

For some more good information on naming and programming conventions, you can look at part 1 and part 2 of the article “Cocoa Style for Objective-C.” 

Python Variables

As much as I love Python, there are two things about it I’ve seen drive programmers experienced in other languages absolutely nuts. The first is the syntax. Instead of using curly braces to delimit blocks of code, Python uses indentation. The second is how variables are handled.

But wait! Aren’t variables like boxes with names, that we can put data into using the names?

Well, yes, and barring pointer operations in C and its derivative languages, that mental model often works perfectly well. For most languages.

So what makes Python different? First, the fact that everything is an object. Even basic datatypes like integers.

Second, variables in Python, like PHP, are dynamic. The type of data is not predetermined when you first create the variable name, or “identifier”. Because of this, you will never see a variable declaration specifying a type as in Java or C like this:

int car = 3;

As a result, the following commands are perfectly valid:

car = 3

car = “Ferrari”

Third, and most importantly, the variable identifier is just a reference to the data. Any data. Even integers. For example, if I create the variables “car” and “pet”, and set them both to 3, they both point to an integer object with the value of 3. If I set the variable “joe” equal to “car”, it doesn’t make a copy of “joe” – it just points “joe” to the same object as “car”:

8753197-9976584-thumbnail

For immutable data types like numbers and strings, the behavior is effectively the same as you’re used to in any other language, because if you change the value that a variable points to, it just gets redirected to the new value. For example:

8753197-9976599-thumbnail

 

Where things get interesting though, is when dealing with mutable data, like lists.

Try this out:

L1 = [2,3,4]

L2 = L1

L1[0] = 24

In the first two lines, you create a list, and set the second variable, L2, equal to the first. Since we are just passing pointers, then L2 actually ends up pointing to the very same list as L1.

8753197-9976608-thumbnail

So when, in the third line, we set the first item in the list L1 to 24.

8753197-9976617-thumbnail

Since both L1 and L2 point to the same list, the first value in BOTH lists is now 24.

Why was it chosen to make Python behave like this? I haven’t found the specific answer in either the python.org sites, or at Guido Rossum’s blog detailing the history and philosophy behind it, but It’s almost certainly a part of trying to make everything an object, while allowing dynamic variable types, and reducing the memory overhead as much as possible while also keeping you from having to deal with memory management.

One of the most useful consequences of these design choices is the ridiculous amount of flexibility in how you pass around and manipulate data. Since everything is an “object” – you are not restricted to “integers” and ‘floats.” Specifically, I’m talking about “duck typing” – named for the expression “If it walks like a duck, and talks like a duck.”. In the case of Python, (or id-type objects in Objective-C), what this means is that not only can you assign any type of data to a variable as we already discussed, but you can try to run any method on it you wish as long as the object supports the method.

This is fundamental. Unlike, for example, Java – where polymorphism is limited to methods of a class or superclass from which the class inherits – Python doesn’t care what type the class is – it just cares if the method exists. It doesn’t matter whether it’s a duck, a rectangle, or a car, it just matters if it responds to the method “quack()”.

So how do we deal with this?

Well, if you’re just passing the variable into a function to be read, it doesn’t matter. If, on the other hand, you want to actually make a copy, or create a new, modified data set without changing the original, you need to make a copy.

Even with copies we run into issues. Lists are objects just like anything else. What happens if the list you are copying contains lists? A simple copy of the top-level list makes a separate set of references for anything in the list, so now both lists seem to be entirely separate. Yet, if the list we are copying has sublists, our copy still contains shared pointers to those sublists, with all of the problems we saw above.

So first, unless you, the programmer explicitly ask it to, Python only passes references. This is never going to cause trouble when dealing with immutable data types.

Secondly, if you want to make a separate copy of a list or mutable datatype, you have to explicitly copy it by using the “copy” method, or a slice  like this:

aList = [0,1,2,3]

bList = a[:] # shallow copy of a using a slice

or

import copy #import the copy module

aList = [0,1,2,3]

bList = copy.copy(aList) # shallow copy using copy module

Third – if you have sublists or other mutable data types within a mutable data type, you need to make “deep” copies. Deep copies allow you to make separate copies of any sublists, dictionaries, or other mutable data that would cause issues in a shallow copy. They require you to either code your own method, or again, you can import the copy module, and use the deepcopy method.

import copy #import the copy module

aList = [0,1,2,3, [“a”, “b”, “c”]  ] # list with sublist

bList = copy.deepcopy(aList) # deep copy using copy module

Hopefully this helps someone out there trying to get their heads wrapped around Python variable handling.

Getting Started With Programming: Tools of the Trade

In programming, the actual “code” programmers create is merely written text, and programs are little more than text files. As a result, the most important tool anyone can have is a solid, reliable editor. Sure, you can use Notepad to whip up a batch file, or TextEdit on the Mac, but most projects require more than Notepad provides, while the rich text features of Wordpad or TextEdit will only get in the way.

The nice thing is that to get started you don’t have to invest a lot of money in fancy IDE’s (integrated development environments) or hours downloading the hundreds of megabytes of XCode or its equivalent. The Eclipse IDE for Java – while fairly large still smaller than Xcode – is free, and most web or Perl/Python/etc. development can be done quite nicely with a broad selection of free editors. Outside of Java and Cocoa (Mac) programming, all of my coding is done in BBEdit.

On the Mac-only side, I recommend getting started with Textwrangler – the “lite” version of BBEdit. Despite missing some advanced features it has the critical ones – syntax color hilights, multiple-file searches, regular expression searches that let you search by pattern definitions, and my favorite file comparison tool for displaying the difference between two files.

For Windows, check out Notepad++.

Finally, for free, ridiculously powerful, and cross-platform (including Linux) there’s Vim and Emacs. Both have all the features you need and then some, come in command-line and GUI variants, and have fanatical adherents who will tell you at the drop of a hat why Vim/Emacs is awesome and the other one sucks. No matter which one you choose, you’ll end up with an editor powerful enough to handle your needs for years (One of the guys handling FX for the movie 2012 uses Emacs for handling 3D render programming…).

For more suggestions, take a look at this lifehacker page on the best text editors.