Display appliance: The Revenge; or Scripting QuickTime Player

QuickTime PlayerIn previous posts, we’ve told launchd to watch a directory for changes and launch a Perl script when changes occur. This script will tell QuickTime Player what to do.

I have a web CGI that people can use to select which QuickTime movie to display. This CGI then creates a symlink called “next.mov” that points to the selected movie. The Perl script looks for this file. Note that we don’t need QuickTime Pro in order to display the movie full-screen; AppleScript can tell QuickTime Player to go full-screen even without QuickTime Pro.

Here is a stripped-down version of the script I use:


#!/usr/bin/perl -w
 
use strict;
 
my $loggedInUser = `who | grep console`;
 
# directory in which we look for movies to play
my $mediapath = "/Users/Shared/DigitalWalkway/display";
 
if ($loggedInUser ne "") {
   # if there is a symlink at "$mediapath/next.mov" then
   # close the current movie and rename next.mov to current.mov
   if (-l "$mediapath/next.mov") {
      closeCurrentMovie();
      rename "$mediapath/next.mov", "$mediapath/current.mov";
   }
 
   # if there is a symlink at "$mediapath/current.mov" then
   # play it if its not already playing
   if (-l "$mediapath/current.mov" ) {
      # readlink gets the actual path of the file
      # that the symlink points to
      my $realpath = `readlink "$mediapath/current.mov"`;
      chomp $realpath;
 
      # basename strips off the path
      # and leaves just the filename
      my $realname = `basename "$realpath"`;
      chomp $realname;
 
      # we use osascript to embed an AppleScript into the Perl
      # script. in the AppleScript, we check to see if the
      # currently playing movie's name is the real name of
      # he current.mov link so we don't keep restarting
      # the same movie over and over
      `osascript<<EOF
tell application "QuickTime Player"
   activate
   set show welcome movie automatically to false
   copy "" to movieName
   if number of movies > 0 then
      copy (name of front movie) to movieName
   end if
   if (movieName is not "$realname") then
      open POSIX file "$mediapath/current.mov"
      set looping of movie 1 to true
      present movie 1 scale screen
   else
      if (display state of front movie is not presentation) then
         present front movie scale screen
      end if
      if (playing of front movie is false) then
         play front movie
      end if
   end if
end tell
EOF`;
   }
}
 
 
sub closeCurrentMovie {
   `osascript<<EOF
tell application "QuickTime Player"
   close every movie
end tell
EOF`;
   if (-l "$mediapath/current.mov" ) {
      unlink "$mediapath/current.mov";
   }
}

Advertisements
Display appliance: The Revenge; or Scripting QuickTime Player

Updating Dock Icons – The Sequel

Dock image
Over a year ago, I posted a script that updated all the icons in a user’s Dock that were pointing to Office v.X apps to Office 2004 apps. That script doesn’t really work under Tiger, because the default plist format has changed in Tiger from XML to binary.

Fortunately, the fix is pretty easy. You can use a utility called “plutil” to convert to/from XML and binary formats:

/usr/bin/plutil -convert xml1 "$homedir/Library/Preferences/com.apple.dock.plist"

Here’s a script that updates Photoshop CS and ImageReady CS dock items to their CS2 counterparts:


#!/usr/bin/perl
# This script edits com.apple.dock.plist
# to remove Adobe Photoshop CS entries and replace them with
# Adobe Photoshop CS2 entries.
#
#
# by Greg Neagle, gregneagle@mac.com, Jan 2006
 
$homedir = $ENV{'HOME'};
 
# is Photoshop CS2 installed?
if (-d "/Applications/Adobe Photoshop CS2") {
   # has this script done its thing?
   if (!(-e "$homedir/Library/Preferences/com.apple.dock.plist.pscs")) {
      
      # convert plist to xml format
      `/usr/bin/plutil -convert xml1 "$homedir/Library/Preferences/com.apple.dock.plist"`;
 
      open DOCKPREFS, "$homedir/Library/Preferences/com.apple.dock.plist"
         or die ("Cannot open $homedir/Library/Preferences/com.apple.dock.plist");
      open DOCKOUTPUT, ">$homedir/Library/Preferences/com.apple.dock.plist.new"
         or die ("Cannot open $homedir/Library/Preferences/com.apple.dock.plist.new for output");
      until (eof DOCKPREFS) {
         $text = readline(DOCKPREFS);
         print DOCKOUTPUT "$text";
         if ($text =~ "file-data") {
            $dictBlock = "";
            $officeAppFound = 0;
            while(!($text =~ "")) {
               $text = readline(DOCKPREFS);
               if ($text =~ "/Applications/Adobe Photoshop CS/Adobe ImageReady CS.app") {
                  # replace the path
                  $text =~ s|Adobe Photoshop CS/Adobe ImageReady CS.app|Adobe Photoshop CS2/Adobe ImageReady CS2.app|;
                  $psAppFound = 1;
               } elsif ($text =~ "/Applications/Adobe Photoshop CS/Adobe Photoshop CS.app") {
                  # replace the path
                  $text =~ s|Adobe Photoshop CS/Adobe Photoshop CS.app|Adobe Photoshop CS2/Adobe Photoshop CS2.app|;
                  $psAppFound = 1;
               }
               $dictBlock = $dictBlock . $text;
            }
            if ($psAppFound) {
               # get rid of the _CFURLAliasData block
               $dictBlock =~ s|_CFURLAliasData.*||s
            }
            print DOCKOUTPUT "$dictBlock";
         }
      }
      close (DOCKOUTPUT);
      close (DOCKPREFS);
 
      #rename our files
      `mv $homedir/Library/Preferences/com.apple.dock.plist $homedir/Library/Preferences/com.apple.dock.plist.pscs` ;
      `mv $homedir/Library/Preferences/com.apple.dock.plist.new $homedir/Library/Preferences/com.apple.dock.plist` ;
 
      # restart the Dock
      `killall Dock`;
   }
}
#END

Updating Dock Icons – The Sequel