Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Source bash script from Applescript

Hey all,


So have a question about sourcing a file.sh script from applescript.


Currently I have the following bash script I made for upon entering a DVD it rips and encodes with HandbrakeCLI. Which when I call it with ./HBcli.sh it runs and works perfectly fine. I do have it as executable too with: chmod +x HBcli.sh

------------------------------------------------------------------

#!/bin/sh

# Get title of DVD

DVD_TITLE=$(df -h | grep dev/disk3 | grep Volumes | awk '{print $9}' | cut -c10-90)

FILE=$DVD_TITLE

SRC=$(df -h | grep dev/disk3 | grep Volumes | awk '{print $9}')

DEST=~/Desktop/Converted


HB_PRESET='--preset="Normal"'


HB=HandBrakeCLI


#echo $DEST/$FILE.mp4

#echo $HB_PRESET


$HB -i $SRC -o $DEST/$FILE.m4v $HB_PRESET


------------------------------------------------------------------

Now I have the Applescript that is set up like so:


------------------------------------------------------------------

--Tried this first

tell application "Finder"

do shell script "./HBcli.sh" -- or "do shell script "source HBcli.sh"

end tell
-----------------------------------------------------------------


I then proceeded to try it all via Applescript without the script itself
---------------------------------------------

tell application "Finder"

set DVD_TITLE to do shell script "df -h | grep dev/disk3 | grep Volumes | awk '{print $9}' | cut -c10-90"

set theFile to DVD_TITLE as string

set SRC to do shell script "df -h | grep dev/disk3 | grep Volumes | awk '{print $9}'"

set DEST to "~/Desktop/Converted" as string



set HB to "/opt/homebrew-cask/Caskroom/handbrakecli/0.10.2/HandBrakeCLI"

do shell script "" & HB & " " & SRC & " -o " & DEST & "/" & theFile & ".mp4 --preset='normal'"


-- Gave the following error: error "Finder got an error: Missing input device. Run /opt/homebrew-cask/Caskroom/handbrakecli/0.10.2/HandBrakeCLI --help for syntax." number 1



--set HB to "HandBrakeCLI"


--do shell script "" & HB & " " & SRC & " -o " & DEST & "/" & theFile & ".mp4 --preset='normal'"


-- Gave the following error: error "Finder got an error: sh: HandBrakeCLI: command not found" number 127



do shell script "" & HB & " " & SRC & " -o " & DEST & "/" & theFile & ".mp4 --preset='normal'"


end tell

---------------------------------------------


1) What is the proper wait to call the script from Applescript?

2) If I am doing mostly correct, what can I do to correct the issues?

3) Is automated able to pull and use bash scripts also.


Also, if you have any recommendations or methods of creating a script or Applescript.


I run a mini OS X 10.11.1

Mac mini, Other OS, 10.10.4

Posted on Oct 30, 2015 4:51 PM

Reply
7 replies

Oct 30, 2015 5:43 PM in response to JoshFree

The shell that AppleScript's do shell script uses, does not read your PATH environment variable, and accordingly, does not know about your home directory, or other folders there. My PATH environment variable is set from my .bashrc file in my home directory, so if I want any local script to work, I have to do the following - assumption that HBcli.sh is in your home directory, otherwise you would have to be path specific.


do shell script "source ~/.bashrc;./HBcli.sh"


If your script took arguments, you would add these quoted, and or spaced accordingly after your script name. If you were expecting output from the script, then you would set your do shell script to a variable in AppleScript.

Oct 30, 2015 7:27 PM in response to VikingOSX

Okay closer. When I updated the .bashrc with the PATH variable and ran it again. (Yes the script is in the home folder /Users/username/)

I got:

error "TERM environment variable not set.

sh: HandBrakeCLI: command not found" number 127



I read you can add export TERM=xterm-color to your .bashrc, which I have tried, but it still throws that error as well.

Oct 31, 2015 6:16 AM in response to JoshFree

I do not use applecript but the following looks obvious-


set HB to "/opt/homebrew-cask/Caskroom/handbrakecli/0.10.2/HandBrakeCLI"

do shell script "" & HB & " " & SRC & " -o " & DEST & "/" & theFile & ".mp4 --preset='normal'"


-- Gave the following error: error "Finder got an error: Missing input device. Run /opt/homebrew-cask/Caskroom/handbrakecli/0.10.2/HandBrakeCLI --help for syntax." number 1


You forgot the "-i" option in HandBrakeCLI.

--set HB to "HandBrakeCLI"


--do shell script "" & HB & " " & SRC & " -o " & DEST & "/" & theFile & ".mp4 --preset='normal'"


-- Gave the following error: error "Finder got an error: sh: HandBrakeCLI: command not found" number 127


Applescript 's "do shell script" inherits the PATH variable available to Applescript which is /usr/bin:/bin:/usr/sbin:/sbin. It does not read any shell configuration files. Use the absolute path to HandBrakeCLI.


Oct 31, 2015 7:11 AM in response to JoshFree

You do not need to export a TERM environment variable in your .bashrc file. I used to, but no longer. Recent Terminal.app support 256 colors without being told. I do, however, declare xterm-256 in the advanced tab for Terminal Preferences. This still may be no more useful than a vestigial notion.


You probably guessed that I have forced the do shell script to use information configured in the .bashrc file over system defaults. Although the system will look in /usr/local/bin by default, it will not see into specialized package manager (homebrew, etc.) installation paths without explicit help.

Good catch by Mark, by the way, on that missing HandbrakeCLI option switch.

Oct 31, 2015 9:08 AM in response to JoshFree

I would NOT source ~/.bashrc, because it is used to set too many things that may be setup for an interactive session, that is not idea for a non-interactive terminal keyboard script.


Either use a full path to target items or just set PATH to include your home directory. But personally, from years of writing lots of 1000+ line shell scripts, it is better to just make the script self sufficient and and use full path where the default PATH does not include the command you want.


Your Applescript PATH is most likely

PATH=/usr/bin:/bin

or it might be

PATH=/usr/bin:/bin:/usr/sbin:/sbin

but it will not include your home directory.


I would use

do shell script "$HOME/HBcli.sh"

or

do shell script "~/HBcli.sh"

or

do shell script "/Users/yourShortUserNameHere/HBcli.sh" -- I do not really like this, but it should work

Source bash script from Applescript

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.