Use AppleScript to launch and manage Quicktime Player in the background I have a Perl-based audio player that runs on OS X, and I need to update how the audio files are played.  I'd like to use AppleScript to control the QuickTime Player, because it will do what I need, but am running into a couple of problems.

First, I need the QuickTime player to launch in the background. The way my Applescript is working now, it's putting the player in the foreground, which then requires users to click back on my Perl app to keep going.

Second, I would like to have playing a new audio file reuse the existing QuickTime Player window, so that there's only one player going at any given time.  So if a song is playing, and they choose to play another song, it stops the currently playing song in the window and loads up a new one in the same window.

Here's a fairly representative sample of the AppleScript that I'm working with.  I'd love to know if there's a way to accomplish both of those goals.

set unixFile to "/Users/minter/mp3/ThomasDolby-SheBlindedMeWithScience.mp3"
set macFile to POSIX file unixFile
set fileRef to (macFile as alias)

tell application "QuickTime Player"
    open fileRef
    play the front document
end tell

delay 3

tell application "QuickTime Player"
   stop the front document
   close the front document
end tell


So when I run this AppleScript, it pops the QT player window up in front.  If I run the script again, it creates a new window.  Both of those are the problems I'm trying to solve.

Any help would be appreciated!
How about this: It's not exactly what you want, but achieves roughly the same thing.

I seem to remember pre-OS X you could activate an app and keep it from being frontmost, but that appears not to be possible these days (or I imagined it.)

to replaceFrontQTPlayerWithFile(aFile)

tell application "QuickTime Player"
    activate

    try
        set frontDoc to front document
        close front document
    on error err number errNum
        if errNum is -1719 then
            -- There is no open document
        else if errNum is -10000 then
            -- Front doc exists, but does not really...
        else
            log err
        end if
    end try

    open aFile
    play front document

    -- Hide QTP
    tell application "System Events"
        keystroke "h" using command down
    end tell

end tell
end replaceFrontQTPlayerWithFile


on run
    set unixFile to "/Users/diggory/Music/iTunes/iTunes Music/Music/Underworld/Oblivion With Bells/01 Crocodile.m4a"
    set macFile to POSIX file unixFile
    set fileRef to (macFile as alias)
    my replaceFrontQTPlayerWithFile(fileRef)
end run