AppleScript for Drop-converting Blu-ray Rips for Streaming

Requirements

  • ffmpeg (install using default Homebrew configuration)
  • One or more .mkv  or .m2ts files ripped from Blu-ray using MakeMKV.

This method preserves 5.1 audio, so if you use a home streaming environment such as Apple TV, the audio can be delivered in Surround Sound.

You can use Rob Griffith’s Blu-ray ripping recommendations to get a compatible .mkv file. I have been able to get the necessary source file by analyzing the Blu-ray disc (you must have a Blu-ray capable CD/DVD disc reader, such as a Buffalo, attached to your Mac) using MakeMKV and selecting the title with the greatest number of chapters and including a 7.1/5.1 audio stream for ripping. If you back up the entire Blu-ray disc, you should also be able to locate the correct .m2ts file and drop it instead. The output file of this AppleScript application is created in the same folder as the dropped source file(s). The process will open a Terminal window which displays the progress of the conversion process. Until you’re used to the process, it’s probably a good idea to press ⌘-. (command – period) while the Terminal window is frontmost a minute or two into the conversion process so that you can audition the output video to be sure it’s what you wanted. If it’s not the right title, or doesn’t contain the correct audio program, you can adjust the MakeMKV ripping configuration settings and try again.

Create the AppleScript applet by copying and pasting the following code into the Script Editor included with a recent Mac system version such as Mojave or High Sierra and saving as an application. You may then simply drag and drop one or more .mkv files created using MakeMKV. I rarely do more than one at a time, but if your Mac has enough disk space to accommodate several, and you have time to let your Mac complete the process, you can convert multiple files with a single drop.

The AppleScript includes a construct for determining the proper crop values to be applied for the version. It is based on determining the video dimensions at a point that is 44 seconds into the video. If you encounter incorrect crop values for the converted video, you may wish to change the value to a higher or lower number. The adjustment should be made to the script by changing the default value specified (“-t 44”) to something else such as (“-t 25”).

--begin AppleScript--


property temppath : "/private/tmp/"
property startnum : 0
property newline : ASCII character 10
property tmpfile : "/tmp/execme.command"

on open the_items
my video_convert(the_items)
end open

on video_convert(the_items)
set theshellscript to ""
repeat with the_item in the_items
set the_item to the_item as alias
try
tell application "Finder"
set sost to (container of the_item) as string
end tell
set pos_filepath to POSIX path of sost
end try
set this_filepath to (the_item as string)

if last character of this_filepath is ":" then
tell me to set it_is_a_folder to true
else
set it_is_a_folder to false
end if
set thesourcename to (name of (info for the_item))
set namepart to (name extension of (info for the_item))
set the_source_file to POSIX path of this_filepath
set newname to replace_chars(thesourcename, namepart, "m4v")
set autocrop to (do shell script "/usr/local/bin/ffmpeg -i" & space & (quoted form of the_source_file) & space & "-t 44 -vf cropdetect -f null - 2>&1 | awk '/crop/ { print $NF }' | tail -1")
try
set theshellscript to the theshellscript & "/usr/local/bin/ffmpeg" & space & "-i" & space & (quoted form of the_source_file) & space & "-pix_fmt yuv420p -c:v libx264 -preset slow -tune film -vf \"" & autocrop & "\" -c:a ac3 -ac 6 -ab 640k -crf 18 -x264opts keyint=40:bitrate=2400:qpmin=8:qpmax=28:qpstep=4" & space & (quoted form of (pos_filepath & newname)) & ";/bin/echo '

==========================

" & newname & space & "FINISHED!" & "

==========================

';"
on error onerr
activate
display dialog onerr
end try

end repeat
set theshellscript to theshellscript & "mv" & space & (quoted form of tmpfile) & space & (quoted form of (POSIX path of (path to trash)))
do shell script "echo " & quoted form of theshellscript & " > " & tmpfile
repeat
try
do shell script "chmod +x " & tmpfile
do shell script "open -a Terminal.app" & space & tmpfile
exit repeat
on error
delay 1
end try
end repeat
end video_convert

on replace_chars(this_text, _bad, _good)
set AppleScript's text item delimiters to the _bad
set the item_list to every text item of this_text
set AppleScript's text item delimiters to the _good as string
set this_text to the item_list as string
set AppleScript's text item delimiters to ""
return this_text
end replace_chars

on run
set the_items to ((choose folder) as list)
video_convert(the_items)
end run

--end AppleScript--

 

Advertisements

AppleScript to delete APFS Local Snapshots

Here’s an AppleScript I’ve been using to view listings for local snapshots then delete unwanted ones for my APFS boot volume in High Sierra. You’ll be prompted for your admin password for deleting snapshots.

