Monday, March 9, 2009

Suddenly Loving AppleScript

Some things are instantly useful, and  you love them from the moment you set eyes on them.  Others take more time to become useful. 

For me, AppleScript has definitely fallen into the second category.  When I first got my iMac in 2006 I was excited to see what I could do with AppleScript and Automator. These, I felt, were what the world of Windows was wanting (alliteration free with every blog post! (not really! That would be really hard!))

So I dove in to attempting to script every aspect of my life. I wanted to automatically move things from my desktop to my FTP server, send batch emails to people about things, and basically just stop wasting time.  And it never worked.  Nothing I tried worked.  Finally, after much struggle, I made a nice little "app" that controls iTunes and DVD Player, so that you can sync a Rifftrax with a DVD, provided you are pretty lenient with the usage of the word "sync". Basically, it would pause one or the other, so that you could get them closer together.  In the end it was almost harder to use than the native interfaces for those two programs, and I pretty much stopped playing with AppleScript altogether. 

Well, not quite. I kept going back, trying to see what I was missing, why so many people thought that AppleScript was awesome, why there were long-running web communities that pass scripts back and forth, why programs went out of their way to be scriptable, and eventually decided that there must be something there.

So I scaled back my expectations of my first scripts.  And a few weeks ago I found the perfect use for Apple's little english language parser: file cruching.  You see, there's this system at work made by a company I'll call "Big Red" (not it's real name. At all), and when you export files from their server they are in Unicode 16.  The problem is that languages like Ruby, PHP, et al, don't like Unicode 16. They like Unicode 8, or else you have to fiddle around under the hood.  So instead of fiddling around under the hood, I wrote a script to start BBEdit, have it open all text files in a directory, and save them as UTF-8 files instead of UTF-16.  See? No big deal.

But it was finally a successful and useful "program".  I use it a few times a week, and it's quite handy.  With one success under my belt, I wondered what else I could do.  I wrote a script that runs on startup, maximizes Safari, and points it to a specific website. Bam!  instant display system.

Today I wrote the script I've been looking for for years.  All I wanted to do was loop a movie indefinitely.  Frustratingly enough, it used to be included with DVD Player, but not with the Leopard version.  So I kept seeing people say "just look in this folder (that existed in OSX 10.3) and run the 'loop' script."  Since it was included with every copy back then no one thought to copy it on to their blog post, so I was stuck loopless.  Still, I knew how to use the Dictionary, and I knew how to write scripts.  So I did.  What I have is functional, not pretty.  But for all of you who are also frustrated trying to make DVD Player keep playing your movie all day long, here's a good start:

--plays a movie through 5 times.
-- To Loop forever change maxLoops on each pass through
--as well as changing loops.

tell application "DVD Player"
    activate
    if has media is true then
       set maxLoops to 5
       set loops to 0
       repeat while loops is less than maxLoops
          delay 3
          --Wait for the DVD to load, then
          --press enter. Usually this will be on the
          --"play all" option.
          if dvd menu active is true then
               press enter key
               delay 2
               if elapsed time is greater than 10 then
                  set elapsed time to 1
                  play dvd
               end if
          end if
          --let the DVD start playing before getting all weird in the head.
          delay 4
          repeat while dvd state is playing
               if remaining time is less than 5 then
                    set elapsed time to 1
                    play dvd
               end if
          end repeat
         set loops to loops + 1
      end repeat
   else
      display dialog "No disc!"
   end if
end tell


I hope this helps someone else fix a common problem, and hopefully helps them also decide that AppleScript isn't a complete waste. Also, if you see easy ways to improve this script, please put them in the comments.

thanks!

0 comments: