Murphy Mac - Screencasts and Tutorials » Posts in 'Advanced' category

Create an openssl Function

Remote Login With SSHThis is a quick way to make encrypting files using the terminal fast and convenient. Murphy posted instructions on making an interactive shell script to do essentially the same thing. This is a slightly different spin. Skip ahead to the screencast to see how easy file encryption can be. The openssl command we’re using is included with OS X.

Like other things we’ve covered – the specific example might not apply to you – but for people who’ve never used a function it might be helpful. Functions can make complicated Terminal commands more convenient to use.

To create the function just add this line to the .bash_profile file in your home directory:

des3() { openssl des3 -salt -in "$1" -out "$2"; }

Note that there’s a space after the opening curly bracket and a space before the closing curly bracket. All we need to remember is the function name, and to provide two file names: one to encrypt and one to be the output file.

The breakdown on the command: the first des3 is what we named the function. We can name it anything but des3 is what Murphy chose. The name of the function is what you’ll type whenever you use it.

The stuff in the curly brackets is what happens when we invoke the function. See this post for more on the openssl command.

The des3 following the openssl command is the type of encryption we’re using. It’s part of the openssl command syntax. We added $1 and $2 after the in and out respectively because they’re the two pieces of information we need when we invoke our function.

The $1 and $2 will be replaced with the paths we type into Terminal. In the screencast Murphy shows how to invoke the function. Instead of typing the paths he drags the file to be encrypted into the window – which saves us the typing. He also names the output file with a des3 extension to remind himself how he encrypted the input file.

As always, be careful with the Terminal if you’re not familiar with it. See Murphy’s warning about the dangerous possibilities.

Watch Screencast | Permalink

Twitter While You Sleep

TwitterWait until you see what a clever pun this title is. Using the sleep command in Terminal you can update your Twitter status while you’re away. Or sleeping.

Apple has jumped around a little with regards to command line scheduling tools, but the sleep command is still dead-simple to use. (Read more about scheduling with lingon and launchd here)

Suppose you have a command you want to run once, at a future time. Use sleep followed by the number of seconds to wait. Then use a semi-colon to separate the command to run at the appointed time.

sleep 30 ; command

The above example will execute command 30 seconds after you hit the return key. 3600 seconds is an hour. 86,400 seconds in each and every day.

So now you just need a command to update Twitter. The curl command comes with your Mac. Twitter Development Talk boasts the easiest way to update Twitter from the command line. Scroll down to The Easiest Way to Play Around with the Twitter API. Christopher Penn has a post on it too, but the command is cut-off on his blog.

The command should look like this:

curl -u username:password -d status=”your message here”

Enter that all on one line. It would be better to make it into a shell script. Just copy that line into a text file, maybe name it, and replace the your message here part with $1. Then use chmod to make it executable.

Note: Don’t know how to make it executable? Read more about making a shell script. Learn even more about shell scripts: make an interactive shell script.

From there you can enter the following in the Terminal:

./ “Looking into starting my own airline…”

And you’ve updated your Twitter status.  You can make that cleaner by setting an alias for your shell script.

Finally, to have your tweet post onto Twitter hours later you could enter something like this in the Terminal:

sleep 7200; ./ “I’m sleeping” (you’ll need to leave your Mac on)

If you’re a stickler about your Twittering and want to be accurate, this is the way to go. Schedule that I’m in the shower tweet in advance. People need to know.

| Permalink

Fetch New Messages from Your Phone

retrieve emailsThis post is intended for people with mobile phones that don’t have Internet access, but can receive emails in the form of text messages. You will also be interested in this post if you’ve had problems with rules in Leopard.

Need to check for new messages in from your stone-age mobile phone? No problem. Send yoursef an email with a preset subject like “MsgFetch” and your unread emails received that day will be forwarded to your phone. This way you don’t always forward to your phone, burning up all your text messages.

The script will append the sender name into the subject so it’s easy to glance at your phone and see who sent each message – since they’re all forwarded from your email address. The messages are marked as read in so they won’t be resent if you issue another request later.

One more thing: This solution also gets around a problem with rules in under Leopard. It seems that scripts called by a rule won’t generate a new mail message, which is something this script needs to do.

Instead of relying on the rules of, the rule processing is baked into the Applescript. By setting the script to run on a periodic basis we can test for trigger emails in the inbox.

How do you get the script to run periodically? You can have iCal do it. Or you could use something like Lingon to configure launchd, which handles scheduled tasks in Leopard. This post has a screencast that covers Lingon.

You could also create a shell script that kicks off your Applescript, or a group of Applescripts for that matter. The shell script would look something like this:

osascript <path to script file>/ <script file>


osascript ~/Documents/scripts/mailforwarder.scpt

You can download the Applescript and read the in-line comments explaining how it works. But let’s take a quick look at two excerpts from the Applescript:

The first part of the script handles what a rule would normally cover. Since rules aren’t working as expected the Applescript handles that duty by looking for an unread email with MsgFetch in the subject. If there is such a message the script calls a subroutine ( a chunk of script) called mail_forwarder.

The other part we’ll look at is located within the mail_forwarder subroutine. It finds the emails that arrived today. The next part, not shown here, checks to see if the message has been read.

The rest of the script has been covered in other Murphy Mac posts. It’s the part that creates and sends the mail message. Murphy’s posts on mailing files from Finder and retrieving files by email both create and send mail messages using Applescript. The Finder post has a screencast that goes with it.

Related posts:

Using Lingon

Creating a shell script

Other tricks with Mail: Pick a Playlist by EmailRetrieve a File by EmailMail a File Dropped in a Finder Folder

Download Applescript | Permalink

Dual Layer DVD+R = Fusion VM Backed Up

duplicate a dvdI picked up a stack of Sony DVD+R DL disks this morning in preparation for my first attempt at DL burning. The objective: Backing up my Fusion install of Windows XP onto two disks.

The XP vm is about 15 GB. I don’t back it up with Time Machine, but I have a separate volume on the Time Machine drive where a copy of the vm is kept. Now I can delete it if I feel the need. The way I use Windows doesn’t require updating the backup with something like SuperDuper. I’m just saving myself from ever enduring the install process by storing a copy.

Burning more than 8.5 GB ? Use Split.

First I dumped the vm folder into a .sparseimage file created with Disk Utility. Then I used the split command in Terminal to split the sparse image into two files. That was simple enough, apart from the annoying difference between GB and GiB.

The disks have 8.5 GB stamped on their tops. That’s the manufacturer’s way of making the capacity look bigger – using decimal notation instead of binary. OS X reported the blank disk as having 7.96GB free.

The disks burnt well, no coasters. If I ever need to restore the XP vm I can copy the files from my dvd+R dl disks and reconstitute them with the cat command. For two bucks I reclaimed 15 GB on my external drive. Not bad. That’s thirteen cents per GB compared with around twenty-five cents per GB on an external HDD.

| Permalink

Tree Command for Mac

tree command for MacThis post is a follow up to Murphy’s post on retrieving a remote file by email. The method described in that post requires you to know the path of the file you’re retrieving. You can use the information in this post to auto-generate a text file with the paths to all your files. Retrieve that file and you’ll have all the information you might need.

For some people the best thing about this post will be this:

find . -print | sed -e 's;[^/]*/;|____;g;s;____|; |;g'

Enter that line in Terminal, hit return on your keyboard, and you’re pretty close to the tree command you loved so much in Windows. (Thanks to the forums for help with the find and sed commands. ) You can copy and paste the command, or download a shell script containing it here. You’ll need to make it executable.

Murphy posted last week about how to retrieve a file by email. The catch was that you needed to know the path of the file. This screencast will show you how to generate a text file listing all your files in a tree format.  If you prefer, you can use this command:

ls -R

That will also display files and folders, but the format is more conventional. Either command will accomplish our goal.

In the screencast Murphy has the find and sed command listed above inside a shell script. The output is redirected to a text file. You can retrieve that text file by email, just like you would any other file.

If you prefer, create a second Applescript like the one in this post. You could alter it to always send a hard-coded file: the file that lists all your files.

In the screencast, Murphy uses Lingon to configure launchd, which handles scheduling tasks you may have used cron for in the past.

For more information on creating a shell script and making the script executable visit this post. If you want to know more about sed (and awk) there’s a whole book to read.

Watch Now | Permalink