New Version of PPC Media Center Universal Binary (10.4-10.8 PPC/Intel)

While back posted about my web-video player/downloader/mp3-maker:

http://www.macscripter.net/viewtopic.php?id=43892

And here is version 6.0 - most likely final version for Mac (moving the project to qt for Mac/PC/Linux). Will be up by about June 5th on http://ppcluddite.blogspot.com/2016/06/new-ppc-media-center-version-6.html *** UPDATE - Just added another Language-Localization comes in (German, Spanish & Italian) :smiley: so will have this up at PPC Luddite around the 8th. Thanks for your patience!***

¢ Much faster link-handling. All use of the old TrimLine hander and Apple Text Delimiter-functions were removed and replaced with shell-script Sed/Awk functions.

¢ In addition to the previous niceties, now also includes fully-automated playback for multi-segmented videos, normally separated by commercials and also for Youtube ListPlay/DL & Vimeo AlbumPlay/DL (opens each segment and goes to full-screen in QuickTime then closes when done and opens next).

¢ Features dynamic checking of Python version installed, and overrides in Intel systems that won’t allow update of system Python.

¢ Features new batch-downloader sub-application (downloads to dated folder on desktop) that runs in background behind normal functions, and the main play/download/mp3 functions may continue unaffected. with redundant check at end to catch stalled-downloads.

¢ Enhanced function dialogs and conflict checks.

¢ Enhanced WebM-avoidance for PPC systems not able to easily handle the format.

¢ Support for over 40 youtube-link variations.

¢ Offering Language Support Patches (starting with German).

¢ The Batch-Downloader has a modified bundle with renamed Unix Executable that shows up to System Events as “ppcBatch” enabling it to be force-quit and reset if Python should hang.

Main Application Script.


------------------------------------------------------------------------------------------------------------------------------------------------------------------
-- A G.U.I. for the Python Script youtube-dl (http://rg3.github.io/youtube-dl/). Written by Adam Albrec (game_creator@hotmail.com) --
------------------------------------------------------------------------------------------------------------------------------------------------------------------



-- Version 6.0 (Last Updated 05/30/2016) --



-- Checks system architecture to customize installation/execution and Sets Python version (in x86 systems corrects issues with failure of Python to update system version).
set endTest to (do shell script "sysctl -n hw.byteorder")
if endTest is "4321" then
	set archType to "PPC"
else
	set archType to "x86"
end if
set pythonVrsn to (do shell script "/usr/local/bin/python -V 2>&1 | sed 's/P/p/g;s/\\ //g' | cut -c1-9")
if archType is "x86" then
	set pythonVrsnNmbr to 0.0
	try
		set pythonVrsnNmbr to ((do shell script "echo " & quoted form of pythonVrsn & " | sed 's/[a-zA-Z]//g'") as real)
	end try
	if pythonVrsnNmbr is less than 3.4 then
		set pythonVrsn to "python3.4"
	end if
end if

-- Checks if PPCMC AutoDownloader is running (adds option to reset in main dialog if running).
set ppcBatchTest to {}
tell application "System Events"
	set ppcBatchTest to (get the name of (every process whose name contains "ppcBatch"))
end tell

-- Purges unneeded files in temp-directory (Runs only when no Terminal processes are using the directory)
tell application "System Events" to set terminalRunning to count of (every process whose name is "Terminal")
if terminalRunning is 0 then
	try
		do shell script "rm -R ~/Library/Application\\ Support/ppcmcTemp"
	end try
else
	set terminalInUse to the count (the windows of application "Terminal" whose name does not contain "bash")
	if terminalInUse is less than 1 then
		try
			do shell script "rm -R ~/Library/Application\\ Support/ppcmcTemp"
		end try
	end if
end if

-- Auto-updater for youtube-dl framework.
set dateCurrent to ((do shell script "date '+%m%d%Y'") as text)
set updateCurrentCheck to (do shell script "if test -e ~/Library/Preferences/ppcmcUpdated" & dateCurrent & "/; then echo 1; else echo 0; fi")
if updateCurrentCheck is not "1" then
	try
		do shell script "rm -R ~/Library/Preferences/ppcmcUpdated*"
	end try
	do shell script "/usr/local/bin/" & pythonVrsn & " /usr/local/bin/youtube-dl -U > /dev/null 2>&1 &"
	do shell script "mkdir ~/Library/Preferences/ppcmcUpdated" & dateCurrent & "/"
end if


-- Prevents time-out while waiting for user-response.
with timeout of 86400 seconds
	
	
	-- Captures URL/s from clipboard (deals with spaces, commas and line-breaks).
	set pageURL to ""
	try
		set pageURL to (the clipboard) as text
	end try
	set pageURL to do shell script "echo " & quoted form of pageURL & " | sed 's/ htt/\\
htt/g;s/\\ //g;s/,htt/\\
htt/g;s/,//g' | awk 'NF'"
	
	-- Prompts for URL if none is on clipboard.
	if pageURL does not contain "http" then
		beep
		tell me
			activate
			display dialog "A Video URL is Required to Proceed" with icon 2 buttons {"OK"} default button "OK"
		end tell
		error number -128
	end if
	
	-- Triggers 'PPCMC AutoDownloader' (batch downloader) if more than one URL is found on the clipboard).
	set pageURL_ListTest to {}
	set pageURL_ListTest to the paragraphs of pageURL
	set ppcBatch to quoted form of (POSIX path of (path to me)) & "Contents/Resources/PPCMC\\ AutoDownloader.app/"
	set batchTest to ""
	set batchTest to the number of items of pageURL_ListTest
	if batchTest is greater than 1 as real then
		if ppcBatchTest is not {} then
			beep
			tell me
				activate
				display dialog "PPCMC AutoDownloader is Already Running.

Please Try Again After it Finishes, 

OR 

Enter an Individual Video URL." with icon 2 buttons {"OK"} default button "OK"
			end tell
			error number -128
		else
			do shell script "open " & ppcBatch
			error number -128
		end if
	end if
	
	-- Video Quality settings for PPC systems.
	set videoQuality to ""
	set videoQualityOptions to {"-f 'best[ext!=webm][height<=?540]/best[ext!=webm][width<=1280]/[ext!=webm][height<=?1080]/best'", "-f 'best[ext!=webm][width<=1280]/[ext!=webm][height<=?1080]/best'"} as list
	
	
	----
	-- Requests desired URL to Play, Save  or Convert to MP3.
	----
	
	if archType is "PPC" then
		
		-- PPC
		if ppcBatchTest is {} then
			tell me
				activate
				set actionChoice to (choose from list {"Play - SD Normal", "Play - SD Theatré Mode", "Play - HD Normal", "Play - HD Theatré Mode", "Download - SD", "Download - HD", "Make MP3 (Delete Video)", "Make MP3 (Keep Video)"} with prompt "Welcome to PPC Media Center!" OK button name "OK" cancel button name "Cancel" default items "Play - SD Normal" without multiple selections allowed) as text
			end tell
		else
			tell me
				activate
				set actionChoice to (choose from list {"Play - SD Normal", "Play - SD Theatré Mode", "Play - HD Normal", "Play - HD Theatré Mode", "Download - SD", "Download - HD", "Make MP3 (Delete Video)", "Make MP3 (Keep Video)", "* Reset AutoDownloader"} with prompt "Welcome to PPC Media Center!" OK button name "OK" cancel button name "Cancel" default items "Play - SD Normal" without multiple selections allowed) as text
			end tell
		end if
		if actionChoice contains "- SD" then
			set videoQuality to the first item of videoQualityOptions
		else if actionChoice contains "- HD" then
			set videoQuality to the second item of videoQualityOptions
		end if
		if actionChoice is "False" then
			error number -128
		end if
		
	else
		
		-- x86
		if ppcBatchTest is {} then
			tell me
				activate
				set actionChoice to (choose from list {"Play - Normal", "Play - Theatré Mode", "Download", "Make MP3 (Delete Video)", "Make MP3 (Keep Video)"} with prompt "Welcome to PPC Media Center!" OK button name "OK" cancel button name "Cancel" default items "Play - Normal" without multiple selections allowed) as text
			end tell
		else
			set actionChoice to (choose from list {"Play - Normal", "Play - Theatré Mode", "Download", "Make MP3 (Delete Video)", "Make MP3 (Keep Video)", "* Reset AutoDownloader"} with prompt "Welcome to PPC Media Center!" OK button name "OK" cancel button name "Cancel" default items "Play - Normal" without multiple selections allowed) as text
		end if
		if actionChoice is "False" then
			error number -128
		end if
	end if
	
	-- Exits and resets PPC AutoDownloader & stops all downloads, if they become stuck.
	
	if actionChoice is "* Reset AutoDownloader" then
		try
			do shell script "killall ppcBatch"
		end try
		try
			do shell script "killall Python"
		end try
		error number -128
	end if
	
	-- Removes extraneous characters/formatting-variations in Youtube links, filters out Vimeo channels/groups code, and streamlines Google/Yahoo video search results.
	set playlistCheck to false
	set pageURL_Im to pageURL
	if pageURL_Im contains "youtu.be" then
		if pageURL_Im contains "?list=" then
			set pageURL_Im to do shell script "echo " & quoted form of pageURL_Im & " | sed 's/^.*?list=//g'"
		end if
		set pageURL to do shell script "echo " & quoted form of pageURL_Im & " | sed 's/%3A/\\:/g;s/%2F/\\//g;s/%3F/?/g;s/%3D/=/g;s/%26/\\&/g;s/attribution_link.*=\\///g;s/^.*.com\\///g;s/^.*.be\\///g;s/\\///g;s/watch?//g;s/playlist?//g;s/&v=//g;s/v=//g;s/list=//g;s/listType=.*&//g;s/feature=//g;s/share//g;s/player_embeddeds//g;s/player_embedded//g;s/embed//g;s/youtu.be//g;s/iv_load_policy=3//g;s/loop=[1,0]//g;s/modestbranding=[1,0]//g;s/showinfo=[1,0]//g;s/autoplay=[1,0]//g;s/rel=[1,0]//g;s/disablekb=[1,0]//g;s/controls=[1,0]//g;s/cc_load_policy=[1,0]//g;s/autohide=[1,0]//g;s/&.*$//g;s/?//g;s/=//g;s/start.*$//g;s/theme.*$//g;s/end.*$//g;s/color.*$//g;s/#t.*$//g'"
		set playlistCheck to true
	else if pageURL_Im contains "youtube.com" then
		if pageURL_Im contains "&list=" then
			set pageURL_Im to do shell script "echo " & quoted form of pageURL_Im & " | sed 's/watch?v=.*&//g'"
		end if
		set pageURL to do shell script "echo " & quoted form of pageURL_Im & " | sed 's/%3A/\\:/g;s/%2F/\\//g;s/%3F/?/g;s/%3D/=/g;s/%26/\\&/g;s/attribution_link.*=\\///g;s/^.*.com\\///g;s/^.*.be\\///g;s/\\///g;s/watch?//g;s/playlist?//g;s/&v=//g;s/v=//g;s/list=//g;s/listType=.*&//g;s/feature=//g;s/share//g;s/player_embeddeds//g;s/player_embedded//g;s/embed//g;s/youtu.be//g;s/iv_load_policy=3//g;s/loop=[1,0]//g;s/modestbranding=[1,0]//g;s/showinfo=[1,0]//g;s/autoplay=[1,0]//g;s/rel=[1,0]//g;s/disablekb=[1,0]//g;s/controls=[1,0]//g;s/cc_load_policy=[1,0]//g;s/autohide=[1,0]//g;s/&.*$//g;s/?//g;s/=//g;s/start.*$//g;s/theme.*$//g;s/end.*$//g;s/color.*$//g;s/#t.*$//g'"
		set playlistCheck to true
	else
		set pageURL to do shell script "echo " & quoted form of pageURL_Im & " | sed 's/http.*http/http/g;s/www.//g;s/%3A/\\:/g;s/%2F/\\//g;s/%3F/?/g;s/%3D/=/g;s/%26/\\&/g;s/vimeo.com\\/channels\\/.*\\//vimeo.com\\//g;s/vimeo.com\\/groups\\/.*\\/videos\\//vimeo.com\\//g'"
		if pageURL contains "vimeo.com" then
			set playlistCheck to true
		end if
	end if
	
	-- Adds '--' flag to escape first-character hyphens in Youtube strings.
	set hyphenFixer to ""
	if pageURL starts with "-" then
		set hyphenFixer to "-- "
	end if
	
	
	----
	-- Plays video in QuickTime.
	----
	
	if actionChoice contains "Play" then
		
		-- Gets video link from URL/s. Special playback handlers.
		set videoURL_data to paragraphs of (do shell script "/usr/local/bin/" & pythonVrsn & " /usr/local/bin/youtube-dl " & videoQuality & " --get-url " & hyphenFixer & "'" & pageURL & "'") as list
		
		-- If a video playback is only offered in WebM, prompts the user to cancel or download.
		if archType is "PPC" then
			set webmCheck to ""
			set webmCheck to videoURL_data as text
			if webmCheck contains ".webm" then
				beep
				tell me
					activate
					display dialog "Video URL is Only Available in WebM & Cannot be Streamed in QuickTime!" with icon 2 buttons {"Cancel", "Download Video"} default button "Download Video"
				end tell
				set webMChoice to button returned of the result
				if webMChoice is "Download Video" then
					--Sets up temp-folder for video being downloaded.
					set rdmString to ""
					repeat 7 times
						set rdmString to rdmString & some item of "abcdefghijklmnopqrstuvwxyz1234567890"
					end repeat
					
					-- Sets up appearance and launches Terminal window for download.
					tell application "System Events"
						set terminalRunning to count of (every process whose name is "Terminal")
						if terminalRunning is 1 then
							tell application "Terminal"
								activate
							end tell
							tell application "System Events" to keystroke "n" using command down
							tell application "Terminal"
								set background color of first window to "black"
								set cursor color of first window to "red"
								set normal text color of first window to "red"
								set bold text color of first window to "red"
							end tell
						else
							tell application "Terminal"
								activate
							end tell
							tell application "Terminal"
								set background color of first window to "black"
								set cursor color of first window to "red"
								set normal text color of first window to "red"
								set bold text color of first window to "red"
							end tell
						end if
					end tell
					tell application "Terminal"
						do script "clear; mkdir -p ~/Library/Application\\ Support/ppcmcTemp/" & rdmString & "/;  echo -e \"
\"; echo -e \"
Downloading WebM Video...\"; sleep 1; echo -e \"
\"; /usr/local/bin/" & pythonVrsn & " /usr/local/bin/youtube-dl " & videoQuality & " -o ~/Library/Application\\ Support/ppcmcTemp/" & rdmString & "/\"%(title)s.%(ext)s\" " & hyphenFixer & "'" & pageURL & "'; mv -f ~/Library/Application\\ Support/ppcmcTemp/" & rdmString & "/* ~/Desktop/ &> /dev/null; rm -R ~/Library/Application\\ Support/ppcmcTemp/" & rdmString & "/;  echo -e \"
\"; echo -e \"
- Download Complete -\"; sleep 1; echo -e \"
\"" in first window
					end tell
error number -128
				else
					error number -128
				end if
			end if
		end if
		
		set numberOfSegments to the number of items of videoURL_data
		if numberOfSegments > 1 then
			
			-- Prompts user for 'first-video only' or 'play all (Theatré Mode)' choice if multi-segmented Youtube or Vimeo videos are detected
			set playlistChoice to ""
			if playlistCheck is true then
				tell me
					activate
					beep
					display dialog "  Youtube Playlist or Vimeo Album Detected" with icon alias ((path to me) & "Contents:Resources:applet.icns" as string) buttons {"Cancel", " AutoPlay 
 All Videos", " Play First 
 Video Only"} default button " AutoPlay 
 All Videos"
				end tell
				set playlistChoice to button returned of the result
				if playlistChoice is " AutoPlay 
 All Videos" then
					if actionChoice contains "Normal" then
						set actionChoice to "Theatré Mode"
					end if
				else if playlistChoice is " Play First 
 Video Only" then
					set videoURL_data to (the first item of videoURL_data) as list
				else
					error number -128
				end if
			end if
			
			-- Automatically switches from Normal Mode to Theatré Mode if other QuickTime windows are open to prevent conflict.
			set qtWindows to ""
			try
				tell application "System Events"
					get the number of windows of application process "QuickTime Player"
				end tell
				set qtWindows to the result
			end try
			if qtWindows > 0 then
				if actionChoice contains "Normal" then
					set actionChoice to "Theatré Mode"
				end if
			end if
		end if
		
		
		----
		-- Normal Mode.
		----
		
		if actionChoice contains "Normal" then
			
			-- Video controller. If a video has multiple segments, each additional segment is triggered after the current QuickTime window is closed.
			tell application "QuickTime Player"
				activate
			end tell
			set segmentNumber to 1
			try
				repeat numberOfSegments times
					set qtLink to item segmentNumber of videoURL_data as string
					if numberOfSegments > 1 then
						try
							repeat
								delay 0.5
								tell application "System Events"
									if window 1 of process "QuickTime Player" exists then
									else
										exit repeat
									end if
								end tell
							end repeat
						end try
					end if
					
					-- Activates and streams video link to QuickTime.
					tell application "QuickTime Player"
						getURL qtLink
						tell application "QuickTime Player" to play the front document
					end tell
					set segmentNumber to segmentNumber + 1
				end repeat
			end try
			
			
			----
			-- Theatré Mode.
			----
			
		else
			
			-- Video controller. If a video has multiple segments, each additional segment is triggered automatically after the current one has played.
			set segmentNumber to 1
			set segmentDuration to ""
			set currentTime to ""
			try
				repeat numberOfSegments times
					set qtLink to item segmentNumber of videoURL_data as string
					
					-- Activates and streams video link to QuickTime.
					tell application "QuickTime Player"
						activate
						delay 1
						getURL qtLink
						delay 1
						pause
					end tell
					tell application "System Events"
						keystroke "f" using command down
					end tell
					delay 1
					tell application "QuickTime Player"
						play
						set theVideo to the front document
					end tell
					
					-- Closes video/segment when finished.
					try
						repeat
							tell application "QuickTime Player"
								set segmentDuration to the duration of theVideo
								set currentTime to the current time of theVideo
							end tell
							
							-- Exits script if QuickTime is quit (Primary).
							tell application "System Events"
								set qtRunning to count of (every process whose name is "QuickTime Player")
								if qtRunning is not greater than or equal to 1 then
									error number -128
								end if
							end tell
							
							if currentTime = segmentDuration then
								close theVideo
								exit repeat
							end if
							delay 1
						end repeat
					end try
					set segmentNumber to segmentNumber + 1
					
					-- Exits script if QuickTime is quit (Secondary).
					tell application "System Events"
						set qtRunning to count of (every process whose name is "QuickTime Player")
						if qtRunning is not greater than or equal to 1 then
							error number -128
						end if
					end tell
				end repeat
			end try
		end if
		
		
		----
		-- Video download code.
		----
		
	else if actionChoice contains "Download" then
		
		--Sets up temp-folder for video being downloaded.
		set rdmString to ""
		repeat 7 times
			set rdmString to rdmString & some item of "abcdefghijklmnopqrstuvwxyz1234567890"
		end repeat
		
		-- Sets up appearance and launches Terminal window for download.
		tell application "System Events"
			set terminalRunning to count of (every process whose name is "Terminal")
			if terminalRunning is 1 then
				tell application "Terminal"
					activate
				end tell
				tell application "System Events" to keystroke "n" using command down
				tell application "Terminal"
					set background color of first window to "black"
					set cursor color of first window to "yellow"
					set normal text color of first window to "yellow"
					set bold text color of first window to "yellow"
				end tell
			else
				tell application "Terminal"
					activate
				end tell
				tell application "Terminal"
					set background color of first window to "black"
					set cursor color of first window to "yellow"
					set normal text color of first window to "yellow"
					set bold text color of first window to "yellow"
				end tell
			end if
		end tell
		tell application "Terminal"
			do script "clear; mkdir -p ~/Library/Application\\ Support/ppcmcTemp/" & rdmString & "/;  echo -e \"
\"; echo -e \"
Downloading Video...\"; sleep 1; echo -e \"
\"; /usr/local/bin/" & pythonVrsn & " /usr/local/bin/youtube-dl " & videoQuality & " -o ~/Library/Application\\ Support/ppcmcTemp/" & rdmString & "/\"%(title)s.%(ext)s\" " & hyphenFixer & "'" & pageURL & "'; mv -f ~/Library/Application\\ Support/ppcmcTemp/" & rdmString & "/* ~/Desktop/ &> /dev/null; rm -R ~/Library/Application\\ Support/ppcmcTemp/" & rdmString & "/;  echo -e \"
\"; echo -e \"
- Download Complete -\"; sleep 1; echo -e \"
\"" in first window
		end tell
		
		
		----
		-- Converts desired video to MP3 Audio.
		----
		
	else if actionChoice contains "Make MP3" then
		
		--Sets up temp-folder for video to be converted.
		set rdmString to ""
		repeat 7 times
			set rdmString to rdmString & some item of "abcdefghijklmnopqrstuvwxyz1234567890"
		end repeat
		
		-- Sets up appearance and launches Terminal window for download & conversion.
		tell application "System Events"
			set terminalRunning to count of (every process whose name is "Terminal")
			if terminalRunning is 1 then
				tell application "Terminal"
					activate
				end tell
				tell application "System Events" to keystroke "n" using command down
				tell application "Terminal"
					set background color of first window to "black"
					set cursor color of first window to "cyan"
					set normal text color of first window to "cyan"
					set bold text color of first window to "cyan"
				end tell
			else
				tell application "Terminal"
					activate
				end tell
				tell application "Terminal"
					set background color of first window to "black"
					set cursor color of first window to "cyan"
					set normal text color of first window to "cyan"
					set bold text color of first window to "cyan"
				end tell
			end if
		end tell
		
		-- Deleting original video.
		if actionChoice contains "(Delete Video)" then
			tell application "Terminal"
				do script "clear; mkdir -p ~/Library/Application\\ Support/ppcmcTemp/" & rdmString & "/;  echo -e \"
\"; echo -e \"
Downloading Video to Convert...\"; sleep 1; echo -e \"
\"; /usr/local/bin/" & pythonVrsn & " /usr/local/bin/youtube-dl -f 'best[ext!=webm]' --playlist-end 1 -o ~/Library/Application\\ Support/ppcmcTemp/" & rdmString & "/\"%(title)s.%(ext)s\" " & hyphenFixer & "'" & pageURL & "';  echo -e \"
\"; echo -e \"
Converting Video to MP3...\"; sleep 1; echo -e \"
\"; vidTtl=\"$(ls ~/Library/Application\\ Support/ppcmcTemp/" & rdmString & "/)\"; /usr/local/bin/ffmpeg-0.5 -i ~/Library/Application\\ Support/ppcmcTemp/" & rdmString & "/\"${vidTtl}\" -f mp3 -ar 44100 -ab 320000 -vn -y ~/Desktop/\"${vidTtl%????}\".mp3; rm -R ~/Library/Application\\ Support/ppcmcTemp/" & rdmString & "/;  echo -e \"
\"; echo -e \"
- Conversion Complete -\"; sleep 1; echo -e \"
\"" in first window
			end tell
			
			-- Keeping original video.
		else if actionChoice contains "(Keep Video)" then
			
			tell application "Terminal"
				do script "clear; mkdir -p ~/Library/Application\\ Support/ppcmcTemp/" & rdmString & "/;  echo -e \"
\"; echo -e \"
Downloading Video to Convert...\"; sleep 1; echo -e \"
\"; /usr/local/bin/" & pythonVrsn & " /usr/local/bin/youtube-dl -f 'best[ext!=webm]' --playlist-end 1 -o ~/Library/Application\\ Support/ppcmcTemp/" & rdmString & "/\"%(title)s.%(ext)s\" " & hyphenFixer & "'" & pageURL & "';  echo -e \"
\"; echo -e \"
Converting Video to MP3...\"; sleep 1; echo -e \"
\"; vidTtl=\"$(ls ~/Library/Application\\ Support/ppcmcTemp/" & rdmString & "/)\"; /usr/local/bin/ffmpeg-0.5 -i ~/Library/Application\\ Support/ppcmcTemp/" & rdmString & "/\"${vidTtl}\" -f mp3 -ar 44100 -ab 320000 -vn -y ~/Desktop/\"${vidTtl%????}\".mp3; mv -f ~/Library/Application\\ Support/ppcmcTemp/" & rdmString & "/* ~/Desktop/ &> /dev/null; rm -R ~/Library/Application\\ Support/ppcmcTemp/" & rdmString & "/;  echo -e \"
\"; echo -e \"
- Conversion Complete -\"; sleep 1; echo -e \"
\"" in first window
			end tell
		end if
	end if
	error number -128
	
	
	
	----
	-- End of Script.
	----
end timeout

AutoDownloader


------------------------------------------------------------------------------------------------------------------------------------
-- AutoDownloader (Batch) for PPC Media Center 6.0. Written by Adam Albrec (game_creator@hotmail.com) --
------------------------------------------------------------------------------------------------------------------------------------



-- Version 1.0 (Last Updated 05/30/2016) --



-- Checks system architecture to customize installation/execution and Sets Python version (in x86 systems corrects issues with failure of Python to update system version).
set endTest to (do shell script "sysctl -n hw.byteorder")
if endTest is "4321" then
	set archType to "PPC"
else
	set archType to "x86"
end if
set pythonVrsn to (do shell script "/usr/local/bin/python -V 2>&1 | sed 's/P/p/g;s/\\ //g' | cut -c1-9")
if archType is "x86" then
	set pythonVrsnNmbr to 0.0
	try
		set pythonVrsnNmbr to ((do shell script "echo " & quoted form of pythonVrsn & " | sed 's/[a-zA-Z]//g'") as real)
	end try
	if pythonVrsnNmbr is less than 3.4 then
		set pythonVrsn to "python3.4"
	end if
end if

-- Prevents timeout if user walks away during long sort.
with timeout of 86400 seconds
	
	-- Prompts user to choose download quality (SD/HD).
	set urlList to ""
	try
		set urlList to (the clipboard) as text
	end try
	if urlList does not contain "http" then
		error number -128
	end if
	
	-- PPC
	if archType is "PPC" then
		tell me
			activate
			display dialog "                  PPCMC AutoDownloader" with icon alias ((path to me) & "Contents:Resources:icon.icns" as string) buttons {"Cancel", "Download - SD", "Download - HD"} default button "Download - HD"
		end tell
		set actionChoice to button returned of the result
		
		-- Website video-quality request options.
		set videoQualityOptions to {"-f 'best[ext!=webm][height<=?540]/best[ext!=webm][width<=1280]/[ext!=webm][height<=?1080]/best'", "-f 'best[ext!=webm][width<=1280]/[ext!=webm][height<=?1080]/best'"} as list
		if actionChoice is "Download - SD" then
			set videoQuality to the first item of videoQualityOptions
		else if actionChoice is "Download - HD" then
			set videoQuality to the second item of videoQualityOptions
		else
			error number -128
		end if
		
		-- x86
	else
		tell me
			activate
			display dialog "       PPCMC AutoDownloader" with icon alias ((path to me) & "Contents:Resources:icon.icns" as string) buttons {"Cancel", "Run"} default button "Run"
		end tell
		set actionChoice to button returned of the result
		if actionChoice is not "Run" then
			error number -128
		end if
		set videoQuality to ""
		end if
	end if
	
	-- Creates Batch-Download folder on desktop (will be added to within same day of script is run more than once) & sorts list (deals with spaces, commas and line-breaks).
	set dateCurrent to ((do shell script "date '+%m-%d-%Y'") as text)
	do shell script "mkdir -p ~/Desktop/PPCMC\\ Batch\\ Downloads\\ " & dateCurrent & "/"
	set outputFolder to (((path to desktop from user domain) & "PPCMC Batch Downloads " & dateCurrent) as string) as alias
	tell application "Finder" to set label index of outputFolder to 3
	set urlList to do shell script "echo " & quoted form of urlList & " | sed 's/ htt/\\
htt/g;s/\\ //g;s/,htt/\\
htt/g;s/,//g' | awk 'NF'"
	set urlList to (the paragraphs of urlList) as list
	
	
	----
	-- Video download code.
	----
	
	-- Download check-loop (see below).
	set finalCheck to false
	repeat
		set urlListCount to ""
		set urlListCount to the number of items of urlList
		set urlListRun to 1
		
		-- Downloaders.
		repeat
			
			-- Starts independent download (does not hold script until complete).
			set pageURL to ""
			set pageURL to item urlListRun of urlList as text
			
			-- Removes extraneous characters/formatting-variations in Youtube links, filters out Vimeo channels/groups code, and streamlines Google/Yahoo video search results.
			set playlistCheck to false
			set pageURL_Im to pageURL
			if pageURL_Im contains "youtu.be" then
				set pageURL to do shell script "echo " & quoted form of pageURL_Im & " | sed 's/%3A/\\:/g;s/%2F/\\//g;s/%3F/?/g;s/%3D/=/g;s/%26/\\&/g;s/attribution_link.*=\\///g;s/^.*.com\\///g;s/^.*.be\\///g;s/\\///g;s/watch?//g;s/playlist?//g;s/&v=//g;s/v=//g;s/list=//g;s/listType=.*&//g;s/feature=//g;s/share//g;s/player_embeddeds//g;s/player_embedded//g;s/embed//g;s/youtu.be//g;s/iv_load_policy=3//g;s/loop=[1,0]//g;s/modestbranding=[1,0]//g;s/showinfo=[1,0]//g;s/autoplay=[1,0]//g;s/rel=[1,0]//g;s/disablekb=[1,0]//g;s/controls=[1,0]//g;s/cc_load_policy=[1,0]//g;s/autohide=[1,0]//g;s/&.*$//g;s/?//g;s/=//g;s/start.*$//g;s/theme.*$//g;s/end.*$//g;s/color.*$//g;s/#t.*$//g'"
			else if pageURL_Im contains "youtube.com" then
				set pageURL to do shell script "echo " & quoted form of pageURL_Im & " | sed 's/%3A/\\:/g;s/%2F/\\//g;s/%3F/?/g;s/%3D/=/g;s/%26/\\&/g;s/attribution_link.*=\\///g;s/^.*.com\\///g;s/^.*.be\\///g;s/\\///g;s/watch?//g;s/playlist?//g;s/&v=//g;s/v=//g;s/list=//g;s/listType=.*&//g;s/feature=//g;s/share//g;s/player_embeddeds//g;s/player_embedded//g;s/embed//g;s/youtu.be//g;s/iv_load_policy=3//g;s/loop=[1,0]//g;s/modestbranding=[1,0]//g;s/showinfo=[1,0]//g;s/autoplay=[1,0]//g;s/rel=[1,0]//g;s/disablekb=[1,0]//g;s/controls=[1,0]//g;s/cc_load_policy=[1,0]//g;s/autohide=[1,0]//g;s/&.*$//g;s/?//g;s/=//g;s/start.*$//g;s/theme.*$//g;s/end.*$//g;s/color.*$//g;s/#t.*$//g'"
			else
				set pageURL to do shell script "echo " & quoted form of pageURL_Im & " | sed 's/http.*http/http/g;s/www.//g;s/%3A/\\:/g;s/%2F/\\//g;s/%3F/?/g;s/%3D/=/g;s/%26/\\&/g;s/vimeo.com\\/channels\\/.*\\//vimeo.com\\//g;s/vimeo.com\\/groups\\/.*\\/videos\\//vimeo.com\\//g'"
			end if
			
			-- Adds '--' flag to escape first-character hyphens in Youtube strings.
			set hyphenFixer to ""
			if pageURL starts with "-" then
				set hyphenFixer to "-- "
			end if
			
			-- Download shell script (only downloads first video of any multi-segmented video-URLs).
			do shell script "/usr/local/bin/" & pythonVrsn & " /usr/local/bin/youtube-dl " & videoQuality & " --playlist-end 1 -o ~/Desktop/PPCMC\\ Batch\\ Downloads\\ " & dateCurrent & "/\"%(title)s.%(ext)s\" " & hyphenFixer & "'" & pageURL & "' > /dev/null 2>&1 &"
			
			delay 1
			if urlListRun = urlListCount then
				exit repeat
			end if
			set urlListRun to urlListRun + 1
			
			-- Starts dependent download (holds script until complete - to ratchet download progress).
			set pageURL to ""
			set pageURL to item urlListRun of urlList as text
			
			-- Removes extraneous characters/formatting-variations in Youtube links, filters out Vimeo channels/groups code, and streamlines Google/Yahoo video search results.
			try
				set playlistCheck to false
				set pageURL_Im to pageURL
				if pageURL_Im contains "youtu.be" then
					set pageURL to do shell script "echo " & quoted form of pageURL_Im & " | sed 's/%3A/\\:/g;s/%2F/\\//g;s/%3F/?/g;s/%3D/=/g;s/%26/\\&/g;s/attribution_link.*=\\///g;s/^.*.com\\///g;s/^.*.be\\///g;s/\\///g;s/watch?//g;s/playlist?//g;s/&v=//g;s/v=//g;s/list=//g;s/listType=.*&//g;s/feature=//g;s/share//g;s/player_embeddeds//g;s/player_embedded//g;s/embed//g;s/youtu.be//g;s/iv_load_policy=3//g;s/loop=[1,0]//g;s/modestbranding=[1,0]//g;s/showinfo=[1,0]//g;s/autoplay=[1,0]//g;s/rel=[1,0]//g;s/disablekb=[1,0]//g;s/controls=[1,0]//g;s/cc_load_policy=[1,0]//g;s/autohide=[1,0]//g;s/&.*$//g;s/?//g;s/=//g;s/start.*$//g;s/theme.*$//g;s/end.*$//g;s/color.*$//g;s/#t.*$//g'"
				else if pageURL_Im contains "youtube.com" then
					set pageURL to do shell script "echo " & quoted form of pageURL_Im & " | sed 's/%3A/\\:/g;s/%2F/\\//g;s/%3F/?/g;s/%3D/=/g;s/%26/\\&/g;s/attribution_link.*=\\///g;s/^.*.com\\///g;s/^.*.be\\///g;s/\\///g;s/watch?//g;s/playlist?//g;s/&v=//g;s/v=//g;s/list=//g;s/listType=.*&//g;s/feature=//g;s/share//g;s/player_embeddeds//g;s/player_embedded//g;s/embed//g;s/youtu.be//g;s/iv_load_policy=3//g;s/loop=[1,0]//g;s/modestbranding=[1,0]//g;s/showinfo=[1,0]//g;s/autoplay=[1,0]//g;s/rel=[1,0]//g;s/disablekb=[1,0]//g;s/controls=[1,0]//g;s/cc_load_policy=[1,0]//g;s/autohide=[1,0]//g;s/&.*$//g;s/?//g;s/=//g;s/start.*$//g;s/theme.*$//g;s/end.*$//g;s/color.*$//g;s/#t.*$//g'"
				else
					set pageURL to do shell script "echo " & quoted form of pageURL_Im & " | sed 's/http.*http/http/g;s/www.//g;s/%3A/\\:/g;s/%2F/\\//g;s/%3F/?/g;s/%3D/=/g;s/%26/\\&/g;s/vimeo.com\\/channels\\/.*\\//vimeo.com\\//g;s/vimeo.com\\/groups\\/.*\\/videos\\//vimeo.com\\//g'"
				end if
				
				-- Adds '--' flag to escape first-character hyphens in Youtube strings.
				set hyphenFixer to ""
				if pageURL starts with "-" then
					set hyphenFixer to "-- "
				end if
				
				-- Download shell script (only downloads first video of any multi-segmented video-URLs).
				do shell script "/usr/local/bin/" & pythonVrsn & " /usr/local/bin/youtube-dl " & videoQuality & " --playlist-end 1 -o ~/Desktop/PPCMC\\ Batch\\ Downloads\\ " & dateCurrent & "/\"%(title)s.%(ext)s\" " & hyphenFixer & "'" & pageURL & "'"
			end try
			
			delay 1
			if urlListRun = urlListCount then
				exit repeat
			end if
			set urlListRun to urlListRun + 1
		end repeat
		
		-- Exits check-loop if no partial-downloads are found. If one of more partials are found, runs a second attempt.
		if finalCheck is true then
			exit repeat
		else if ((do shell script "ls ~/Desktop/PPCMC\\ Batch\\ Downloads\\ " & dateCurrent & "/") as text) does not contain ".part" then
			exit repeat
		else
			set finalCheck to true
		end if
	end repeat
	
	
	
	----
	-- End of Script.
	----
end timeout

P.S. Here is the installer (and yes, I am using “sudo” and “with administrator privileges” - have had problems when only using the second).

This installer is WAY-COOLer than the previous because it deletes the installer-files after they are used, and replaces itself (main.script) with the app script at the end of installation. So 60MB app becomes 400K after installation!


----------------------------------------------------------------------------------------------------------------
-- Installer for PPC Media Center 6.0. Written by Adam Albrec (game_creator@hotmail.com) --
----------------------------------------------------------------------------------------------------------------



-- Version 6.1 (Last Updated 07/09/2016) --



-- Detects if user is attempting to run from the DMG and if so prompts them to copy the app to their hard-drive and try again.
set appLocation to quoted form of (POSIX path of (path to me)) as text
if appLocation contains "/Volumes/PPC Media Center 6.0/PPC Media Center.app" then
	beep
	tell me
		activate
		display dialog "PPC Media Center cannot be run from the install image. 

Please copy to your Hard-Drive and try again.

" with icon 0 buttons {"OK"} default button "OK"
	end tell
	error number -128
end if


-- Checks system architecture to customize installation/execution and Sets Python version (in x86 systems corrects issues with failure of Python to update system version).
set endTest to (do shell script "sysctl -n hw.byteorder")
if endTest is "4321" then
	set archType to "PPC"
else
	set archType to "x86"
end if
set pythonVrsn to (do shell script "/usr/local/bin/python -V 2>&1 | sed 's/P/p/g;s/\\ //g' | cut -c1-9")
if archType is "x86" then
	set pythonVrsnNmbr to 0.0
	try
		set pythonVrsnNmbr to ((do shell script "echo " & quoted form of pythonVrsn & " | sed 's/[a-zA-Z]//g'") as real)
	end try
	if pythonVrsnNmbr is less than 3.4 then
		set pythonVrsn to "python3.4"
	end if
end if


-- Prevents time-out while waiting for user-response.
with timeout of 86400 seconds
	
	
	-- Prompts for agreement to component licenses and installation.
	tell me
		activate
		display dialog "                      - Special Thanks To -

¢ Jason Myers and Jeff Wayne for their help with debugging/testing, as well as Dan DeVoto for help with these and also hosting PPC Media Center from the beginning @ ppcluddite.blogspot.com!!!

¢ Lars Baumstark, Jorge Galadi and Pasquale Barbaro for adding our FIRST LANGUAGE LOCALIZATIONS (German, Spanish & Italian)!!!  Hopefully, more coming soon.

¢ Daniel Bolton and the team at youtube-dl ( rg3.github.io/youtube-dl ) for their incredible app -  NONE of this would be possible without their genius!

¢ Chris Forsythe and Graham Booker of perian.org for unleashing the power of QuickTime AND for allowing me to bundle Perian in this installer!!

¢ ProgrammingKid, creator of the most-awesome 'FLV Crunch' video-converter, for allowing the use of his compiled-binaries of ffmpeg, allowing MP3 conversion,

¢ Jean-Daniel Dupas ( github.com/Jean-Daniel/Spark ), maker of the OH SO COOL Hotkey-Utility 'Spark' for also allowing it to be included with this installer,

¢ and Last, but Not Least - the folks at Python.org for their amazing work and generous open-license to use Python.
" buttons {"Next"} default button "Next"
	end tell
	tell me
		activate
		display dialog "              Acknowledgments and Licenses:                                                 

I, Adam Albrec, give the Applescript-code herein to all to use freely for whatever they wish, without condition.  All may reuse, any or all-parts of it, as they see fit.  My heartfelt gratitude to all the GREAT Apple/Bash-scripters, over the years, who's shoulders I've stood on to make this work!

By installing this software, you agree before all applicable Laws, God, and the Millions of Transistors of this computer to abide by the licenses and terms set fourth by all contributing parties (see 'Licenses' folder to review Open-Source usage of this program and its component-parts.)" buttons {"Decline", "Accept"} default button "Accept"
	end tell
	set agreeChoice to button returned of the result
	if agreeChoice is "Accept" then
		set licenseDocs to quoted form of (POSIX path of (path to me)) & "Contents/Resources/Licenses/"
		beep
		
		-- Opens folder of license documents.
		do shell script "open " & licenseDocs
		
		-- Creates /usr/local/ folders if not present.
		do shell script "sudo mkdir -p /usr/local/bin/; sudo mkdir -p /usr/local/lib//" with administrator privileges
		set usrLocalBin to "/usr/local/bin/"
		set usrLocalLib to "/usr/local/lib/"
		set servicesFolder to "/Library/Services/"
		set youtubedlBin to quoted form of (POSIX path of (path to me)) & "Contents/Resources/Install\\ Files/usrLocal/youtube-dl"
		
		-- Installs youtube-dl and sets permissions.
		do shell script "sudo cp -n " & youtubedlBin & " " & usrLocalBin & "; sudo chmod 777 /usr/local/bin/youtube-dl" with administrator privileges
		set ffmpegBin to quoted form of (POSIX path of (path to me)) & "Contents/Resources/Install\\ Files/usrLocal/" & archType & "/ffmpeg-0.5"
		
		-- Installs ffmpeg (for MP3 conversion).
		try
			do shell script "sudo cp -n " & ffmpegBin & " " & usrLocalBin with administrator privileges
		end try
		set libfaacDylib to quoted form of (POSIX path of (path to me)) & "Contents/Resources/Install\\ Files/usrLocal/" & archType & "/libfaac.0.dylib"
		
		-- Installs libfaac.0.dylib (for MP3 conversion).
		try
			do shell script "sudo cp -n " & libfaacDylib & " " & usrLocalLib with administrator privileges
		end try
		set libfaadDylib to quoted form of (POSIX path of (path to me)) & "Contents/Resources/Install\\ Files/usrLocal/" & archType & "/libfaad.0.dylib"
		
		-- Installs libfaad.0.dylib (for MP3 conversion).
		try
			do shell script "sudo cp -n " & libfaadDylib & " " & usrLocalLib with administrator privileges
		end try
		set libmp3lameDylib to quoted form of (POSIX path of (path to me)) & "Contents/Resources/Install\\ Files/usrLocal/" & archType & "/libmp3lame.0.dylib"
		
		-- Installs libmp3lame.0.dylib (for MP3 conversion).
		try
			do shell script "sudo cp -n " & libmp3lameDylib & " " & usrLocalLib with administrator privileges
		end try
		
		-- Looks in system and user 'PreferencePanes' folders for Perian. Triggers installation if not found.
		set perianInstalled to (do shell script "if test -e /Library/PreferencePanes/Perian.prefPane/; then echo 1; else echo 0; fi")
		if perianInstalled is not "1" then
			set perianInstalled to (do shell script "if test -e ~/Library/PreferencePanes/Perian.prefPane/; then echo 1; else echo 0; fi")
			if perianInstalled is not "1" then
				set perianPrefPane to quoted form of (POSIX path of (path to me)) & "Contents/Resources/Install\\ Files/Perian.prefPane/"
				do shell script "open " & perianPrefPane
			end if
		end if
		
		-- Loops until after System Preferences is closed (Perian installed).
		repeat
			delay 1
			tell application "System Events" to set sysPrefsRunning to count of (every process whose name is "System Preferences")
			if sysPrefsRunning is 0 then
				exit repeat
			end if
		end repeat
		
		-- Looks for Python 2.7 (PPC) or 3.4 (x86), and triggers installation if not found.
		if archType is "PPC" then
			set pythonVrsnInstalled to "python2.7"
		else
			set pythonVrsnInstalled to "python3.4"
		end if
		set pythonInstalled to (do shell script "if test -e /usr/local/bin/" & pythonVrsnInstalled & "/; then echo 1; else echo 0; fi")
		if pythonInstalled is not "1" then
			if archType is "PPC" then
				set pythonInstaller to quoted form of (POSIX path of (path to me)) & "Contents/Resources/Install\\ Files/Python/Python.mpkg"
			else
				set pythonInstaller to quoted form of (POSIX path of (path to me)) & "Contents/Resources/Install\\ Files/Python/python-3.4.4-macosx10.6.pkg"
			end if
			do shell script "sleep 2; open " & pythonInstaller
		end if
		if archType is "x86" then
			do shell script "sudo mkdir -p /Library/Services/" with administrator privileges
			set ppcmcQuickLaunch to quoted form of (POSIX path of (path to me)) & "Contents/Resources/Install\\ Files/QuickLaunch/PPCMC\\ QuickLaunch.workflow"
			
			-- Installs PPCMC QuickLaunch Service (for x86 hotkey-support).
			try
				do shell script "sudo cp -R " & ppcmcQuickLaunch & " " & servicesFolder & ";sleep 1; sudo /System/Library/CoreServices/pbs" with administrator privileges
			end try
		end if
		
		-- Loops until after Python is installed and then looks for available updates for youtube-dl.
		repeat
			delay 1
			
			-- Updates youtube-dl framework to current version after install (waits for Python installation to complete if needed).
			tell application "System Events" to set istllrRunning to count of (every process whose name is "Installer")
			if istllrRunning is 0 then
				try
					do shell script "/usr/local/bin/" & pythonVrsn & " /usr/local/bin/youtube-dl -U"
				end try
				exit repeat
			end if
		end repeat
		
		-- For PPC systems, promps user for download/install of Spark for hotkey-support if not found. If found, prompts to check Read Me for Spark usage. For Intel systems, prompts to check Read Me for system hotkey support.
		try
			if archType is "PPC" then
				set sparkInstalled to (do shell script "if test -e ~/Library/Application\\ Support/Spark/; then echo 1; else echo 0; fi")
				if sparkInstalled is "0" then
					beep
					tell me
						activate
						display dialog "The installer has detected that you are running a PPC version of OSX. 
				
Would you like to install Spark? 
		
Spark enables hotkey-support for PPC Media Center and many other Apps! See Read Me for more info." with icon alias ((path to me) & "Contents:Resources:applet.icns" as string) buttons {"No", "Yes"} default button "Yes"
					end tell
					set sparkChoice to button returned of the result
					if sparkChoice is "Yes" then
						set sparkInstaller to quoted form of (POSIX path of (path to me)) & "Contents/Resources/Install\\ Files/QuickLaunch/Spark.dmg"
						do shell script "cp " & sparkInstaller & " ~/Desktop/; open ~/Desktop/Spark.dmg"
					end if
				else
					beep
					tell me
						activate
						display dialog "The installer has detected that you have Spark Installed! 
				
Spark can offer hotkey-support for PPC Media Center (see Read Me for info)." with icon alias ((path to me) & "Contents:Resources:applet.icns" as string) buttons {"OK"} default button "OK"
					end tell
				end if
			else
				beep
				tell me
					activate
					display dialog "The installer has detected that you are running an Intel Mac System. 
				
OSX can offer hotkey-support for PPC Media Center (see Read Me for info)." with icon alias ((path to me) & "Contents:Resources:applet.icns" as string) buttons {"OK"} default button "OK"
				end tell
			end if
		end try
		
		-- Removes install files from package after installation.
		set appScriptFolder to quoted form of (POSIX path of (path to me)) & "Contents/Resources/Scripts/"
		set installedAppScript to quoted form of (POSIX path of (path to me)) & "Contents/Resources/Install\\ Files/Installed\\ AppScript/main.scpt"
		set installFiles to quoted form of (POSIX path of (path to me)) & "Contents/Resources/Install\\ Files/"
		do shell script "sleep 2; sudo cp -f " & installedAppScript & " " & appScriptFolder & "; sudo rm -fR " & installFiles & "  > /dev/null 2>&1 &" with administrator privileges
		error number -128
	else
		error number -128
	end if
	
	
	
	----
	-- End of Script.
	----
end timeout

UPDATE - VERSION 6.5

This has got to be the last of these! Anyway, just did an update to initially fix a bug in my youtube-link scraper. The player_embedded tag, when combined with youtube-links featuring an (S) on the front of the video-ID where accidentally combining forces to rob that (S), right out of the string. The rarer tag player_embeddedS was not getting removed till after forward-slashes (/) so occasionally, the two above would wind-up together and get taken out as player_embeddedS. Fixed.

Also, added a bunch of new Functionality to the Theatré Mode QuickTime Automation for Multi-Segmented video-playback. The user is given a choice between Windowed or Fullscreen, with each segment opening, playing and closing on its own (unaffected by pausing video). The best part, is that if you go from Fullscreen to Windowed during playback or vice-versa, that choice is remembered in the next segment, as is the Window’s last position, scale and audio-volume setting. Checks every 1/2 second or so for changes to these settings.

Primary App (most changes).


------------------------------------------------------------------------------------------------------------------------------------------------------------------
-- A G.U.I. for the Python Script youtube-dl (http://rg3.github.io/youtube-dl/). Written by Adam Albrec (game_creator@hotmail.com) --
------------------------------------------------------------------------------------------------------------------------------------------------------------------



-- Version 6.5 (Last Updated 10/06/2016) --



-- Checks system architecture to customize installation/execution and Sets Python version (in x86 systems corrects issues with failure of Python to update system version).
set endTest to (do shell script "sysctl -n hw.byteorder")
if endTest is "4321" then
	set archType to "PPC"
else
	set archType to "x86"
end if
set pythonVrsn to (do shell script "/usr/local/bin/python -V 2>&1 | sed 's/P/p/g;s/\\ //g' | cut -c1-9")
if archType is "x86" then
	set pythonVrsnNmbr to 0.0
	try
		set pythonVrsnNmbr to ((do shell script "echo " & quoted form of pythonVrsn & " | sed 's/[a-zA-Z]//g'") as real)
	end try
	if pythonVrsnNmbr is less than 3.4 then
		set pythonVrsn to "python3.4"
	end if
end if

-- Checks if PPCMC AutoDownloader is running (adds option to reset in main dialog if running).
set ppcBatchTest to {}
tell application "System Events"
	set ppcBatchTest to (get the name of (every process whose name contains "ppcBatch"))
end tell

-- Purges unneeded files in temp-directory (Runs only when no Terminal processes are using the directory)
tell application "System Events" to set terminalRunning to count of (every process whose name is "Terminal")
if terminalRunning is 0 then
	try
		do shell script "rm -R ~/Library/Application\\ Support/ppcmcTemp"
	end try
else
	set terminalInUse to the count (the windows of application "Terminal" whose name does not contain "bash")
	if terminalInUse is less than 1 then
		try
			do shell script "rm -R ~/Library/Application\\ Support/ppcmcTemp"
		end try
	end if
end if

-- Auto-updater for youtube-dl framework.
set dateCurrent to ((do shell script "date '+%m%d%Y'") as text)
set updateCurrentCheck to (do shell script "if test -e ~/Library/Preferences/ppcmcUpdated" & dateCurrent & "/; then echo 1; else echo 0; fi")
if updateCurrentCheck is not "1" then
	try
		do shell script "rm -R ~/Library/Preferences/ppcmcUpdated*"
	end try
	do shell script "/usr/local/bin/" & pythonVrsn & " /usr/local/bin/youtube-dl -U > /dev/null 2>&1 &"
	do shell script "mkdir ~/Library/Preferences/ppcmcUpdated" & dateCurrent & "/"
end if


-- Prevents time-out while waiting for user-response.
with timeout of 86400 seconds
	
	
	-- Captures URL/s from clipboard (deals with spaces, commas and line-breaks).
	set pageURL to ""
	try
		set pageURL to (the clipboard) as text
	end try
	set pageURL to do shell script "echo " & quoted form of pageURL & " | sed 's/ htt/\\
htt/g;s/\\ //g;s/,htt/\\
htt/g;s/,//g' | awk 'NF'"
	
	-- Prompts for URL if none is on clipboard.
	if pageURL does not contain "http" then
		beep
		tell me
			activate
			display dialog "A Video URL is Required to Proceed" with icon 2 buttons {"OK"} default button "OK"
		end tell
		error number -128
	end if
	
	-- Triggers 'PPCMC AutoDownloader' (batch downloader) if more than one URL is found on the clipboard).
	set pageURL_ListTest to {}
	set pageURL_ListTest to the paragraphs of pageURL
	set ppcBatch to quoted form of (POSIX path of (path to me)) & "Contents/Resources/PPCMC\\ AutoDownloader.app/"
	set batchTest to ""
	set batchTest to the number of items of pageURL_ListTest
	if batchTest is greater than 1 as real then
		if ppcBatchTest is not {} then
			beep
			tell me
				activate
				display dialog "PPCMC AutoDownloader is Already Running.

Please Try Again After it Finishes, 

OR 

Enter an Individual Video URL." with icon 2 buttons {"OK"} default button "OK"
			end tell
			error number -128
		else
			do shell script "open " & ppcBatch
			error number -128
		end if
	end if
	
	-- Video Quality settings for PPC systems.
	set videoQuality to ""
	set videoQualityOptions to {"-f 'best[ext!=webm][height<=?540]/best[ext!=webm][width<=1280]/[ext!=webm][height<=?1080]/best'", "-f 'best[ext!=webm][width<=1280]/[ext!=webm][height<=?1080]/best'"} as list
	
	
	----
	-- Requests desired URL to Play, Save  or Convert to MP3.
	----
	
	if archType is "PPC" then
		
		-- PPC
		if ppcBatchTest is {} then
			tell me
				activate
				set actionChoice to (choose from list {"Play - SD Normal", "Play - SD Theatré Mode", "Play - HD Normal", "Play - HD Theatré Mode", "Download - SD", "Download - HD", "Make MP3 (Delete Video)", "Make MP3 (Keep Video)"} with prompt "Welcome to PPC Media Center!" OK button name "OK" cancel button name "Cancel" default items "Play - SD Normal" without multiple selections allowed) as text
			end tell
		else
			tell me
				activate
				set actionChoice to (choose from list {"Play - SD Normal", "Play - SD Theatré Mode", "Play - HD Normal", "Play - HD Theatré Mode", "Download - SD", "Download - HD", "Make MP3 (Delete Video)", "Make MP3 (Keep Video)", "* Reset AutoDownloader"} with prompt "Welcome to PPC Media Center!" OK button name "OK" cancel button name "Cancel" default items "Play - SD Normal" without multiple selections allowed) as text
			end tell
		end if
		if actionChoice contains "- SD" then
			set videoQuality to the first item of videoQualityOptions
		else if actionChoice contains "- HD" then
			set videoQuality to the second item of videoQualityOptions
		end if
		if actionChoice is "False" then
			error number -128
		end if
		
	else
		
		-- x86
		if ppcBatchTest is {} then
			tell me
				activate
				set actionChoice to (choose from list {"Play - Normal", "Play - Theatré Mode", "Download", "Make MP3 (Delete Video)", "Make MP3 (Keep Video)"} with prompt "Welcome to PPC Media Center!" OK button name "OK" cancel button name "Cancel" default items "Play - Normal" without multiple selections allowed) as text
			end tell
		else
			set actionChoice to (choose from list {"Play - Normal", "Play - Theatré Mode", "Download", "Make MP3 (Delete Video)", "Make MP3 (Keep Video)", "* Reset AutoDownloader"} with prompt "Welcome to PPC Media Center!" OK button name "OK" cancel button name "Cancel" default items "Play - Normal" without multiple selections allowed) as text
		end if
		if actionChoice is "False" then
			error number -128
		end if
	end if
	
	-- Exits and resets PPC AutoDownloader & stops all downloads, if they become stuck.
	
	if actionChoice is "* Reset AutoDownloader" then
		try
			do shell script "killall ppcBatch"
		end try
		try
			do shell script "killall Python"
		end try
		error number -128
	end if
	
	-- Removes extraneous characters/formatting-variations in Youtube links, filters out Vimeo channels/groups code, and streamlines Google/Yahoo video search results.
	set playlistCheck to false
	set pageURL_Im to pageURL
	if pageURL_Im contains "youtu.be" then
		if pageURL_Im contains "?list=" then
			set pageURL_Im to do shell script "echo " & quoted form of pageURL_Im & " | sed 's/^.*?list=//g'"
		end if
		set pageURL to do shell script "echo " & quoted form of pageURL_Im & " | sed 's/%3A/\\:/g;s/%2F/\\//g;s/%3F/?/g;s/%3D/=/g;s/%26/\\&/g;s/player_embeddeds//g;s/attribution_link.*=\\///g;s/^.*.com\\///g;s/^.*.be\\///g;s/\\///g;s/watch?//g;s/playlist?//g;s/&v=//g;s/v=//g;s/list=//g;s/listType=.*&//g;s/feature=//g;s/share//g;s/player_embedded//g;s/embed//g;s/youtu.be//g;s/iv_load_policy=3//g;s/loop=[1,0]//g;s/modestbranding=[1,0]//g;s/showinfo=[1,0]//g;s/autoplay=[1,0]//g;s/rel=[1,0]//g;s/disablekb=[1,0]//g;s/controls=[1,0]//g;s/cc_load_policy=[1,0]//g;s/autohide=[1,0]//g;s/&.*$//g;s/?//g;s/=//g;s/start.*$//g;s/theme.*$//g;s/end.*$//g;s/color.*$//g;s/#t.*$//g'"
		set playlistCheck to true
	else if pageURL_Im contains "youtube.com" then
		if pageURL_Im contains "&list=" then
			set pageURL_Im to do shell script "echo " & quoted form of pageURL_Im & " | sed 's/watch?v=.*&//g'"
		end if
		set pageURL to do shell script "echo " & quoted form of pageURL_Im & " | sed 's/%3A/\\:/g;s/%2F/\\//g;s/%3F/?/g;s/%3D/=/g;s/%26/\\&/g;s/player_embeddeds//g;s/attribution_link.*=\\///g;s/^.*.com\\///g;s/^.*.be\\///g;s/\\///g;s/watch?//g;s/playlist?//g;s/&v=//g;s/v=//g;s/list=//g;s/listType=.*&//g;s/feature=//g;s/share//g;s/player_embedded//g;s/embed//g;s/youtu.be//g;s/iv_load_policy=3//g;s/loop=[1,0]//g;s/modestbranding=[1,0]//g;s/showinfo=[1,0]//g;s/autoplay=[1,0]//g;s/rel=[1,0]//g;s/disablekb=[1,0]//g;s/controls=[1,0]//g;s/cc_load_policy=[1,0]//g;s/autohide=[1,0]//g;s/&.*$//g;s/?//g;s/=//g;s/start.*$//g;s/theme.*$//g;s/end.*$//g;s/color.*$//g;s/#t.*$//g'"
		set playlistCheck to true
	else
		set pageURL to do shell script "echo " & quoted form of pageURL_Im & " | sed 's/http.*http/http/g;s/www.//g;s/%3A/\\:/g;s/%2F/\\//g;s/%3F/?/g;s/%3D/=/g;s/%26/\\&/g;s/vimeo.com\\/channels\\/.*\\//vimeo.com\\//g;s/vimeo.com\\/groups\\/.*\\/videos\\//vimeo.com\\//g'"
		if pageURL contains "vimeo.com" then
			set playlistCheck to true
		end if
	end if
	
	-- Adds '--' flag to escape first-character hyphens in Youtube strings.
	set hyphenFixer to ""
	if pageURL starts with "-" then
		set hyphenFixer to "-- "
	end if
	
	
	----
	-- Plays video in QuickTime.
	----
	
	if actionChoice contains "Play" then
		
		-- Gets video link from URL/s. Special playback handlers.
		set videoURL_data to paragraphs of (do shell script "/usr/local/bin/" & pythonVrsn & " /usr/local/bin/youtube-dl " & videoQuality & " --get-url " & hyphenFixer & "'" & pageURL & "'") as list
		
		-- If a video playback is only offered in WebM, prompts the user to cancel or download.
		if archType is "PPC" then
			set webmCheck to ""
			set webmCheck to videoURL_data as text
			if webmCheck contains ".webm" then
				beep
				tell me
					activate
					display dialog "Video URL is Only Available in WebM & Cannot be Streamed in QuickTime!" with icon 2 buttons {"Cancel", "Download Video"} default button "Download Video"
				end tell
				set webMChoice to button returned of the result
				if webMChoice is "Download Video" then
					--Sets up temp-folder for video being downloaded.
					set rdmString to ""
					repeat 7 times
						set rdmString to rdmString & some item of "abcdefghijklmnopqrstuvwxyz1234567890"
					end repeat
					
					-- Sets up appearance and launches Terminal window for download.
					tell application "System Events"
						set terminalRunning to count of (every process whose name is "Terminal")
						if terminalRunning is 1 then
							tell application "Terminal"
								activate
							end tell
							tell application "System Events" to keystroke "n" using command down
							tell application "Terminal"
								set background color of first window to "black"
								set cursor color of first window to "red"
								set normal text color of first window to "red"
								set bold text color of first window to "red"
							end tell
						else
							tell application "Terminal"
								activate
							end tell
							tell application "Terminal"
								set background color of first window to "black"
								set cursor color of first window to "red"
								set normal text color of first window to "red"
								set bold text color of first window to "red"
							end tell
						end if
					end tell
					tell application "Terminal"
						do script "clear; mkdir -p ~/Library/Application\\ Support/ppcmcTemp/" & rdmString & "/;  echo -e \"
\"; echo -e \"
Downloading WebM Video...\"; sleep 1; echo -e \"
\"; /usr/local/bin/" & pythonVrsn & " /usr/local/bin/youtube-dl " & videoQuality & " -o ~/Library/Application\\ Support/ppcmcTemp/" & rdmString & "/\"%(title)s.%(ext)s\" " & hyphenFixer & "'" & pageURL & "'; mv -f ~/Library/Application\\ Support/ppcmcTemp/" & rdmString & "/* ~/Desktop/ &> /dev/null; rm -R ~/Library/Application\\ Support/ppcmcTemp/" & rdmString & "/;  echo -e \"
\"; echo -e \"
- Download Complete -\"; sleep 1; echo -e \"
\"" in first window
					end tell
					error number -128
				else
					error number -128
				end if
			end if
		end if
		
		set numberOfSegments to the number of items of videoURL_data
		if numberOfSegments > 1 then
			
			-- Prompts user for 'first-video only' or 'play all (Theatré Mode)' choice if multi-segmented Youtube or Vimeo videos are detected
			set playlistChoice to ""
			if playlistCheck is true then
				tell me
					activate
					beep
					display dialog "  Youtube Playlist or Vimeo Album Detected" with icon alias ((path to me) & "Contents:Resources:applet.icns" as string) buttons {"Cancel", " AutoPlay 
 All Videos", " Play First 
 Video Only"} default button " AutoPlay 
 All Videos"
				end tell
				set playlistChoice to button returned of the result
				if playlistChoice is " AutoPlay 
 All Videos" then
					if actionChoice contains "Normal" then
						set actionChoice to "Theatré Mode"
					end if
				else if playlistChoice is " Play First 
 Video Only" then
					set videoURL_data to (the first item of videoURL_data) as list
				else
					error number -128
				end if
			end if
			
			-- Automatically switches from Normal Mode to Theatré Mode if other QuickTime windows are open to prevent conflict.
			set qtWindows to ""
			try
				tell application "System Events"
					get the number of windows of application process "QuickTime Player"
				end tell
				set qtWindows to the result
			end try
			if qtWindows > 0 then
				if actionChoice contains "Normal" then
					set actionChoice to "Theatré Mode"
				end if
			end if
		end if
		
		
		----
		-- Normal Mode.
		----
		
		if actionChoice contains "Normal" then
			
			-- Video controller. If a video has multiple segments, each additional segment is triggered after the current QuickTime window is closed.
			tell application "QuickTime Player"
				activate
			end tell
			set segmentNumber to 1
			try
				repeat numberOfSegments times
					set qtLink to item segmentNumber of videoURL_data as string
					if numberOfSegments > 1 then
						try
							repeat
								delay 0.5
								tell application "System Events"
									if window 1 of process "QuickTime Player" exists then
									else
										exit repeat
									end if
								end tell
							end repeat
						end try
					end if
					
					-- Activates and streams video link to QuickTime.
					tell application "QuickTime Player"
						getURL qtLink
						tell application "QuickTime Player" to play the front document
					end tell
					set segmentNumber to segmentNumber + 1
				end repeat
			end try
			
			
			----
			-- Theatré Mode.
			----
			
		else
			
			-- Prompts for Windowed or Fullscreen playback.
			set playbackStyle to "Fullscreen"
			set qtWindowScaleANDPosition to ""
			set qtCurrentVolume to "256"
			set qtDisplayMode to ""
			-- Prompts for display style (Windowed/Fullscreen) for multi-segmented videos.
			if the (count of items of videoURL_data) > 1 then
				tell me
					activate
					display dialog "Choose Multi-Segmented Video Playback-Style...

*Defaulting to Fullscreen in 5-Seconds" with icon alias ((path to me) & "Contents:Resources:applet.icns" as string) buttons {" Windowed", "Fullscreen"} default button "Fullscreen" giving up after 5
				end tell
				try
					set playbackStyle to button returned of the result
				end try
			end if
			
			-- Video controller. If a video has multiple segments, each additional segment is triggered automatically after the current one has played.
			set segmentNumber to 1
			set segmentDuration to ""
			set currentTime to ""
			try
				repeat numberOfSegments times
					set qtLink to item segmentNumber of videoURL_data as string
					
					-- Activates and streams video link to QuickTime.
					tell application "QuickTime Player"
						activate
						getURL qtLink
						delay 1
						set theVideo to the front document
						set theVideoName to the name of theVideo
						-- Sets subsequent segments to the same audio volume as the previous one.
						set the sound volume of theVideo to qtCurrentVolume
						-- Sets subsequent segments to the same window size and position as the previous one.
						try
							tell window theVideoName to set the bounds to qtWindowScaleANDPosition
						end try
					end tell
					
					-- Windowed.
					if playbackStyle is " Windowed" then
						delay 1
						tell application "QuickTime Player" to tell theVideo to play
						
					else
						
						-- Fullscreen.
						tell application "QuickTime Player"
							delay 1
							present theVideo scale screen
							play
						end tell
					end if
					
					-- Closes video/segment when finished.
					try
						repeat
							-- QuickTime 'User Settings' checker (Primary).
							tell application "QuickTime Player"
								-- Gets current audio volume to use subsequent segment/s.
								set qtCurrentVolume to the sound volume of theVideo
								-- Gets current display mode (Normal/Fullscreen) to use subsequent segment/s.
								set qtDisplayMode to (the display state of theVideo) as text
							end tell
							if qtDisplayMode is not "presentation" then
								set playbackStyle to " Windowed"
							else
								set playbackStyle to "Fullscreen"
							end if
							-- Gets current window size and position to use subsequent segment/s.
							if playbackStyle is " Windowed" then
								tell application "QuickTime Player" to set qtWindowScaleANDPosition to the bounds of window theVideoName
							end if
							
							-- Exits script if QuickTime is quit (Primary).
							tell application "System Events"
								set qtRunning to count of (every process whose name is "QuickTime Player")
								if qtRunning is not greater than or equal to 1 then
									error number -128
								end if
							end tell
							
							-- Closes QuickTime window once finished playing.
							tell application "QuickTime Player"
								set segmentDuration to the duration of theVideo
								set currentTime to the current time of theVideo
							end tell
							if currentTime = segmentDuration then
								close theVideo
								exit repeat
							end if
							
							-- QuickTime 'User Settings' checker (Secondary).
							tell application "QuickTime Player"
								-- Gets current audio volume to use subsequent segment/s.
								set qtCurrentVolume to the sound volume of theVideo
								-- Gets current display mode (Normal/Fullscreen) to use subsequent segment/s.
								set qtDisplayMode to (the display state of theVideo) as text
							end tell
							if qtDisplayMode is not "presentation" then
								set playbackStyle to " Windowed"
							else
								set playbackStyle to "Fullscreen"
							end if
							-- Gets current window size and position to use subsequent segment/s.
							if playbackStyle is " Windowed" then
								tell application "QuickTime Player" to set qtWindowScaleANDPosition to the bounds of window theVideoName
							end if
							delay 1
						end repeat
					end try
					set segmentNumber to segmentNumber + 1
					
					-- Exits script if QuickTime is quit (Secondary).
					tell application "System Events"
						set qtRunning to count of (every process whose name is "QuickTime Player")
						if qtRunning is not greater than or equal to 1 then
							error number -128
						end if
					end tell
				end repeat
			end try
		end if
		
		
		----
		-- Video download code.
		----
		
	else if actionChoice contains "Download" then
		
		--Sets up temp-folder for video being downloaded.
		set rdmString to ""
		repeat 7 times
			set rdmString to rdmString & some item of "abcdefghijklmnopqrstuvwxyz1234567890"
		end repeat
		
		-- Sets up appearance and launches Terminal window for download.
		tell application "System Events"
			set terminalRunning to count of (every process whose name is "Terminal")
			if terminalRunning is 1 then
				tell application "Terminal"
					activate
				end tell
				tell application "System Events" to keystroke "n" using command down
				tell application "Terminal"
					set background color of first window to "black"
					set cursor color of first window to "yellow"
					set normal text color of first window to "yellow"
					set bold text color of first window to "yellow"
				end tell
			else
				tell application "Terminal"
					activate
				end tell
				tell application "Terminal"
					set background color of first window to "black"
					set cursor color of first window to "yellow"
					set normal text color of first window to "yellow"
					set bold text color of first window to "yellow"
				end tell
			end if
		end tell
		tell application "Terminal"
			do script "clear; mkdir -p ~/Library/Application\\ Support/ppcmcTemp/" & rdmString & "/;  echo -e \"
\"; echo -e \"
Downloading Video...\"; sleep 1; echo -e \"
\"; /usr/local/bin/" & pythonVrsn & " /usr/local/bin/youtube-dl " & videoQuality & " -o ~/Library/Application\\ Support/ppcmcTemp/" & rdmString & "/\"%(title)s.%(ext)s\" " & hyphenFixer & "'" & pageURL & "'; mv -f ~/Library/Application\\ Support/ppcmcTemp/" & rdmString & "/* ~/Desktop/ &> /dev/null; rm -R ~/Library/Application\\ Support/ppcmcTemp/" & rdmString & "/;  echo -e \"
\"; echo -e \"
- Download Complete -\"; sleep 1; echo -e \"
\"" in first window
		end tell
		
		
		----
		-- Converts desired video to MP3 Audio.
		----
		
	else if actionChoice contains "Make MP3" then
		
		--Sets up temp-folder for video to be converted.
		set rdmString to ""
		repeat 7 times
			set rdmString to rdmString & some item of "abcdefghijklmnopqrstuvwxyz1234567890"
		end repeat
		
		-- Sets up appearance and launches Terminal window for download & conversion.
		tell application "System Events"
			set terminalRunning to count of (every process whose name is "Terminal")
			if terminalRunning is 1 then
				tell application "Terminal"
					activate
				end tell
				tell application "System Events" to keystroke "n" using command down
				tell application "Terminal"
					set background color of first window to "black"
					set cursor color of first window to "cyan"
					set normal text color of first window to "cyan"
					set bold text color of first window to "cyan"
				end tell
			else
				tell application "Terminal"
					activate
				end tell
				tell application "Terminal"
					set background color of first window to "black"
					set cursor color of first window to "cyan"
					set normal text color of first window to "cyan"
					set bold text color of first window to "cyan"
				end tell
			end if
		end tell
		
		-- Deleting original video.
		if actionChoice contains "(Delete Video)" then
			tell application "Terminal"
				do script "clear; mkdir -p ~/Library/Application\\ Support/ppcmcTemp/" & rdmString & "/;  echo -e \"
\"; echo -e \"
Downloading Video to Convert...\"; sleep 1; echo -e \"
\"; /usr/local/bin/" & pythonVrsn & " /usr/local/bin/youtube-dl -f 'best[ext!=webm]' --playlist-end 1 -o ~/Library/Application\\ Support/ppcmcTemp/" & rdmString & "/\"%(title)s.%(ext)s\" " & hyphenFixer & "'" & pageURL & "';  echo -e \"
\"; echo -e \"
Converting Video to MP3...\"; sleep 1; echo -e \"
\"; vidTtl=\"$(ls ~/Library/Application\\ Support/ppcmcTemp/" & rdmString & "/)\"; /usr/local/bin/ffmpeg-0.5 -i ~/Library/Application\\ Support/ppcmcTemp/" & rdmString & "/\"${vidTtl}\" -f mp3 -ar 44100 -ab 320000 -vn -y ~/Desktop/\"${vidTtl%????}\".mp3; rm -R ~/Library/Application\\ Support/ppcmcTemp/" & rdmString & "/;  echo -e \"
\"; echo -e \"
- Conversion Complete -\"; sleep 1; echo -e \"
\"" in first window
			end tell
			
			-- Keeping original video.
		else if actionChoice contains "(Keep Video)" then
			
			tell application "Terminal"
				do script "clear; mkdir -p ~/Library/Application\\ Support/ppcmcTemp/" & rdmString & "/;  echo -e \"
\"; echo -e \"
Downloading Video to Convert...\"; sleep 1; echo -e \"
\"; /usr/local/bin/" & pythonVrsn & " /usr/local/bin/youtube-dl -f 'best[ext!=webm]' --playlist-end 1 -o ~/Library/Application\\ Support/ppcmcTemp/" & rdmString & "/\"%(title)s.%(ext)s\" " & hyphenFixer & "'" & pageURL & "';  echo -e \"
\"; echo -e \"
Converting Video to MP3...\"; sleep 1; echo -e \"
\"; vidTtl=\"$(ls ~/Library/Application\\ Support/ppcmcTemp/" & rdmString & "/)\"; /usr/local/bin/ffmpeg-0.5 -i ~/Library/Application\\ Support/ppcmcTemp/" & rdmString & "/\"${vidTtl}\" -f mp3 -ar 44100 -ab 320000 -vn -y ~/Desktop/\"${vidTtl%????}\".mp3; mv -f ~/Library/Application\\ Support/ppcmcTemp/" & rdmString & "/* ~/Desktop/ &> /dev/null; rm -R ~/Library/Application\\ Support/ppcmcTemp/" & rdmString & "/;  echo -e \"
\"; echo -e \"
- Conversion Complete -\"; sleep 1; echo -e \"
\"" in first window
			end tell
		end if
	end if
	error number -128
	
	
	
	----
	-- End of Script.
	----
end timeout

AutoDownloader Sub-App (only changed youtube-scraper as mentioned above).


------------------------------------------------------------------------------------------------------------------------------------
-- AutoDownloader (Batch) for PPC Media Center 6.0. Written by Adam Albrec (game_creator@hotmail.com) --
------------------------------------------------------------------------------------------------------------------------------------



-- Version 1.1 (Last Updated 08/17/2016) --



-- Checks system architecture to customize installation/execution and Sets Python version (in x86 systems corrects issues with failure of Python to update system version).
set endTest to (do shell script "sysctl -n hw.byteorder")
if endTest is "4321" then
	set archType to "PPC"
else
	set archType to "x86"
end if
set pythonVrsn to (do shell script "/usr/local/bin/python -V 2>&1 | sed 's/P/p/g;s/\\ //g' | cut -c1-9")
if archType is "x86" then
	set pythonVrsnNmbr to 0.0
	try
		set pythonVrsnNmbr to ((do shell script "echo " & quoted form of pythonVrsn & " | sed 's/[a-zA-Z]//g'") as real)
	end try
	if pythonVrsnNmbr is less than 3.4 then
		set pythonVrsn to "python3.4"
	end if
end if

-- Prevents timeout if user walks away during long sort.
with timeout of 86400 seconds
	
	-- Prompts user to choose download quality (SD/HD).
	set urlList to ""
	try
		set urlList to (the clipboard) as text
	end try
	if urlList does not contain "http" then
		error number -128
	end if
	
	-- PPC
	if archType is "PPC" then
		tell me
			activate
			display dialog "                  PPCMC AutoDownloader" with icon alias ((path to me) & "Contents:Resources:icon.icns" as string) buttons {"Cancel", "Download - SD", "Download - HD"} default button "Download - HD"
		end tell
		set actionChoice to button returned of the result
		
		-- Website video-quality request options.
		set videoQualityOptions to {"-f 'best[ext!=webm][height<=?540]/best[ext!=webm][width<=1280]/[ext!=webm][height<=?1080]/best'", "-f 'best[ext!=webm][width<=1280]/[ext!=webm][height<=?1080]/best'"} as list
		if actionChoice is "Download - SD" then
			set videoQuality to the first item of videoQualityOptions
		else if actionChoice is "Download - HD" then
			set videoQuality to the second item of videoQualityOptions
		else
			error number -128
		end if
		
		-- x86
	else
		tell me
			activate
			display dialog "       PPCMC AutoDownloader" with icon alias ((path to me) & "Contents:Resources:icon.icns" as string) buttons {"Cancel", "Run"} default button "Run"
		end tell
		set actionChoice to button returned of the result
		if actionChoice is not "Run" then
			error number -128
		end if
		set videoQuality to ""
	end if
	
	-- Creates Batch-Download folder on desktop (will be added to within same day of script is run more than once) & sorts list (deals with spaces, commas and line-breaks).
	set dateCurrent to ((do shell script "date '+%m-%d-%Y'") as text)
	do shell script "mkdir -p ~/Desktop/PPCMC\\ Batch\\ Downloads\\ " & dateCurrent & "/"
	set outputFolder to (((path to desktop from user domain) & "PPCMC Batch Downloads " & dateCurrent) as string) as alias
	tell application "Finder" to set label index of outputFolder to 3
	set urlList to do shell script "echo " & quoted form of urlList & " | sed 's/ htt/\\
htt/g;s/\\ //g;s/,htt/\\
htt/g;s/,//g' | awk 'NF'"
	set urlList to (the paragraphs of urlList) as list
	
	
	----
	-- Video download code.
	----
	
	-- Download check-loop (see below).
	set finalCheck to false
	repeat
		set urlListCount to ""
		set urlListCount to the number of items of urlList
		set urlListRun to 1
		
		-- Downloaders.
		repeat
			
			-- Starts independent download (does not hold script until complete).
			set pageURL to ""
			set pageURL to item urlListRun of urlList as text
			
			-- Removes extraneous characters/formatting-variations in Youtube links, filters out Vimeo channels/groups code, and streamlines Google/Yahoo video search results.
			set playlistCheck to false
			set pageURL_Im to pageURL
			if pageURL_Im contains "youtu.be" then
				set pageURL to do shell script "echo " & quoted form of pageURL_Im & " | sed 's/%3A/\\:/g;s/%2F/\\//g;s/%3F/?/g;s/%3D/=/g;s/%26/\\&/g;s/player_embeddeds//g;s/attribution_link.*=\\///g;s/^.*.com\\///g;s/^.*.be\\///g;s/\\///g;s/watch?//g;s/playlist?//g;s/&v=//g;s/v=//g;s/list=//g;s/listType=.*&//g;s/feature=//g;s/share//g;s/player_embedded//g;s/embed//g;s/youtu.be//g;s/iv_load_policy=3//g;s/loop=[1,0]//g;s/modestbranding=[1,0]//g;s/showinfo=[1,0]//g;s/autoplay=[1,0]//g;s/rel=[1,0]//g;s/disablekb=[1,0]//g;s/controls=[1,0]//g;s/cc_load_policy=[1,0]//g;s/autohide=[1,0]//g;s/&.*$//g;s/?//g;s/=//g;s/start.*$//g;s/theme.*$//g;s/end.*$//g;s/color.*$//g;s/#t.*$//g'"
			else if pageURL_Im contains "youtube.com" then
				set pageURL to do shell script "echo " & quoted form of pageURL_Im & " | sed 's/%3A/\\:/g;s/%2F/\\//g;s/%3F/?/g;s/%3D/=/g;s/%26/\\&/g;s/player_embeddeds//g;s/attribution_link.*=\\///g;s/^.*.com\\///g;s/^.*.be\\///g;s/\\///g;s/watch?//g;s/playlist?//g;s/&v=//g;s/v=//g;s/list=//g;s/listType=.*&//g;s/feature=//g;s/share//g;s/player_embedded//g;s/embed//g;s/youtu.be//g;s/iv_load_policy=3//g;s/loop=[1,0]//g;s/modestbranding=[1,0]//g;s/showinfo=[1,0]//g;s/autoplay=[1,0]//g;s/rel=[1,0]//g;s/disablekb=[1,0]//g;s/controls=[1,0]//g;s/cc_load_policy=[1,0]//g;s/autohide=[1,0]//g;s/&.*$//g;s/?//g;s/=//g;s/start.*$//g;s/theme.*$//g;s/end.*$//g;s/color.*$//g;s/#t.*$//g'"
			else
				set pageURL to do shell script "echo " & quoted form of pageURL_Im & " | sed 's/http.*http/http/g;s/www.//g;s/%3A/\\:/g;s/%2F/\\//g;s/%3F/?/g;s/%3D/=/g;s/%26/\\&/g;s/vimeo.com\\/channels\\/.*\\//vimeo.com\\//g;s/vimeo.com\\/groups\\/.*\\/videos\\//vimeo.com\\//g'"
			end if
			
			-- Adds '--' flag to escape first-character hyphens in Youtube strings.
			set hyphenFixer to ""
			if pageURL starts with "-" then
				set hyphenFixer to "-- "
			end if
			
			-- Download shell script (only downloads first video of any multi-segmented video-URLs).
			do shell script "/usr/local/bin/" & pythonVrsn & " /usr/local/bin/youtube-dl " & videoQuality & " --playlist-end 1 -o ~/Desktop/PPCMC\\ Batch\\ Downloads\\ " & dateCurrent & "/\"%(title)s.%(ext)s\" " & hyphenFixer & "'" & pageURL & "' > /dev/null 2>&1 &"
			
			delay 1
			if urlListRun = urlListCount then
				exit repeat
			end if
			set urlListRun to urlListRun + 1
			
			-- Starts dependent download (holds script until complete - to ratchet download progress).
			set pageURL to ""
			set pageURL to item urlListRun of urlList as text
			
			-- Removes extraneous characters/formatting-variations in Youtube links, filters out Vimeo channels/groups code, and streamlines Google/Yahoo video search results.
			try
				set playlistCheck to false
				set pageURL_Im to pageURL
				if pageURL_Im contains "youtu.be" then
					set pageURL to do shell script "echo " & quoted form of pageURL_Im & " | sed 's/%3A/\\:/g;s/%2F/\\//g;s/%3F/?/g;s/%3D/=/g;s/%26/\\&/g;s/player_embeddeds//g;s/attribution_link.*=\\///g;s/^.*.com\\///g;s/^.*.be\\///g;s/\\///g;s/watch?//g;s/playlist?//g;s/&v=//g;s/v=//g;s/list=//g;s/listType=.*&//g;s/feature=//g;s/share//g;s/player_embedded//g;s/embed//g;s/youtu.be//g;s/iv_load_policy=3//g;s/loop=[1,0]//g;s/modestbranding=[1,0]//g;s/showinfo=[1,0]//g;s/autoplay=[1,0]//g;s/rel=[1,0]//g;s/disablekb=[1,0]//g;s/controls=[1,0]//g;s/cc_load_policy=[1,0]//g;s/autohide=[1,0]//g;s/&.*$//g;s/?//g;s/=//g;s/start.*$//g;s/theme.*$//g;s/end.*$//g;s/color.*$//g;s/#t.*$//g'"
				else if pageURL_Im contains "youtube.com" then
					set pageURL to do shell script "echo " & quoted form of pageURL_Im & " | sed 's/%3A/\\:/g;s/%2F/\\//g;s/%3F/?/g;s/%3D/=/g;s/%26/\\&/g;s/player_embeddeds//g;s/attribution_link.*=\\///g;s/^.*.com\\///g;s/^.*.be\\///g;s/\\///g;s/watch?//g;s/playlist?//g;s/&v=//g;s/v=//g;s/list=//g;s/listType=.*&//g;s/feature=//g;s/share//g;s/player_embedded//g;s/embed//g;s/youtu.be//g;s/iv_load_policy=3//g;s/loop=[1,0]//g;s/modestbranding=[1,0]//g;s/showinfo=[1,0]//g;s/autoplay=[1,0]//g;s/rel=[1,0]//g;s/disablekb=[1,0]//g;s/controls=[1,0]//g;s/cc_load_policy=[1,0]//g;s/autohide=[1,0]//g;s/&.*$//g;s/?//g;s/=//g;s/start.*$//g;s/theme.*$//g;s/end.*$//g;s/color.*$//g;s/#t.*$//g'"
				else
					set pageURL to do shell script "echo " & quoted form of pageURL_Im & " | sed 's/http.*http/http/g;s/www.//g;s/%3A/\\:/g;s/%2F/\\//g;s/%3F/?/g;s/%3D/=/g;s/%26/\\&/g;s/vimeo.com\\/channels\\/.*\\//vimeo.com\\//g;s/vimeo.com\\/groups\\/.*\\/videos\\//vimeo.com\\//g'"
				end if
				
				-- Adds '--' flag to escape first-character hyphens in Youtube strings.
				set hyphenFixer to ""
				if pageURL starts with "-" then
					set hyphenFixer to "-- "
				end if
				
				-- Download shell script (only downloads first video of any multi-segmented video-URLs).
				do shell script "/usr/local/bin/" & pythonVrsn & " /usr/local/bin/youtube-dl " & videoQuality & " --playlist-end 1 -o ~/Desktop/PPCMC\\ Batch\\ Downloads\\ " & dateCurrent & "/\"%(title)s.%(ext)s\" " & hyphenFixer & "'" & pageURL & "'"
			end try
			
			delay 1
			if urlListRun = urlListCount then
				exit repeat
			end if
			set urlListRun to urlListRun + 1
		end repeat
		
		-- Exits check-loop if no partial-downloads are found. If one of more partials are found, runs a second attempt.
		if finalCheck is true then
			exit repeat
		else if ((do shell script "ls ~/Desktop/PPCMC\\ Batch\\ Downloads\\ " & dateCurrent & "/") as text) does not contain ".part" then
			exit repeat
		else
			set finalCheck to true
		end if
	end repeat
	
	
	
	----
	-- End of Script.
	----
end timeout