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!

1 Answer 1


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"

        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...
            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
  • Did it help at all?
    – Diggory
    Mar 22, 2012 at 16:52
  • That seemed to do the trick in Script Editor, but not in the Perl Applescript module that I'm using (but that's my problem, not yours) - thanks for the insight. Mar 24, 2012 at 23:39
  • One other question - do you know how I would use that same setup to stop the currently playing song in the existing QuickTime player? Mar 24, 2012 at 23:40
  • tell application "QuickTime Player" stop front document end tell
    – Diggory
    Mar 28, 2012 at 10:20

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .