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

If This Than That For AppleScript Kickoff

For a long time I’ve used scripts that were kicked off by incoming mail on my Mac.  As long as the mail was from me and the subject matched a keyword the script would run.  There are plenty of tools enabling remote control of your Mac.  But for some things I still find it easier to kick off a script than to manually take control of the Mac.  SSH takes longer, and so do remote desktop apps.

There were some problems with the scripts.  Sometimes Mail wasn’t running.  Sometimes they didn’t kick off for some unknown reason.  When things don’t work every time you tend to stop relying on them.

I’ve got a new way to kick off scripts by sending a text message to If This Than That.   IFTTT receives my message and writes a text file to my Dropbox account named after the message content.   My Mac sees the file and a Folder Action script runs, performing whatever task I requested.

There are a couple I use frequently:

  • Mute my Mac
  • Launch iTunes
  • Reboot my Mac
  • Kill and restart EyeTV
  • Turn on Require Password in System Preferences

I’ve assigned a one letter code to each of those things.  When I send the letter in a text to IFTTT I’m done.  My Mac goes silent or iTunes launches for my Apple TV or whatever it is I need happens.  Less steps than SSH or some kind of remote desktop.

My mind is starting to go so it’s hard to remember which letter does what.  Every once in a while I text #m to IFTTT and they text me back a canned text message I prepared with the key to my commands.  Their reply to #m looks something like this:

a. mute b. iTunes c. restart EyeTV

The thing I don’t like about this method is giving IFTTT access to my Dropbox account. Not that I have an issue with them specifically. I’m just hoping that the protocols to allow interaction between various online services become more granular so you don’t have to grant so much access for whatever you want to do.

I’ll post some more information over the next few days detailing the AppleScripts themselves.


| Permalink

iPhone Photo Rotation – Broken in Lion

I had a solution, but now it’s broken. Before we get into that let’s review what we’re talking about.

If you email portrait (vertically) – oriented photos from your iPhone your recipient is likely to view them sideways, especially if they open them using a browser-based mail service like Gmail.

The solution I was actually using: Email the files to myself first. My Mac would receive the photos and run an AppleScript triggered by the email subject. The script rotated the files, stripped out the exif tag that caused the problem, and mailed them back to me. I could then use that copy for sending to other people. It was relatively painless as I could email the photo right from the Camera app which kept the number of steps reasonable.

But a couple things aren’t working with Lion. The part of my AppleScript that pulls the attachments out and saves them to a folder in the Finder isn’t working. Others have the same problem. Further, the Automator action that sent the email back out isn’t working either. I’m not alone on that one either.

So, I looked at alternate solutions. Like uploading to Dropbox. Dropbox uploads a full res version of your photo. So I created a folder action script that handles the rotating and exif tweaking as soon as my Mac sees the file. That works fine. The problem is that the Dropbox app on my iPhone doesn’t download the full res version, it pulls down a version I consider too small.

I didn’t give up right away. The iOS Safari Dropbox DOES open a nice big version of the photo. From there I can save the photo to my camera roll and email it out. Recipients get a correctly oriented version of the photo, big enough to see.

So now the problem is the number of steps and apps involved. I could use something like Instagram but sometimes I just want to send a photo in an email, straight-up.

I don’t understand why Apple has made this an issue. iOS used to rotate a different way. Their own browser on the Mac isn’t going to display the photo correctly because of the way they’re handling the rotation, with a tag. Why browsers can’t read the tag – I don’t know.

Anyway, I’ve got a solution for when I need it, but I don’t see myself using it much. Too many steps.

11/2011 Update:  I have this working again.  My Automator workflow fails at the step where it sends the email.  So I’ve updated the AppleScript with a command to send it.  I’ll post the updated script shortly.

| Permalink

Hide Everything

desktopEver want to hide ALL your applications and start with a clear, swept off desktop on your Mac? Yeah, there’s Exposé, but that’s not the same as what we’ll see here. When you show your desktop via Exposé you’re not changing the state of your applications to hidden – so as soon as you switch to an application the others will all be visible too.
If you’ve used Windows you may have used the Show Desktop icon in the taskbar to minimize all windows. We’re not going to minimize, we’re going to hide. But the end effect is the same. You’re looking at your desktop with nothing in your way.
How do we do this? A very short and simple Applescript. With one small concession.
If you’ve ever tried to hide ALL your applications you probably know what our obstacle is.

