Warm tip: This article is reproduced from stackoverflow.com, please click
applescript ffmpeg homebrew install xcode

Install ffmpeg with applescript

发布于 2020-04-10 10:07:15

I'm trying to create an applescript that installs ffmpeg. I have two issues.

  • I want to install Xcode, homebrew, ffmpeg, node, grant permissions, and ffmpeg-progressbar-cli one at a time. Not all at once but in that order. ffmpeg is dependant on xcode so it needs to wait before xcode is done installing.
  • The command for homebrew requires " which applescript makes me change to a ' before I can run it but in this case it doesn't work.

Here is the script I have so far.

tell application "Terminal"
    do script "xcode-select --install && ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" && brew install ffmpeg && brew install node && sudo chown -R $(whoami) /usr/local/bin /usr/local/etc && npm install --global ffmpeg-progressbar-cli"
    activate
end tell

I tried this and it doesn't seem to work as intended.

tell application "Terminal"
        do script "sudo chown -R $(whoami) /usr/local/bin /usr/local/etc && xcode-select --install"
        display dialog "Select OK once Xcode has installed" buttons {"OK"} default button 1
        do script "ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)""
        display dialog "Select OK once Homebrew has installed" buttons {"OK"} default button 1
        do script "brew install ffmpeg"
        display dialog "Select OK once ffmpeg has installed" buttons {"OK"} default button 1
        do script "brew install node"
        display dialog "Select OK once node has installed" buttons {"OK"} default button 1
        do script "npm install --global ffmpeg-progressbar-cli"
        display dialog "Select OK once ffmpeg-bar has installed" buttons {"OK"} default button 1
        activate
    end tell

For the second issue it needs to be

ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

but applescript makes me change it to

ruby -e '$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)'

with single quote which doesn't work.

Help and suggestions apreciated!

Questioner
Andy Tailor
Viewed
72
Ted Wrigley 2020-02-03 01:16

Ok, try the following script, but see caveats below:

shellScriptHandler("xcode-select --install", false)
shellScriptHandler("sudo chown -R $(whoami) /usr/local/Cellar", true)
shellScriptHandler("ruby -e \"$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)\" <<< " & return, false)
shellScriptHandler("sudo chown -R $(whoami) /usr/local/var/homebrew /usr/local/share/zsh /usr/local/share/zsh/site-functions", true)
shellScriptHandler("/usr/local/Homebrew/bin/brew install ffmpeg", false)
shellScriptHandler("/usr/local/Homebrew/bin/brew install node", false)
shellScriptHandler("sudo chown -R $(whoami) /usr/local/bin /usr/local/etc", true)
shellScriptHandler("/usr/local/bin/npm install --global ffmpeg-progressbar-cli", false)

on shellScriptHandler(command, usesAdmin)
    log command
    (*
        The try block catches any errors and reports them; the user can choose 
        to continue execution if it's a warning, or cancel execution outright.

        The 'eval' statement forces do shell script to read the standard
        interactive $PATH variable, so that it sees /user/local/, otherwise it 
        can't find the files it needs.
    *)
    try
        do shell script "eval `/usr/libexec/path_helper -s`; " & command administrator privileges usesAdmin
    on error errstr
        display dialog errstr buttons {"Continue", "Cancel"} default button "Continue"
    end try
end shellScriptHandler

Caveats:

  1. If there is a previous install of homebrew, the first line may fail with a permissions error; on my system, it tried to copy something from the Xcode bundle to /usr/local/Homebrew/.git/..., but needed root privileges. I solved that problem by deleting the folder /usr/local/Homebrew entirely, but that might be a bit drastic for the general case. You'll have to see if the problem recurs.
  2. I added a couple of chown commands in lines 2 and 4. These popped up as errors when I ran the ruby script and brew install.... You may run across other such errors based on idiosyncrasies in different systems, but it shouldn't hurt to add more such commands to straighten out the permissions in /usr/local.
  3. This script should run with a minimum of user interaction, but it does need the occasional 'ok' or password entry. Those could be automated, but I'm not sure it's worth it.