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

View Airport Express Connections via Command Line

airport expressThese days I need two Airports to cover the house.   There’s an Airport Extreme and an Airport Express that’s wired via Ethernet to extend the network.   The Express was acting a little flaky and I wanted to know if anyone was connecting to it.  (check out Glenn Fleishman’s e-book for in-depth info on optimizing your Apple wireless network.)

One option is using the Airport Utility to see connections to your Apple wireless access points.  But that’s a lot of clicks, and you can’t easily check it from your iPhone or iPad.   Here’s an alternative:  Use an SNMP command to see what’s going on with your Airport Express.

Murphy found a suitable command on Polydistortion.net, shown below:

snmpget -v 2c -c PASSWORD -M /usr/share/snmp/mibs:$HOME/share/mibs \
-m+AIRPORT-BASESTATION-3-MIB MyAirportExpress.local AIRPORT-BASESTATION-3-MIB::wirelessNumber.0

You can delete that backslash at the end of the top line and enter it all as one line in Terminal.  You also want to enter your router’s password in place of password, and your router’s name in place of MyAirportExpress.   Don’t get confused and enter your wifi password or the name of your wireless network.  It’s the name of the router you’re looking for and the administrative password.

When you enter that in Terminal you should get a response that looks something like this:

AIRPORT-BASESTATION-3-MIB::wirelessNumber.0 = INTEGER: 0

The last number represents the number of clients connected to that Airport.  In this case there aren’t any.
I tried using the same command to query my Airport Extreme and had no luck.  I saw reports of people having trouble with SNMP once the last Airport firmware update hit.   Let me know if you have a solution.
The Terminal command was a more direct way for me to get the information I was looking for.  Using an app like TouchTerm SSH I can get the answer from my phone or iPad as well.
Take a look at the brief screencast.  First it quickly demonstrates how many clicks Airport Utility requires to get the information.  Then it shows the Terminal command being executed.
The command is really long.  You could create an alias and save it in your .profile file so it would always be available in your Terminal.  That way you could enter something like “aeusers” in the Terminal instead of the long command.  See Murphy’s post on alias creation for more information.
The page where Murphy found the command for querying the Airport had other commands as well. SNMP (Simple Network Monitoring Protocol) is a cross-platform protocol that’s been around forever – you can do plenty with it. The Airport can also report its information via Syslog, but that’ll have to wait for another day.
Screencast | Permalink

iPhone Photo Rotation – Part Two

beautifulMurphy already posted about issues with mailing vertically-oriented photos from your ios4 iPhone:  Chances are your recipient sees the photo rotated 90 degrees to the left.  Web browsers – Safari included – don’t know how to read the exif orientation tag included with the jpg file.

My previous solution was to use an alternate camera app.  But I’ve got another approach that requires a Mac running Mail.

When I take a photo with my iPhone – in portrait orientation – I simply email it to myself.  My Mac is configured to watch for email with a particular subject and then automatically grab the attachment, strip the exif rotation information, and rotate the photo without using a tag.  Then my Mac mails the photo back to me.

Here’s the setup:

In Finder

In your home folder create a folder called lab with subfolders in and out.  Like this:

/Users/murphy/lab/in

/Users/murphy/lab/out

AppleScript

Download my AppleScript.  Edit the AppleScript and change all occurrences of ‘murphy” to the name of your home folder.  There are notes in the script to help you.

Automator

WorkflowCreate an Automator workflow.  You’ll need to add 4 actions in the same order they’re listed below.

  • Mail: New Mail Message
  • Finder: Get Specified Finder Items
  • Mail: Add Attachments to Front Message
  • Mail: Send Outgoing Messages

Only the first two Automator elements need further configuration.  The first one defines the address your altered photo will be emailed to.  You might add a subject as well.

Then specify this file path in the Finder element:

/Users/murphy/lab/out/outbound.jpg

Make sure you replace “murphy” with your home folder name.  The path points to the photo Automator will mail back to you. Save as a workflow. The AppleScript expects the workflow to be in a folder called _applescripts in your Documents folder. Edit the AppleScript if your workflow will be stored somewhere else.

Mail

WorkflowIn Mail you need a rule to kick off the AppleScript when a message arrives meeting certain criteria.  My rule looks like this screenshot.  The script runs when an email arrives with the subject “rotate”.  You might select a different word or phrase.  Make sure you set the script path to the location where you saved your AppleScript. More on Mail rules.

jhead

The AppleScript relies on a command line utility called jhead to strip the exif rotation tag, which I found in this TUAW post.  My script expects jhead to be in the /Users/murphy/lab/in folder.  After downloading jhead you’ll need to make it executable.  In the Terminal:

chmod +x path/to/jhead

In my case I entered chmod +x /Users/murphy/lab/in/jhead

That’s about it.  My script pulls the photo from the email, rotates it, strips the exif rotation information, copies it to another folder, and kicks off an Automator workflow to mail the photo back to me.

When I receive the altered photo on my iPhone I can simply forward it to someone.  If they view it in web Gmail they’ll see the photo inline-style within the email.  I prefer to save the photo to my camera roll and send it from there.  That way I’m prompted to select a size and recipients using web-based gmail get a thumbnail with a choice to view or download.

Notes

You might want to place jhead somewhere other than where I did.  If you move it you’ll need to edit the AppleScript.

Mail needs to be running on your Mac for this to work.

