AppleScript & Automator - batch creating speech recordings from text files

Discussion in 'Mac Programming' started by learnthrusong, Sep 12, 2011.

  1. learnthrusong macrumors newbie

    Sep 12, 2011
    Hi All,

    Firstly, what a cool site! I'm very new to AppleScript and in fact programming full stop. I am working on a very simple application outputs all text files in a given project into a specific sub-directory of the project, however, it's not working properly. I'm getting an end of file error (error -39) and can't work out why.

    In theory (well, tested throughout by outputting the vars in dialogs too) the script
    1. takes the filenames of the txt files that are passed into it,
    2. strips the extension then
    3. builds the links to the _audio directory & filename to output the file later
    4. gets the contents of the text file into a var
    5. records them into the specified output directory as speech

    My projects are audio tutorials that are always in the same root folder (.../Documents/My LearnThruSong Revision Projects/(Project Name)/(Track Name).Each project has a specific set of sub directories - '_audio' and '_text_to_convert'.

    The process in Automator
    Choose finder items ... (locked to tyoe text)
    pass those files into applescript then run the following applescript:

    on run {input, parameters}
       set fileChoice to input
       set i to 1
       repeat with thisFile in fileChoice
           set thisTxtFilePath to thisFile as string
           set AppleScript's text item delimiters to "/"
           set txtFileName to last text item of thisTxtFilePath --as string
           set projectDirectory to text items 1 thru -3 of thisTxtFilePath --as string
           set audioPath to projectDirectory & "_audio"
           set AppleScript's text item delimiters to "."
           set stripTxtNameExtension to first text item of txtFileName --as string
           set AppleScript's text item delimiters to "/"
           set audioFileName to stripTxtNameExtension & ".aiff" --as string
           set AppleScript's text item delimiters to "/"
           set outPut_Aif to audioPath & audioFileName
           set OutList to text items 1 thru -1 of outPut_Aif
           set Finaloutput to OutList as string
           display dialog Finaloutput
           set AppleScript's text item delimiters to {""}
           open for access thisTxtFilePath
           set fileContents to (read thisTxtFilePath)
           close access thisTxtFilePath
           say fileContents using Alex saving to file Finaloutput
           set i to i + 1
       end repeat
    end run
    Any help would be greatly appreciated!!!



    Model: MacPro 8x2.8GHz
    AppleScript: 2.1.2 (Editor - v2.3)
    Browser: Mozilla/5.0 (000000000; 00000 000 00 0 0000; 00000000) DDDDDDDDDDDDDD DDDDDDDDDDDDD
    Operating System: Mac OS X (10.6)
  2. DESNOS, Sep 12, 2011
    Last edited: Sep 12, 2011

    DESNOS macrumors 6502

    Aug 24, 2011
    I'm not positive what's wrong (I'm looking into it), but there's one thing I notice that can be problematic. It's often a good idea to put:
    	close access file thisTxtFilePath
    end try
    before any attempt at access, just incase it's still open.

    I'll see if I can figure out what the problem is.
    I figured out the problem. Use the code above and it should work. You may have to remove "file" from that close access though.
    If access isn't closed, then the last point at which the file was read will be remembered, which would be at the end of the file, and then throws that error.

Share This Page