Here’s a script to start handbrake automatically when a disc is inserted.
since handbrake has no dictionary, keystrokes are used although the delay 20 seconds command doesn’t work so well since I don’t know how long it will take the disc to load.
How can I improve this?
name of disc is found in the previous section
tell application "HandBrake"
activate
delay 2
tell application "System Events" to keystroke nameofdisc
tell application "System Events" to keystroke return
delay 20
tell application "System Events" to keystroke "t" using command down
delay 2
repeat with i from 1 to 6
tell application "System Events" to keystroke tab using shift down
end repeat
tell application "System Events" to keystroke nameofdisc & ".mp4"
tell application "System Events" to keystroke "s" using command down
end tell
I’m not sure how this command works but the applescript activates when the disc appears on the desktop.
After starting up handbrake, it needs to be loaded into handbrake which takes between 10 seconds and maybe a couple of minutes.
Sorry, Isn’t this command to be used outside of the handbrake subroutine?
if so, I launched the applescript when a dvd is inserted in the system preferences under dvd so it seems to be already incorporated.
If not, I don’t understand how to incorporate it into the handbrake subroutine portion of the script?
I don’t use Handbrake but I just downloaded it.
As I can see, the pop up button Title is disabled while scanning the DVD.
This script waits until the pop up button is enabled.
You could also check the value of the text field Source:
activate application "HandBrake"
tell application "System Events"
tell process "HandBrake"
repeat until (enabled of pop up button 1 of window 1)
delay 2
end repeat
end tell
end tell
Note. there’s also a command line version, which is probably easier to handle
tell application "System Events"
try
set DiskName to name of (1st disk whose ejectable is true and free space is 0)
on error
set DiskName to ""
end try
end tell
set capdiskname to "path to copy location" & DiskName & ".mp4"
tell application "HandBrake"
activate
delay 2
tell application "System Events" to keystroke DiskName
tell application "System Events" to keystroke return
end tell
tell application "System Events"
tell process "HandBrake"
repeat until (enabled of pop up button 1 of window 1)
delay 2
end repeat
end tell
end tell
tell application "HandBrake"
tell application "System Events" to keystroke "t" using command down
delay 2
repeat with i from 1 to 6
tell application "System Events" to keystroke tab using shift down
end repeat
tell application "System Events" to keystroke capdiskname
tell application "System Events" to keystroke "s" using command down
end tell
Here a little optimized version.
GUI scripting should always look like
activate application "whatever"
tell application "System Events" to tell process "whatever" to do something
try
tell application "System Events" to set DiskName to name of (1st disk whose ejectable is true and free space is 0)
on error
set DiskName to ""
end try
set capdiskname to "path to copy location" & DiskName & ".mp4"
activate application "HandBrake"
delay 2
tell application "System Events"
tell process "HandBrake"
keystroke DiskName
keystroke return
repeat until (enabled of pop up button 1 of window 1)
delay 2
end repeat
keystroke "t" using command down
delay 2
repeat with i from 1 to 6
keystroke tab using shift down
end repeat
keystroke capdiskname
keystroke "s" using command down
end tell
end tell
for completion the CLI version with default settings (HandBrakeCLI must be installed)
on video DVD appeared theObject
set DiskName to text 1 thru -2 of (theObject as text)
set theSource to quoted form of ((POSIX path of theObject as text) & "VIDEO_TS")
do shell script "/path/to/HandBrakeCLI -i " & theSource & " -o " & quoted form of (POSIX path of "path to copy location" & DiskName & ".mp4")
end video DVD appeared
I’ll have to chew on that one for a while because I don’t understand shell script but I tried writing a little back end to this script that would turn off handbrake and eject the disc but I’ve run into a couple of problems.
I’m stuck at finding out how to know when the program is done.
you can set in the preferences to display a dialog that says
and a button that says
The second issue is my keystroke q command which makes the script editor quit and not handbrake.
tell application "System Events"
tell process "HandBrake"
keystroke "q" using command down
end tell
end tell
tell application "Finder" to eject disk DiskName
and this is not working for the button
tell application "System Events" to tell (first button whose name is "ok") of (first window of application process "handbrake") to if exists then click
That’s the part that does work, however it needs to fire when the dialog box comes up.
I believe this is the dialog box
display alert "Put down that cocktail..." message "your handbrake queue is done!" buttons {"OK"} default button 1
The command q was supposed to turn off handbrake and i’m stumped at the best way to check for the dialog box.
I could check every few seconds for it however that seems like a lot of cpu time in a process that may take several hours.
path to copy location must be a HFS path (colon separated) /path/to/HandBrakeCLI must be a POSIX path (slash separated) and the CLI must be located there
yes, I did and tried several separate commands.
I made a folder called Handbrakecli inside my applications folder and inserted Handbrakecli
set theSource to quoted form of ((POSIX path of theObject as text) & "VIDEO_TS")
do shell script "/Applications/HandBrakeCLI/HandBrakeCLI -i " & theSource & " -o " & quoted form of (POSIX path of "/Volumes/volname/volnamemovies/" & DiskName & ".mp4")
and
do shell script "Handbrake" & "/Applications/HandBrakeCLI/HandBrakeCLI -i " & theSource & " -o " & quoted form of (POSIX path of "/Volumes/volname/volnamemovies/" & DiskName & ".mp4")
and this one to set the criteria for copying and testing independently
set SourceFIle to quoted form of "/source/file/path name"
set DestinationFile to quoted form of "destination/file/path name"
set ScriptText to "/Applications/HandBrakeCLI/HandBrakeCLI -i " & SourceFIle & " -o " & DestinationFile & " -L -f mp4 -m -e x264 -b 2500 -2 -d -B 160 -T;"
do shell script ScriptText
and to set normal preset
do shell script "Handbrake" & "/Applications/HandBrakeCLI/HandBrakeCLI -i " & theSource & " -o " & quoted form of (POSIX path of "/Volumes/volname/volnamemovies/" & DiskName & ".mp4") "--preset='Normal'"
I’m not sure if any have worked since I am not getting any computer feedback.