I have this applescript in an automator service. it works.

I cannot however get it to save to my desired directory, or convert to mp3.

At the most basic, I would like to save to /Users/<user>/Music/Mandarin-text2speech.

Ideally, I would like filenames to increase incrementally, convert to mp3 and delete the aiff.

here is my basic script that works for now.

on run {input, parameters}
  repeat with thisText in input
      say thisText speaking rate 130 using "Mei-Jia" saving to file (((path to desktop) as string) & "audio.aiff")
  end repeat
end run

The say command has been able to save in different formats for a while. The supported formats can vary with the voice, but for Mei-Jia it will save to mp3 or mp4 directly, so you don't need to do any converting or deleting.

EDIT: Looks like I was mistaken about the encoding - there isn't an mp3, but you can use the say shell script to set other encodings such as mp4:

on run {input, parameters}
    set saveFolder to (((path to music folder) as text) & "Mandarin-text2speech:")
    repeat with thisText in input
        set fileName to getUniqueName for "audio.mp4" from saveFolder
        set output to quoted form of POSIX path of (saveFolder & fileName)
        do shell script "say -v 'Mei-Jia' -r 130 -o " & output & " --file-format=mp4f " & quoted form of thisText

    end repeat
end run

to getUniqueName for someName from someFolder -- add a numerical suffix as needed to get a unique name
    set {divider, counter} to {"_", 0} -- the dividing text and starting suffix number
    set leadingZeros to 2 -- maximum leading zeros
    set here to -(offset of "." in ((reverse of text items of someName) as text)) - 1 -- split extension at last period
    set theName to text 1 thru here of someName
    if here is -1 then -- no extension
        set theExtension to ""
        set theExtension to text (here + 1) thru -1 of someName
    end if

    if counter < 1 then -- always start with a suffix
        set counter to 1
        if leadingZeros > 0 then set counter to text -(leadingZeros + 1) thru -1 of ("000000" & counter)
        set newName to theName & divider & counter & theExtension
    else -- only add suffix as needed
        set counter to counter - 1 -- adjust for while loop
        set newName to theName & theExtension
    end if
    tell application "System Events" to tell (get name of items of folder (someFolder as text) whose visible is true)
        repeat while it contains newName
            set counter to counter + 1
            if leadingZeros > 0 then set counter to text -(leadingZeros + 1) thru -1 of ("000000" & counter)
            set newName to theName & divider & counter & theExtension
        end repeat
    end tell

    return newName
end getUniqueName
  • Works great. Thanks. FWIW, the filename is saved as mp3 but it is still aiff (PCM). I don't support that there is another flag to specify the format?
    – jakethedog
    Mar 6 '20 at 9:36
  • I've updated my answer to use the say shell script, although it doesn't look like there is a built-in conversion to mp3.
    – red_menace
    Mar 6 '20 at 21:31

You must log in to answer this question.

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