Stan
Stan

Reputation: 11

Automator Service for Terminal commands with variables

I'm an artist looking for automating some processes. Unfortunately, I'm too far from coding to wrap my head around even this seemingly elementary problem. I'm crying for professional's help with this task:

I need to create a service via Automator that will re-encode video files with FFMPEG. It should basically do what I'm doing manually when executу in Terminal a line like this:

ffmpeg —i FILE_NAME.EXTENSION -vf "vflip,hflip" -an FILE_NAME_converted.MP4

When this script is wrapped into the service FILE_NAME must be substituted with the file the service is applied to. How can I use such variables in AppleScript of Shell-script? I would highly appreciate any advice or even step-by-step instruction. Thank you!

Upvotes: 0

Views: 140

Answers (1)

Stan
Stan

Reputation: 11

I was able to figure it out. Here is the AppleScript code that works for me:

on run {input, parameters}
    tell application "Finder"
        set theWin to window 1
        set thePath to (POSIX path of (target of theWin as alias))
        set theFile to name of file input
    end tell

    tell application "Terminal"
        activate
        set currentTab to do script ("cd " & thePath)
        do script ("ffmpeg -i " & theFile & " -vf \"vflip,hflip\" -an " & theFile & "_converted.mp4") in currentTab
        set frontWindow to window 1
        repeat until busy of frontWindow is false
            delay 1
        end repeat
        quit
    end tell
end run

Upvotes: 1

Related Questions