Finding the path of a folder inside another script bundle

applescriptaudiopathrecording

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

Best Answer

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.