Embedding And Playing MP4 applescript

Discussion in 'Mac Programming' started by UNTITLED7, Aug 6, 2015.

  1. UNTITLED7 macrumors newbie


    Aug 6, 2015
    Sydney, NSW
    It appears it's beyond my knowledge for applescript.
    What I need is to embed an MP4 file into my applescript application, and play in in a separate window. It's damn hard. Help?
  2. Partron22, Aug 6, 2015
    Last edited: Aug 6, 2015

    Partron22 macrumors 68020


    Apr 13, 2011
    You'll want to save your script as either a Script Bundle or an App, then open the bundle (contextual menu) and drag your mp3 file into its Resources folder.
    do shell script "afplay /Users/MyHome/Desktop/MyScript.scptd/Contents/Resources/BorntobeWild.mp3"

    "path to me" will get you most of the path you need, but you'll have to convert : to / using "POSIX path of".
    If your MP4 title has spaces in it, you'll need to use "quoted form of".
  3. superscape macrumors 6502a


    Feb 12, 2008
    East Riding of Yorkshire, UK
    By "AppleScript Application", do you mean something you're creating in Xcode? If so, you can use some of the various Cocoa / AppleScriptObjC bits and bobs. Shouldn't be *too* nasty. If not then the suggestion Partron22 makes is probably the way to go.
  4. Mark FX macrumors regular

    Mark FX

    Nov 18, 2011
    West Sussex, UK
    I assume that your using Xcode, as you talk about using Windows in your project.
    If that is the case, then you could do it all with Cocoa Frameworks.

    First drag your audio file into the project, making sure that it is one of the Core Audio supported audio formats.

    Then add the AVFoundation Framework to your project.

    Then use something like the example code below, assuming your audio file is called "AudioTrack.mp3"

       property audioPlayer : missing value
        on applicationDidBecomeActive:aNotification
           set myAppBundle to current application's NSBundle's mainBundle()
           set myAudioFileURL to myAppBundle's URLForResource:("AudioTrack") withExtension:("mp3")
           set my audioPlayer to current application's AVAudioPlayer's alloc()'s initWithContentsOfURL:myAudioFileURL |error|:(missing value)
       end applicationDidBecomeActive:
        on playAudio:sender
           if not (my audioPlayer's isPlaying()) then
               my audioPlayer's play()
           end if
       end playAudio:
        on pauseAudio:sender
           if (my audioPlayer's isPlaying()) then
               my audioPlayer's pause()
           end if
       end pauseAudio:
        on stopAudio:sender
           if (my audioPlayer's isPlaying()) then
               my audioPlayer's |stop|()
           end if
       end stopAudio:
    In the above example, I've used the AVAudioPlayer Class of the AVFoundation Framework, because you can control all aspects of the audio playback.
    Then I connected three buttons on a window to the playAudio: pauseAudio: and stopAudio: methods which I think you would understand.
    I've kept my code very simple with no error checking, just so you can see how the AVAudioPlayer works in basic.
    Although I would recommend reading the Apple Developer Doc's on this class for a full picture of it's capabilities.
    I've also not posted any of the other required Application Delegate methods to keep it a shorter post.

    Regards Mark

Share This Page