set IDList to paragraphs of (do shell script "tmutil listlocalsnapshots /")
set BL_1 to {}
repeat with i in IDList
set end of BL_1 to ((word 2 of i & "-" & word 3 of i & "-" & word 4 of i & "-" & word 5 of i) as text)
end repeat
if (count of BL_1) is not 0 then
choose from list BL_1 with prompt "Select snapshots to delete. Select as many as desired. Enter administrator password when prompted." with multiple selections allowed
set theSelection to result
if (count of theSelection) is not 0 then
repeat with theItem in theSelection
do shell script ("tmutil deletelocalsnapshots" & space & theItem) with administrator privileges
end repeat
end if
else
display dialog "There appear to be no snapshots to delete." buttons {"OK"} default button 1
end if

 

AppleScript: Clipboard to text file on Desktop

Clipping files created by dragging a text selection to the desktop are very useful, but sometimes it’s more beneficial to create a text file. Here’s an AppleScript that does that. The script is most conveniently used by activating it with a keyboard shortcut or other shortcut rather than having to find it in your Script menu. One way to make it quickly accessible is to place it in the toolbar of Finder windows. The way I run it is to use ⇧⌃⌥L (Shift-Ctrl-Opt L) in Keyboard Maestro. You could also put it into the Script Menu and launch it using Daniel Jalkut’s FastScripts, or you could even create a System Service from the AppleScript using Automator and configure the keyboard trigger in the Shortcuts tab of the Keyboard sysprefpane (System Preferences> Keyboard). Whatever way you choose to run it, all you need to do is make sure the text you want turned into a file is copied to your clipboard first. The Finder window toolbar and Automator System Service methods are built into the Mac system and are completely free. Keyboard Maestro and FastScripts are commercial automation solutions, but both are well worth the investment.

 

property temppath : "/private/tmp/"

property startnum : 0

property clipname : "clip"

property add_on : ".txt"

activate

set clipname to (text returned of (display dialog "Enter a description of the clipboard text that will be used to name the file:" default answer clipname))

set rootpath to ((path to desktop) as Unicode text)

set thepathname to rootpath & clipname

set pos_filepath to POSIX path of rootpath as Unicode text

set dest_file to (quoted form of (temppath & clipname & add_on))

do shell script "pbpaste >" & space & dest_file

tell application "Finder"

set basename to clipname

repeat

try

set name_target to alias (rootpath & clipname & add_on) --file name is already taken at destination if no error occurs.

set startnum to startnum + 1

set clipname to text returned of (display dialog"A file with that name already exists. You may enter a new name or cancel."default answer (basename & startnum))

on error _error

set startnum to 0

if _error contains clipname then --if target is clear and source file can be copied from temp directory

tell me to do shell script"ditto" & space & dest_file & space & (quoted form of (pos_filepath & clipname & add_on))

exit repeat

else -- if action is cancelled by user

exit repeat

end if

end try

end repeat

end tell

Keysinkers

If you ever get tired of holding down a key or a combination of keys to make your Mac observe any of several modes or behaviors at startup, you might be interested in preparing a few keysinkers. For example, you could use a couple of keysinkers to boot into Recovery Mode while you go fetch a cup of coffee. You can readily construct keysinkers using cheap fishing tackle (line sinkers) and Sugru or Oogoo.

Just adapt the bottom of the sinker to give it a flat surface and make it lean enough to accommodate the angle of your particular keyboard’s surface. I’ve been able to compromise the angle so that my keysinkers can remain stable for different degrees of keyboard tilt.

Illustration of keysinkers on Mac keyboard keys

 

 

Droplet for How Long an App has been Running

The Robservatory blog featured a method for finding out how long an app has been running. Here’s the source for an AppleScript application that lets you drag and drop your running app’s icon to see that information. Put this script into an AppleScript Editor (currently known as “Script Editor“) document, then save it as an application. Don’t copy the line numbers. Be sure your app is running, then drag that app’s icon onto the new app you created.

Warning: If the running process of the app doesn’t have the same name as the app’s icon (excluding the “.app” extension, of course), the droplet won’t work.

Previously, I discovered a problem with some apps with longer names, and provided a revised version. Now I’ve added the ability to select an app icon in the Finder and launch this app from the Dock or the Script menu for those of you who might prefer not to drag your app icons.

on open the_item
	my build_answer(the_item)
end open

on run
	tell application "Finder"
		--activate
		set the_item to get selection as string
	end tell
	set the_item to get the_item as alias
	my build_answer(the_item)
end run

on build_answer(the_item)
	--	tell me to activate
	set item_info to get info for the_item
	set _choice to characters 1 thru -5 of (displayed name of item_info) as string
	try
		set _pids to paragraphs of (do shell script "/bin/ps -acxwwl")
		set ls to {}
		set ps to _choice
		repeat with _line in _pids
			if _line is "" or _line contains "TIME COMMAND" then
			else
				set _pid to 2nd word of _line
				set s to last text item of _line
				set n to seventh word of _line
				if s is "t" then set s to "init"
				tid("     ")
				set _process to s
				set _ful to _pid & tab & n & tab & _process
				if _ful ends with ps then
					set end of ls to (_pid & tab & n & tab & _process) as string
				end if
			end if
		end repeat
		tid(tab)
		set _pit to item 1 of ls
		set _pid to word 1 of _pit
		set _uptime to (do shell script "ps -o etime= -p" & space & quoted form of _pid)
		set time_val to count words of _uptime
		set _secs to word time_val of _uptime
		set _mins to word (time_val - 1) of _uptime
		if (time_val - 2) > 0 then
			set _hours_string to ((time_val - 2) as string) & space & "hour(s)," & space
		else
			set _hours_string to ""
		end if
		if (time_val - 3) > 0 then
			set _days_string to ((time_val - 3) as string) & space & "day(s)," & space
		else
			set _days_string to ""
		end if
		display dialog "The" & space & _choice & space & "app has been running for" & space & _days_string & _hours_string & _mins & space & "minute(s)" & space & "and" & space & _secs & space & "second(s)." buttons {"OK"} default button 1
	on error
		display dialog "Something went wrong. Are you sure the app of the icon you dropped is really running?." buttons {"OK"} default button 1
	end try
end build_answer

on tid(s)
	set my text item delimiters to s
end tid

You can still remove annoying agreement prompts from Mac disk images (.dmg files)

I got tired of accepting agreement notices when opening distributed disk image (.dmg) files, so I created this script. It should work until certain people decide it shouldn’t. Create a MacOS Service using Automator with one Run AppleScript action, and make the service accept documents in Finder. Paste the following AppleScript into the Run AppleScript action and save. Here’s the shortest complete documentation I could find (written by Ben Waldie, an extremely proficient Mac automation enthusiast) that explains how to use Automator to quickly deploy AppleScripts as MacOS Services.

After you’ve added the MacOS Service, when you encounter a disk image that you’ve downloaded that wants you to agree to something before it opens, just click “disagree”, then launch the service from the contextual menu (right-click the disk image). After a few seconds (depending on the size of the image), you can double-click it and it will open with no further nagging.

------
property temppath : "/private/tmp/"

on run {input}

 (* Your script goes here *)
 repeat with the_item in input
 build_archive(the_item)
 end repeat
end run

on build_archive(the_item)
 --repeat with the_item in the_items
 set the_item to the_item as alias
 try
 tell application "Finder"
 set sost to ((container of file (the_item as string)) as alias) as string
 end tell
 set sost to POSIX path of sost
 on error therrr
 log therrr
 display dialog therrr
 set sost to "/Volumes/"
 end try
 set this_filepath to (the_item as string)

 if last character of this_filepath is ":" then
 tell me to set it_is_a_folder to true
 else
 set it_is_a_folder to false
 end if
 if not it_is_a_folder then
 set thesourcename to (name of (info for the_item))
 set the_source_file to POSIX path of this_filepath
 set pos_filepath to sost
 --set thesourcename to replace_chars(thesourcename, " ", "_")
 set dest_file to (quoted form of (temppath & thesourcename))
 set tempname to ((characters 1 thru -5 of thesourcename) & "temp")
 try
 set my_command to "hdiutil convert -format UDRW" & space & (quoted form of the_source_file) & space & "-o" & space & (quoted form of (temppath & tempname))
 do shell script my_command
 set my_2nd_command to "hdiutil convert" & space & (quoted form of (temppath & tempname & ".dmg")) & space & "-format UDZO -imagekey zlib-level=9 -o" & space & (quoted form of (temppath & thesourcename))
 do shell script my_2nd_command
 do shell script "rm" & space & (quoted form of (temppath & tempname & ".dmg"))
 on error onerr
 activate
 display dialog onerr
 end try

 if pos_filepath is "/Volumes/" then
 set pos_filepath to (POSIX path of (path to desktop folder))
 end if
 set targdetect to (pos_filepath & thesourcename)
 set name_target to alias (POSIX file targdetect)
 --use this instead of above to prevent overwriting your original disk image: set finishname to "_recomp"
 set finishname to ""
 set thesourcename to (((characters 1 thru -5 of thesourcename) & finishname) as Unicode text)
 tell me to do shell script "ditto -rsrcFork" & space & dest_file & space & "\"" & (pos_filepath & thesourcename) & ".dmg" & "\""
 end if
 try
 do shell script "rm" & space & dest_file
 end try
 --end repeat
end build_archive
------