Try this: Switch to Finder and hit Command-Option-H. That hides all applications except Finder. Once they're hidden try hitting Command-H to hide the Finder itself. It doesn't work.

So here’s a workaround that’s acceptable to me: Our Applescript will hide the windows of all applications except the Finder. Then it will CLOSE all the Finder windows. The end result is a clean desktop with application windows that stay out of sight until you summon them back into view.
Optionally, you could include the 3 lines of the script after the line “Close every window” (commented out with dashes in the image below) to open up a single Finder window to a specified folder and use that as your starting point.


Once you’ve created the script you need a way to kick it off. I use TextExpander for a lot of these things. TexExpander is a fantastic text replacement utility that can also run Applescripts when you type certain characters. For example, Murphy might type dsktp or ddesk to make TextExpander run our script and display the clean desktop. There’s a lot more to TextExpander than running Applescripts, but I find it very handy in that capacity. Instead of memorizing somewhat arbitrary key combinations you can make up a code – so your shortcuts are more like keywords with meaning than shortcuts that generally only carry one letter. Murphy covered TexExpander here.
As an alternate check out FastScripts from Red Sweater Software. FastScripts lets you assign a script to a keyboard shortcut. And it has powerful features for helping you manage your script library. If you’re a heavy Applescripter you probably already know about it. If not, take a look at the features.

| Permalink

Remote Control Spotify

“I don’t think necessity is the mother of invention. Invention, in my opinion, arises directly from idleness, possibly laziness, to save myself trouble.”
-Agatha Christie

In Murphy’s continuing efforts to avoid leaving the sofa he’s using an iPhone to kick off Spotify on the Mac upstairs and have it stream over Airfoil to an Apple TV connected to a stereo downstairs. It works, it’s pretty simple, and once it’s started there are other options for controlling it. spotify
The key is that Spotify recently added some basic AppleScript support to their Mac app. Not as much as we’d like to see, but enough to get us started. Spotify says they’re just experimenting with AppleScript at this point. (There were workarounds before Spotify added support) We’ll be watching for further enhancements.
This short and simple AppleScript (view entire script) is all you need to get started. It launches Airfoil (an application for redirecting audio from your Mac to remote speakers via Airport Express or Apple TV) and selects Spotify as the audio source application. Then it selects a playlist in Spotify and initiates playback. That’s it. Let’s take a look at the script.
The lines in the first block set Spotify as the application Airfoil will pull audio from. As long as you’ve got Spotify in your Applications folder you can copy those lines exactly. The line in the next block references your speaker id, which you probably don’t know. You can get the id by running a very simple script that looks like this. That line will query your remote speaker for its id and display it in the results pane of the Applescript Editor. Be sure to use the name of your Apple TV or Airport Express in the quotes. Once you’ve got the id you shouldn’t need to run that script again.
The script pauses to make sure your Mac doesn’t get ahead of the launching applications. Spotify hasn’t provided playlist selection hooks for Applescript yet, but they do provide something almost as good. In the Spotify application you can right-click a playlist or track and get an identifier, just like you see in the screenshot at the top of the post. You want to select Copy Spotify URI. Notice the open location line in the script, it’s not inside the Spotify tell – end tell section because it’s not in the Spotify Applescript dictionary. Regardless, it works to select a playlist and that’s all we really need. The last part of the script tells Spotify to start playing.
So – now we just need the script to run. Murphy is using a Mail rule. He sends himself an email with a subject Mail is watching for. Mail runs the script whenever a message with that subject comes in. Once Spotify is up and running the Speakers app that comes with Airfoil can be used to pause or skip to the next track.
I’d rather have an application on my iPhone, maybe something like TextExpander, that could list Applescript TextExpander snippets on my Mac and kick them off that way. But I don’t know of an app like that. The email kick-off works fine, and it’s quick and easy to use, but at times I might have to use another app to wake the Mac up.
In the past I’ve used Mail to run Applescripts on my Mac. It started with putting my Mac to sleep by email but I’ve also used the strategy for kicking off iTunes (before the Remote app) and for file retrieval.

If you’re looking for more information on Applescript and Airfoil they’ve got some examples on their site.
Interesting: Initially I made a script that opened a Spotify playlist and didn’t include the line to commence playback. It stopped working after a day, I don’t know why. It would play the one track and then stop. The day before it jumped to whatever playlist had been set as the current playlist in the Spotify application and continued playback. Not sure what happened there.

| 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:




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.


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:


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.


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.


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.


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.


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