OS X: Console Login

Ever wondered how to log into OS X without the pretty windows? It’s a BSD-system after all. Why would anyone want to do that? If you managed to get your system in a state where the usual login simply does not work any more, for example. Let’s say you created a LaunchAgent, everything looked peachy, you installed it, rebooted and as soon as you log into an account everything goes haywire.

To cut a long story short, if—for whatever reason—you are not able to log in normally, and you know what to do about it, you either need to ssh into the machine or you have to log in using the console. But how do you do that?

Simple, your login-dialog needs to be set to “name and password”, though. Enter >console as username (the “>” is no typo) and hit ↩ (no password). The screen changes to a run-of-the-mill terminal, you probably have to press ↩ again to be greeted by a standard login-prompt. Remember to use the credentials of an administrative account since “normal” users are not allowed to use sudo. To revert back to normal use sudo reboot.

Disclaimer: It works for me, YMMV. I’m not responsible for any damage you inflict on your system, be careful. ;)

Technorati Tags: ,

iTunes: How to Print a Jewel Case Insert

Something many people miss, because it’s just not that obvious: Mark a playlist in iTunes, select “File > Print…” and voila:

Print-dialog iTunes

It’s that easy, it’s a Mac. ;)

Ruby: Simple Calculation of Billable Time using Epoch-Seconds

How to calculate billable time in Ruby using epoch-seconds. Out of interest,I ported the Perl-Code to Ruby and have to say, wow. Ruby is very elegant (not sure if my code is, but it works).

The idea was to use another (scripting-)language readily available on OS X. As I said already, it works like the Perl-version:


#!/usr/bin/ruby

# Variables
starttime = Time.now
epochtime = starttime.to_i

# write to file
timetemp = File.new("timetemp.txt", "w+")

timetemp.puts epochtime

timetemp.close # always close files

#output information for copying
puts "customer|topic"
puts "Start: #{starttime}"
puts ""

start_record.rb gets the current time in epoch-seconds and writes it to a file called timetemp.txt in the same directory. Nothing new here, but I like the construct timetemp.puts epochtime, Ruby is very clean. ;)

When you’re done, end_record.rb is used to read in the starting time and to perform the necessary calculations:


#!/usr/bin/ruby

# Variables
endtime = Time.now
endsecs = endtime.to_i

# open the file
timetemp = File.open("timetemp.txt")

startsecs = timetemp.readline.to_i

timetemp.close # always close files

#calculations
starttime = Time.at(startsecs)

timesecs = endsecs - startsecs

timeminutes = (timesecs.to_f / 60)

timehours = (timeminutes / 60)

# output results
puts "Start: #{starttime}"
puts "End: #{endtime}"
puts "Seconds: #{timesecs}"
puts "Minutes: #{timeminutes}"
puts "Hours: #{timehours}"
puts ""
puts "Bill: "
puts ""

If you don’t want to copy and paste from the terminal-window, simply pipe the results to a file like this:

$ > ruby start_record.rb > my_file.txt

and when you’re done

$ > ruby end_record.rb >> my_file.txt

Remember though, > means overwrite the file if it exists. If you want to append, use >>. This might be obvious to most people fluent in UNIX, but should be noted as a “just in case you need to know”. ;)

Technorati Tags: , , , ,

OS X: More than one E-Mail Address in a Single Account

Did you ever wonder if it’s possible to maintain more than one e-mail sender-address for a single account in Mail.app? Then this post is for you.

The reasons for sending mail—seemingly originating from different mail-accounts—are manifold, not all with good intentions, but let’s concentrate on valid reasons to do so. For example:

  • All your e-mails are sent from your domain mydomain.com.
  • Your “regular” e-mail address is myname@mydomain.com.

So far, so good. Let’s assume you are developing software and you want to differentiate incoming mails regarding your software-package from all the other mails. The software is called mygreatsw. You could set up another mail-account mygreatsw@mydomain.com, but then you’ll have to deal with two mailboxes. There is another way, if you are using Mail.app, that is.

