Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

UNTITLED7

macrumors newbie
Original poster
Aug 6, 2015
1
0
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?
 

Partron22

macrumors 68030
Apr 13, 2011
2,655
808
Yes
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.
Then
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".
 
Last edited:

superscape

macrumors 6502a
Feb 12, 2008
937
223
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.
 

Mark FX

macrumors regular
Nov 18, 2011
159
17
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"

Code:
   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
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.