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

Video From iPhone Straight to Apple TV

rubeNote: This was posted before Airplay. But it still applies to the old Apple TV.
I was thinking it would be nice to watch a video I’d just recorded with my iPhone on the Apple TV. But it’s kind of a pain to do that. I’d have to run the iPhone upstairs, plug it in, wait for it to sync, add the video to a playlist…

Alternative: I could upload the video to YouTube, but you have to fill out the fields to upload and it seems sometimes my Apple TV loses track of my YouTube account. Or at least loses access.

So I’ve got a Rube Goldberg alternative that makes it really simple from a user perspective – once you’ve done the upfront legwork. Just email the video to yourself, if it’s short enough. Let your Mac automatically grab the video from your email and drop it into iTunes.  My Apple TV seems to immediately sync on its own when new content is added to a Playlist that’s set to sync.

I’m using a pretty straightforward AppleScript and a rule configured in my Mac’s Mail program. Here’s what happens:

  • I email the video to myself with the subject atv.
  • The email arrives.
  • A Mail-based rule sees the subject and kicks off an AppleScript that copies the video file to the Automatically Add to iTunes folder in my iTunes Music folder.
  • The video gets added to my iTunes Library.
  • The video is added to a Smart Playlist.
  • The Apple TV is set to sync with the Smart Playlist.
  • The video is synced to my Apple TV.

That looks like a lot of stuff but the Mac will do all the work. There are only a couple things you need to set up:

  • Create a Smart Playlist and tell Apple TV to sync with it
  • Create a rule in mail and have it run the AppleScript when a matching email comes in.

Then all you do is email yourself the video. If you open Finder and look around in your iTunes Music folder you’ll see a folder called Automatically Add to iTunes. Anything that gets dropped in there gets added to your library, so that’s where the script copies the file.

I threw this script together quickly, there’s nothing fancy, no error checking – so feel free to post any enhancements.   My intention is to provide an example of what can be done easily with AppleScript and Mail rules.  It might help someone accomplish other tasks similar to this one.

Here’s a link to the script and screenshots of a Mail rule and sample Smart Playlist settings.   The script was saved as a text file.  You need to change the path in the third line of script to reflect your user name. You also need to save it as an AppleScript using AppleScript Editor.

In the Mail rule you need to change the path to where you saved your AppleScript.

Mail Rule Screenshot:
Mail_Rule

Smart Playlist Screenshot:
Mail_Rule

You might come up with something more sophisticated for the Smart Playlist…

Finally – here’s a look at the AppleScript, which you can download.

Mail_Rule

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

| Permalink

Restore the Master Library Container in iTunes

LibraryRemember when iTunes used to have a Library container that you could select as the basis for a search of ALL your content? That was nice. Now if you want to search for a TV show in the library you need to click on the TV Shows node before initiating a search.

If there were a keyboard shortcut to select each container I’d use it, but I don’t see any such shortcuts. Thanks to the king of AppleScripts for iTunes I’ve pulled together some pieces that give me a solution.

Hidden Preferences If you really miss the all-inclusive Library container you can add it back into iTunes using an AppleScript application provided by Doug Adams called Change Hidden iTunes Preferences. From there you can script selection of that container or you can go a step further: I’m using TextExpander to kick off a script that facilitates iTunes searching. The script can select the Library container or go on to select a container like TV Shows, Movies, or Podcasts. The extra step is only required if you prefer to have search results limited by media type.

The commands to select the Library container were provided by Doug. When hooks for selecting other containers weren’t readily apparent (maybe they’re there and I didn’t see them) I moved on to another strategy: Selecting the Library and then scripting arrow key presses to move down the tree. So far that works ok. Last step: Place the cursor in the search box. Doug’s page on automating keystrokes helps there too.

scriptThe simple script, shown in the screenshot, can be kicked off with a keyboard shortcut. For me it means not using the mouse, and that’s worth the effort. Still, the script isn’t working 100% of the time. For example, if the Library node is already selected it fails. Needs a little work…

Murphy's Web Host | 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

Distribute Files as a Quick Look “Show”

QuickI was so close to making a screencast tonight, but the stuff I was looking at isn’t quite worth the time. For me. Maybe for you?

I found these little apps on Apple’s Applescript site that let you drop some files onto a droplet. The files are stored in the droplet which when double-clicked displays the files in Quick Look. So you could send the droplet to another Leopard user and they could see a group of photos, Numbers docs, or whatever mix of files you drop in there. As long as Quick Look can display them you’re good.

The downside is that it’s not really using Quick Look – the script utilizes qlmanage which is a Terminal utility intended for Quick Look testing. Unfortunately the command line version of Quick Look doesn’t do everything full-on Quick Look does. For example, once I invoked the Index view I couldn’t get back to regular view. I couldn’t go full screen. And the space bar wouldn’t close the Quick Look window. The site provides ample warning that qlmanage isn’t everything that Quick Look is. It does play video though.

On the plus side, it’s conveivable you might want a different look for files you’re distributing. A single Quicktime movie sent this way might get a little more attention, it won’t look like the same old same old. Then again, if it gets sent on to a non-Leopard user that wouldn’t be good.

Looking at the package contents (right click the droplet and select Show Package Contents) you’ll see that the underlying Applescript invokes qlmanage with the -p option, which displays normal Quick Look previews. I was hopeful when I saw the man page in Terminal. The -t option displays thumbnails, but not in the manner you might expect.  I expected the Quick Look Index view.

Altering the script to use -t resulted in thumbnails displayed one at a time in a Preview-looking window – revealing the next preview only when you close the previous one. It didn’t look like Quick Look at all.

If there were a way to make it open in Index view and have that view function like Quick Look – letting me get back to the default view – I’d be interested. But I’m not. What about you?

You can read more about the downloads here. The download includes another droplet for Quick Look that opens the files you drop on it immediately with pseudo Quick Look.   It uses qlmanage as well.

There’s another download on the site that lets you click a Finder toolbar button to jump into a full-size Finder window in Cover Flow view – with one click. Is that anything?

| Permalink

Fetch New Mail.app 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 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 EmailRetrieve a File by EmailMail a File Dropped in a Finder Folder

Download Applescript | Permalink