I used an Automator workflow to send the return email.  You can do it in AppleScript if you prefer, but I’ve had mixed results down that road.  Decided to try Automator.

You could use Automator to extract the attachment.  I’d already written a script to extract an attachment from Mail so it seemed like the way to go.

You could alter the AppleScript so everything happens in one folder.  I used two folders only to help me with troubleshooting the script the first time through.

Don’t forget to make jhead executable.

Dropbox Alternative

Here’s another alternate solution:  Dropbox

You could upload your images to Dropbox from your iPhone – then mark them as a favorite.  From there you copy the image to the clipboard to paste into a mail or save it down to your camera roll.   They won’t be full-size though.  And if you paste and mail it’ll be sent as a png.  But the orientation will be correct.  If you simply upload and send the link the orientation will be wrong.

OR

You could use something like my script above to place a full-size copy of your file in your Mac Dropbox.  Then you can access the photo from the Dropbox app on your iPhone.  You’ll still need to copy and paste or save it to the roll to get the rotation right.

perfect Yeah – it’s a lot to set up and you need a Mac running to use it. I can wake my Mac up from my phone so that’s not a big deal. And I find mail-based workarounds like this convenient to use. Still – maybe Apple should switch back to the old way until the browsers catch up.

Script Download | Permalink

Terminal Sleep Command

You want to schedule something really quickly, and you don’t have time to set up cron or other scheduling tools. In some cases, using the sleep command in Terminal might be all you need.

Here’s an example. My Mac was busy doing something, and I didn’t want Dropbox working on a big sync until it was done. So I quit Dropbox and entered this in Terminal:

sleep 3600; open -a Dropbox

The sleep command had my Mac wait for 60 minutes before launching Dropbox. The other job had finished and Dropbox launched and went to work. More on using Terminal to launch applications.

Of course there are other ways to schedule tasks. But could you implement them faster for this particular scenario?

Related:
scheduling a twitter tweet
running an applescript with iCal
terminal as application launcher

Comments | Permalink

Random Filenames for Digital Photo Frame Images

RandomMany of the digital photo frames on the market still don’t have the ability to shuffle images and display them in random order.   A couple people I know have frames and want to mix up the photo stream a little. The first time I ran into this issue I used a Windows utility called renamer to assign random names to the image files. But what about a pure Mac solution?

There is one, and you don’t need anything other than OS X to get it done.  Skip to the screencast if you prefer – otherwise here’s what you do:

Place the files to be renamed in one folder. It’s best to have them all in one folder without subfolders. If you have images scattered inside subdirectories you’ll have to make some adjustments to these instructions.  USE A COPY OF YOUR ORIGINAL IMAGE FILES,  there’s no reason not to.

Open the Terminal from your Utilities folder in Applications. Type cd followed by a space. Then drag the folder containing the files from Finder to the Terminal window. Hit return. The Terminal prompt should update to indicate you’re now in the directory containing the images.  This folder should NOT BE THE SAME AS THE ONE YOUR ORIGINALS RESIDE IN.  Sorry for that, but it’s important.

Finally, type the following command in the Terminal:

for i in *.jpg; do mv $i $RANDOM.jpg; done

The command is case-sensitive.  If your images have names ending .JPG you’ll need to adjust the *.jpg part of the command above to use a capital JPG.

Hit return and your image files are renamed using random numbers.  Copy them to the digital photo frame and your images should cycle randomly.  Thanks to this thread at Mac Rumors Forums.

I’ve included a screencast for those not familiar with Terminal.  Be careful in the Terminal.  You can delete files accidentally and they won’t be in your Trash !  See this warning for more information.

Screencast | Permalink

Reboot or Sleep a Mac from your iPhone

Sometimes Murphy is just a little too lazy to go upstairs and reboot a Mac that needs rebooting. With an iPhone or iPod touch you don’t need to leave the sofa.

Today, Murphy was watching tv on the iPhone via EyeTV’s iPhone app. It started to lock up so a reboot seemed like a good idea. Murphy has Touch Term installed on the iPhone. And the Mac is set to accept SSH sessions. One tap to connect and one command to start the reboot was all it took. Here’s what you need:

  • First, go to Sharing in your System Preferences. Turn on Remote Login. This allows your Mac to allow incoming SSH sessions.
  • On your iPhone install Touch Term, or some other SSH client. Murphy remembers getting Touch Term for free, but now it starts at $3.99. You might want to comparison shop for similar products.
  • Next, configure Touch Term to connect to your Mac, either by name or ip address.
  • Once you’ve connected just type the command to reboot your Mac, and press Return. Here’s the command:    sudo shutdown -r now

Murphy took the extra step of creating a shell script to run that command, so there’s less to type when it’s time to reboot.  That’s all there is to it.

You might want a command to send your Mac to sleep instead of rebooting it.  This command should accomplish that:   osascript -e 'tell application "System Events" to sleep'

Of course, you can always try something completely different.  A long time ago Murphy wrote some posts about using the Mail app on a Mac to trigger events.  Murphy has Applescripts on the Mac that do different things when emails come in with certain characteristics.  Scripts that adjust iTunes, retrieve files, or put the Mac to sleep.  The whole thing was based on a post here.

Some of these scripts had issues with different updates to OS X and changes to Mail.  Your results may vary…

I haven’t looked into other ways to reboot my Mac from the iPhone, so if you’ve got a good way let me know.

As always, be careful when using the Terminal.

| Permalink