How to get an applescript to play a song file

applescriptquicktime

Okay so I want an applescript app that plays a song. The song isn't already on the computer and it's contained inside contents/resources of the app's package contents. This is my current code

tell application "Finder"
    set apppath to POSIX path of (application file id "com.apple.ScriptEditor.id.play" as text)
end tell
set song to {apppath, "/Contents/Resources/song.mp3"} as string
set songpath to POSIX file song as text
tell application "QuickTime Player"
    activate
    open songpath
    play the front document
    tell application "System Events"
        set visible of application process "QuickTime Player" to false
    end tell
end tell

On my computer this code seems to work. However when distributing it, other computers get an error in quicktime. The document “song.mp3” could not be opened. The file doesn’t exist. Any help?

Edit: Fixed an incorrect variable which doesn't fix the problem because I changed the code to post it here and mixed it up.

Best Answer

Activating QuickTime is a lot of overhead for playing a single file.

You can use afplay instead:

do shell script "afplay " & songpath