Open your “Preferences”-Panel in Mail.app and klick on “Accounts”. Activate the mail-account in question and add ,mygreatsw@mydomain.com to your already existing mailaddress myname@mydomain.com. The comma is important. The textbox “Email Address” should look like this now: myname@mydomain.com,mygreatsw@mydomain.com.

If you send a new e-mail now, there is a pop-up menu in the “Account”-line, allowing you to send the mail from myname@mydomain.com or mygreatsw@mydomain.com.

It is that simple, but often overlooked. Just in case you need to know.

Technorati Tags: , , ,

Simple Calculation of Billable Time using epoch-seconds

Every freelancer uses some kind of accounting-package to keep track of the billable time, but, given multiple machines, on-site gigs and the like, keeping track is not always that easy, isn’t it? For that reason, I came up with a really simple combination of two Perl-scripts.

The first one, start_record.pl, writes the epoch-seconds to a temporary file and prints some information to the terminal-window.

Update: Thanks to Hebikai for pointing out a problem with the relative path, the scripts are using the environment-variable $HOME to determine the path now.


#!/usr/bin/perl

$HOME_dir = $ENV{HOME};
$DATA_dir = "$HOME_dir/work/time_control_stuff";

$start_time = time;

#print "Seconds: $start_time\n";

print "customer|topic\n";
print "Remarks: \n";
print localtime($start_time)."\n";

open(MY_HANDLE, ">$DATA_dir/time.log") or die "$0: something went wrong: $!\n";
print MY_HANDLE $start_time."\n";
close(MY_HANDLE);

The script assumes, that there is a directory called time_control_stuff in ~/work/. Its output should be copied or piped into a file, it records the time you started. If you want, feel free to modify the customer|topic and Remarks lines to taste.

If you are done with the task, the end_record.pl script comes into play.


#!/usr/bin/perl
$HOME_dir = $ENV{HOME};
$DATA_dir = "$HOME_dir/work/time_control_stuff";
$end_time = time;

open(MY_HANDLE, "$DATA_dir/time.log") or die "$0: something went wrong: $!\n";

while()
{
chop;
$start_time = $_;

}

$total_time = $end_time - $start_time;
$total_mins = $total_time/60;
$total_hours = $total_mins/60;

print "Start: ".localtime($start_time)."\n";

print "End: ".localtime($end_time)."\n";

print "Total: ".$total_time."\n";
print "Mins: $total_mins\n";
print "Hours: $total_hours\n";
print "\nBill: \n\n";

Its output is pretty much self-explanatory, copy or append to the same file and you are done.

I use an USB-stick to carry the scripts with me all the time. If I need to record billable time and don’t have access to one of my machines, these scripts are my way to go. I input the recorded data into my accounting-package as soon as I’m able to do so.

Granted, they could be a lot more sophisticated, but remember KISS (Keep It Simple S…) rules. Use the scripts as a starting-point for you “accounting on the road.”

Getting the current time in epoch-seconds

I already wrote about epoch-seconds, but there is a quicker way to get them, this time without resorting to Perl.

$ date "+%s"

Will return the current epoch-seconds, at the time of this writing 1191687083. This should work with most incarnations of the date-command. For the restless (a.k.a. the ones not willing to read the post linked to above), converting from epoch-seconds to human-readable time works like this:

$ date -r 1191687083

Or whatever second-count you want to convert, of course. One last thing to try:

$ date -r 0000000000

Returns the time your system deems to be the beginning of its time, usually January 1st 1970.

I have no idea, if some of this works on Windows™, it should though, if you’d install cygwin on it.

Technorati Tags: , , ,

InDesign CS3 and a case-sensitive Filesystem?

Just to spare you, dear InDesign CS3-(would-be-)installer, some grief: If you dared to use the “case-sensitive, journaled” option for your OS X system-partition and you want to install InDesign CS3 on said system, let go of all hope!

You’ll have to re-install your OS (and everything you added to it), because ID CS3 won’t play with “case-sensitive, journaled”, period. Partitions? Nada, zilch, nothing to gain here; the program wants to live on your system-partition.

Just in case you need to know. :mrgreen: By the by, “journaled” seems to be OK (so far, that is), it’s just the case-sensitive part. Adobe’s say on this is here.

Happy re-installing. ;)

Using sed to transform XML to be displayed in HTML

Pasting an XML-snippet into HTML can be full of surprises, the rendered result may not what you had in mind. Most browsers will try to interpret the snippet and you end up with something completely different. ;-)
If the XML to be used is still well-formed, XSLT is one option to transform the offending characters into entities, which in turn render the way they should in any web-browser. But, if we are talking about a couple of lines of XML, things are different. Since the XML isn’t well-formed any more, XSLT is not an option.
A simple sed one-liner could look like this:

sed 's/\\</\\&lt;/g' file1.xml | sed 's/\\>/\\&gt;/g' > file2.txt

Let’s have a detailed look:

  1. sed is called and
  2. and told to perform a substitution s of all occurences of < to &lt; globally g in file1.xml.
  3. The output goes via | to the second call of sed which
  4. perfoms another substitution similar to the one in 2. This time for all occurences of >.
  5. The resulting text is written to file2.txt.

Now you’re able to cut and paste snippets like this

<figure>
  <title>Tags Window</title>
  <screenshot>
<mediaobject>
  <imageobject>
    <imagedata fileref="pictures/ch03/ch03-02tagswindow1.png"
      format="PNG" />
  </imageobject>
</mediaobject>
  </screenshot>
</figure>
<para>The menu of the <quote>Tags</quote> window
allows us to create a new tag.</para>

into the HTML-editor of your choice.

Technorati Tags: , ,

CouchDB, Database re-invented?

I stumbled over a document-oriented, replicating Database named CouchDB. It’s still in “alpha” but everything sounds very promising:

  • Written in Erlang
  • Flat-file and document-centered
  • Replication built in
  • Runs on
    1. Linux
    2. OS X
    3. Windows
  • Open Source

What’s not to like? And since the local database is replicated with the peers, no backups. But don’t forget to put two or three machines in another building. ;)

AppleScript: How to split a string

I think of AppleScript as a fantasic tool to automate things in OS X. But, people—like me—being used to the likes of Perl, Ruby or Python miss the split()-function.

A short explanation regarding split(): The function allows the programmer (a.k.a. us) to translate a string like ‘A-B-C-D’ into a character array, using (in this case) - as delimiter, or marker where data ends or begins during the translation.

Back to AppleScript, there is no function to split a string easily; the “whys” seem to be buried in the vaults of the developers. Not that we wouldn’t need that kind of functionality on a recurring basis, though. ;) But since AppleScript has a property called text item delimiters, we could make use of said property, couldn’t we? Let’s take a look at the code first:


	set myTestString to "1-2-3-5-8-13-21"

	set myArray to my theSplit(myTestString, "-")

	on theSplit(theString, theDelimiter)
		-- save delimiters to restore old settings
		set oldDelimiters to AppleScript's text item delimiters
		-- set delimiters to delimiter to be used
		set AppleScript's text item delimiters to theDelimiter
		-- create the array
		set theArray to every text item of theString
		-- restore the old setting
		set AppleScript's text item delimiters to oldDelimiters
		-- return the result
		return theArray
	end theSplit

The first two lines make use of the handler and display the result in the, appropriately named, “Result”-window in Script Editor.app. The Handler theSplit expects a string and the delimiter to be used as arguments. We need to store the current setting of text item delimiters first. (Never ever override the delimiter without restoring it as soon as you are done, debugging of larger scripts can be a real pain should you forget to restore, believe me.) Then we override the current setting with our delimiter. The theArray is populated with the split-up contents of theString, the delimiter is restored and we return the result.

I’m aware that I could have just used theString and overwrite it with the result, but doing it this way allows the insertion of another handler to convert the array to a different data-type, if needed. ;)

The other thing I’m aware of is the fact, that the code shown above ruins my XHTML-conformity for good, a sacrifice for you, dear reader. :mrgreen: