Archive for the ‘Advanced’ Category

Twitter While You Sleep

Thursday, May 8th, 2008

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” http://twitter.com/statuses/update.xml

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 tw.sh, 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:

./tw.sh “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; ./tw.sh “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.

Fetch New Mail.app Messages from Your Phone

Thursday, March 13th, 2008

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 Mail.app rules in Leopard.

Need to check for new messages in Mail.app 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 Mail.app 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 Mail.app 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 Mail.app, 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>

or

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 Mail.app 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 Email - Retrieve a File by Email - Mail a File Dropped in a Finder Folder

Download Applescript

Dual Layer DVD+R = Fusion VM Backed Up

Monday, March 3rd, 2008

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.

Tree Command for Mac

Friday, February 29th, 2008

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 unix.com 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

Retrieve a Remote File - by Email

Saturday, February 16th, 2008

zipIf you’re using an iPhone or a touch or some other mobile device this could be a very convenient way to retrieve a file from home and view it on your device.

Here’s the scenario: You’re on the bus, you realize your French homework is sitting at home on your Mac. Send yourself a text message with the path to the file. By the time you get to school and log on to a computer your file has been emailed. Problem solved.

Configure Murphy’s script to run every so often using something like launchd. Then send yourself an email. Include the path to the file you want sent in the body, like this:

/Users/murphy/Documents/PenskeFile.doc.

Mail will create a new message, attach the file, and send it to the address in the script. You can configure the script to run when a certain word is in the email subject, like FileGrab. This is an ideal use for rules in Mail.app. But rules don’t seem to work in Mail as of 10.5.1. Murphy hasn’t tried 10.5.2 yet.

Update:  If you leave the file name out the folder’s contents will be zipped and sent.

Instead of using rules, this script is written to run periodically. Maybe every ten minutes or so. If it sees an email with the keyword in the subject it checks for the file and sends it. If the file doesn’t exist the script sends an email that says so.

zip

Security hole? Well, Murphy doesn’t think so. The file will only be mailed to the address you specify in the script, it’s not a reply. (you could alter the script to have it reply, but that doesn’t seem like a good idea.) You can use secure email to send the request and the file. That’s secure.

Getting back to the mobile devices: Your unhacked iPhone and touch don’t let you download files. So you can’t SSH back to the house and grab the file. You could set up your Mac as a web server, but I wouldn’t want to do that.

Using this script the file is emailed to you, so you can view it in Mobile Mail or Gmail depending on what kind of file it is. You can easily view the file later, when you’re offline. The catch? You need to know the path to the file you’re retrieving. Don’t know the path? Here’s some help.

Windows Users: Have a look here.

Note: Script requires use of Mail.app. To schedule this script to run on a schedule you might want to try something like lingon for editing your launchd configuration.

Other Murphy scripts for working with Mail:

Sleep Your Mac by Email

Select a Playlist by Email

Get a List of Your Files by Email

Retrieve Mail.app Messages by Text Message (this post shows how to match emails with Applescript using multiple criteria)


Download Script