0

I am new to Applescript. I am trying to write a script to record an audio file and save it to a different script bundle - "PlayPro.scptd" - with the name passed to the script from an Excel file. The path - savePath - seems to be correct but it doesn't work. It does nothing when I run it. Here is my script:

on RecordM4a(RecordName)    
    tell application "Finder"
        set r to POSIX path of ((container of (path to me) as text))
        set r2 to "PlayPro/Contents/Resources/AudioFiles/"
        set savePath to r & r2 & RecordName
        display dialog savePath
        tell application "QuickTime Player"
            activate
            set doku to new audio recording
            start doku
            delay 4
            stop doku
            set newDoc to last item of (documents whose name contains "Untitled")
            export newDoc in file savePath using settings preset "Audio Only"
            close newDoc saving no
        end tell
    end tell
    tell application "QuickTime Player"
        if it is running then
            quit
        end if
    end tell    
end RecordM4a
2
  • What does the script do that you don't expect and what do you expect?
    – mmmmmm
    Apr 21 at 20:18
  • Thank you mmmmmm for answering. It simply did not start recording. It didn't do anything. BTW, the answer that Mockman provided fixed it. Thanks again. Apr 27 at 16:22
0

Try this. The key issue is probably that you did not include the script bundle extension in r2, which makes savePath incorrect.

tell application "Finder"
    set recName to "audio_rec.m4a"
    set pathName to (container of (path to me)) as text
    --> "MacHD:Users:username:Desktop:" 
end tell

set appName to "PlayPro.scptd"
set intPath to ":Contents:Resources:AudioFiles:"

set afPath to pathName & appName & intPath
--> "MacHD:Users:username:Desktop:PlayPro.scptd:Contents:Resources:AudioFiles:"

tell application "Finder" to open afPath
set savePathName to afPath & recName
--> MacHD:Users:username:Desktop:PlayPro.scptd:Contents:Resources:AudioFiles:audio_rec.m4a"

tell application "QuickTime Player"
    activate
    set doku to new audio recording
    start doku
    delay 4
    stop doku
    set untDoc to front document
    export untDoc in file savePathName using settings preset "Audio Only" with replacing
    close untDoc saving no
end tell

NB I didn't see any intent for requiring posix paths so I removed them. Unless you expect some other script to meddle with your quicktime player windows, you can simplify that reference. I'm assuming that the folder 'AudioFiles' exists already, and that the destination script bundle is in the same folder as this script.

As an aside, why an Excel file? It might be worth considering using csv, which would be easier to get the string from and probably make your process quicker overall as you don't have to deal with Excel just to acquire a few characters. Finally, on the matter of making a good question, it is helpful when the details include what happens. 'It didn't work' isn't helpful at all — you wouldn't have asked the question if it worked. For example, your problem here isn't with quicktime player — as you suggested, it's about referencing a folder correctly.

2
  • Thank you very much Mockman for your answer. It worked for me. Yes, the folder 'AudioFiles' exists already and the destination script bundle is in the same folder as this script. I used Excel to program a vocabulary practice sheet using VBA. It worked very well but I was not able to add audio to it - the words are not English. Finally I found out that I can use Applescript to play the audio files of the pronunciations. It worked but I wanted to add a button to record the pronunciation of each word, use the word to name the file and then play it by passing the same name to another Applescript. Apr 27 at 16:18
  • That sounds like an interesting project, as well as a novel approach to integrating the various technologies. I'm glad it worked out.
    – Mockman
    Apr 27 at 19:00

You must log in to answer this question.

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