Take the 2-minute tour ×
Ask Different is a question and answer site for power users of Apple hardware and software. It's 100% free, no registration required.

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!

share|improve this question
add comment

1 Answer 1

up vote 1 down vote accepted

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
share|improve this answer
    
Did it help at all? –  Diggory Mar 22 '12 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. –  H. Wade Minter Mar 24 '12 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? –  H. Wade Minter Mar 24 '12 at 23:40
    
tell application "QuickTime Player" stop front document end tell –  Diggory Mar 28 '12 at 10:20
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.