Question

Request assistance with truncating/trimming a string in an automator action used to create text-to-speech audio files from a text selection ranging from 30 or so characters to 800+, which is too long for a file name.

In short, I am trying to truncate a string to 30 characters and pass that to the "Text to Audio File" action as the file name.

Basic workflow is:

Select text Initiate following Automator Action via Services Menu

  1. "Set Value of Variable" to input (e.g. selected text) and define as TextToSpeech
  2. "Set Value of Variable" to input (e.g. selected text) and define as FileName
  3. "Get Value of Variable" FileName
  4. "Run AppleScript"

    on run {input, parameters}
        set theResult to input as string
        set finalResult to input as string
        set txtLength to (length of theResult)
        if txtLength > 30 then
            set finalResult to (characters 1 thru 30 of theResult) as string
        end if
        return finalResult
    end run
    
  5. "Set Value of Variable" input (e.g. selected text) and define as FileName

  6. "Get Value of Variable" TextToSpeech
  7. "Text to Audio File" with Save As: set to "FileName"
  8. "Encode to MPEG Audio"

Any assistance/suggestions is greatly appreciated!

Regards,

Zephyr

Was it helpful?

Solution

In general you pass from the applescript to the next action whatever it needs using the "return" command at the end of the code. In your case though the automator action "Text to Audio File" doesn't accept a fileName variable so if you want that much control you need another method. Luckily that automator action can be replaced easily in the applescript code with a simple "say" command.

So create your automator service and receive the selected text. Then add an applescript action and use the following as the code. Then add an "Encode to MPEG audio" action.

For the applescript code just modify the voiceName and saveFolder variables with values of your choosing. The saveFolder path must end with a colon (:). Note that I use 26 instead of 30 because we add ".aif" to the end of the filename... to get a total of 30 characters.

on run {input, parameters}
    set voiceName to "Jill"
    set saveFolder to path to desktop as text

    set selectedText to item 1 of input
    if (length of selectedText) > 26 then
        set fileName to text 1 thru 26 of selectedText
    else
        set fileName to selectedText
    end if
    set fileName to fileName & ".aif"
    set filePath to saveFolder & fileName

    say selectedText using voiceName saving to file filePath

    return {POSIX path of filePath}
end run

If you need to determine your saveFolder use this to get the path. Run this code and copy/paste the result into the saveFolder variable above.

(choose folder) as text

OTHER TIPS

Here's what worked:

Select text

Initiate following Automator Action via Services Menu

"Set Value of Variable" to input (e.g. selected text) and define as TextToSpeech

"Run AppleScript"

on run {input, parameters}
    set theResult to input as string
    set finalResult to input as string
    set txtLength to (length of theResult)
    if txtLength > 50 then
        set finalResult to characters 1 thru 50 of theResult as string
    end if
    return finalResult as string
end run

"Set Value of Variable" input (e.g. selected text) and define as FileName

"Get Value of Variable" TextToSpeech and ignore this action's input

"Text to Audio File" with Save As: set to "FileName" and the save as location set.

"Encode to MPEG Audio"

Result: an audio file of the selected text spoken by the desired voice with a file name set to the first 50 characters of the selected text.

( I increased to characters to lessen the number of duplicate file names I was getting as part of my workflow.)

@regulus6633 : your answer is more compact, and elegant, but I need to learn more about how applescript gets it's inputs and returns it's results. Thanks again.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top