25/12/07

DVD en lecture automatique avec VLC

VLC DVD.scpt

Ce billet fait suite au premier du genre : ouverture automatique des DVD avec VLC ; lui-même suivi d'une légère mise à jour de l'AppleScript (Mise à jour de VLC DVD.scpt).

Pendant ces vacances de fin d'année, je me suis rendu compte que ce script ne fonctionnait plus, la mise à jour de Tiger vers Leopard y étant peut-être pour quelque chose (?), je me suis donc décidé à réécrire le VLC DVD.scpt "from scratch" (ou presque) de façon à en faire quelque chose d'un peu plus facile à maintenir par la suite ; faut dire que les versions précédentes étaient un peu "usine à gaz".

(*    
    VLC DVD.scpt
    Harry-Proton <http://blogosx.homeunix.org>
 
    updated Dec. 28, 2007
*)
 
on run
	set lsDisks to {} as list
	set lsDisks to FunctionMountedDevices()
	
	set i to 0 as small integer
	set isDVD to false as boolean
	set cntDisks to (count of lsDisks) as small integer
	
	repeat while ((isDVD is false) and (i < cntDisks))
		set nbDisk to (item (cntDisks - i) of lsDisks) as small integer
		set isDVD to FunctioniSMediaType(nbDisk, "DVD-ROM")
		set i to i + 1
		
		if ((isDVD) and (FunctioniSPartitionType(nbDisk, "Apple_partition_scheme") = false)) then
			FunctionLaunchDVD(nbDisk)
		end if
	end repeat
end run
 
(* 
Functions are below:
	FunctionMountedDevices() return list of disks
	FunctioniSTypeDisk(nbDisk, typeDisk) return boolean
	FunctioniSPartitionType(nbDisk, typePart) return boolean
	FunctionLaunchDVD(nbDisk) open DVD with VLC
	FunctionReplace(find, replace, str) str initial string
*)
 
on FunctionMountedDevices()
	set lsDisks to (do shell script "ls /dev/rdisk?") as string
	set lsDisks to FunctionReplace("/dev/rdisk", "", lsDisks)
	--lsDisks: "0 1 2 3…"
	set listVolumes to {} as list
	set nbVolumes to (count words of lsDisks) as small integer
	set i to 1 as small integer
	
	repeat nbVolumes times
		set nbDisk to ((word i of lsDisks) as small integer)
		set listVolumes to listVolumes & nbDisk
		
		set i to i + 1
	end repeat
	
	return listVolumes
end FunctionMountedDevices
 
on FunctioniSMediaType(nbDisk, type)
	set infoDisk to (do shell script "diskutil info disk" & nbDisk) as string
	set MediaType to (do shell script "echo '" & infoDisk & "' | awk -F \"Media Type:\" '{print $2}' | awk -F \"Protocol:\" '{print $1}'") as string
	if MediaType contains type then
		return true
	end if
	return false
end FunctioniSMediaType
 
on FunctioniSPartitionType(nbDisk, type)
	set infoDisk to (do shell script "diskutil info disk" & nbDisk) as string
	set PartitionType to (do shell script "echo '" & infoDisk & "' | awk -F \"Partition Type:\" '{print $2}' | awk -F \"Bootable:\" '{print $1}'") as string
	if (PartitionType contains type) then
		return true
	end if
	return false
end FunctioniSPartitionType
 
on FunctionLaunchDVD(nbDisk)
	set appDVD to "VLC"
	set nbDisk to nbDisk as small integer
	if exists application appDVD then
		tell application appDVD to open ":dev:rdisk" & nbDisk
		tell application "System Events"
			set frontmost of application process appDVD to true
			tell process appDVD
				keystroke "f" using command down
			end tell
		end tell
	end if
end FunctionLaunchDVD
 
on FunctionReplace(find, replace, str)
	set ditd to text item delimiters
	set res to missing value
	set text item delimiters to find
	repeat with tis in text items of str
		if res is missing value then
			set res to tis
		else
			set res to res & replace & tis
		end if
	end repeat
	set text item delimiters to ditd
	return res
end FunctionReplace

Le script s'utilise toujours à l'aide du Digital Hub de Mac OS X. Du fait de sa simplification, et de son amélioration, je pense suis sûr que la rapidité d'exécution a grandement été améliorée (par rapport aux précédentes versions), notamment du fait que le script "scrute" les disques par ordre de montage décroissant ; avec des temps d'exécution de l'ordre de la seconde. Ci-après, les résultats en secondes de 100 tests, effectués avec 6 volumes dont un DVD (monté en dernier) :

{0.998400211334, 1.002999782562, 1.991500139236, 1.001899957657, 1.002300024033, 1.001600027084, 1.996200084686, 1.001699924469, 1.993000030518, 1.001599788666, 1.002300024033, 1.00720000267, 1.992400169373, 1.003700017929, 1.002300024033, 1.994599819183, 1.002700090408, 1.004199981689, 1.99129986763, 1.001999855042, 1.003099918365, 1.992300033569, 1.004599809647, 1.994699954987, 1.007899999619, 1.993999958038, 1.996099948883, 0.998800039291, 1.003799915314, 1.992100000381, 1.005300045013, 1.992000102997, 1.006999969482, 1.005100011826, 1.008199930191, 1.993600130081, 1.005000114441, 1.991599798203, 1.004299879074, 1.994899988174, 1.005400180817, 1.002099990845, 1.004100084305, 1.007800102234, 1.995100021362, 1.003499984741, 1.995399951935, 1.006899833679, 1.995000123978, 1.006500005722, 1.004299879074, 1.002700090408, 1.993499994278, 1.001300096512, 1.002700090408, 1.992599964142, 1.005299806595, 1.995900154114, 1.993999958038, 1.001600027084, 1.006099939346, 1.995599985123, 1.996200084686, 1.00049996376, 1.996299982071, 0.996199846268, 1.994699954987, 1.002799987793, 2.001199960709, 1.002300024033, 1.003999948502, 1.993000030518, 1.003900051117, 1.002400159836, 1.003400087357, 1.995800018311, 1.003399848938, 1.002899885178, 1.992199897766, 1.003600120544, 1.001899957657, 1.994500160217, 1.003000020981, 1.99599981308, 1.003700017929, 1.99599981308, 1.002700090408, 1.997800111771, 1.002399921417, 1.994599819183, 1.00469994545, 2.000999927521, 1.002300024033, 1.995899915695, 1.003799915314, 1.994600057602, 1.004100084305, 1.996299982071, 1.004899978638, 1.003999948502}

soit un temps moyen d'exécution de 0.992923789024 seconde.

Amélioration : les DVD tels que le DVD d'installation de Mac OS X ne sont plus exécutés par le script dans VLC.

L'icône du script est sous licence CC by-nc-nd et provient du Front Row 1.3 Realistic Skin de MugenB16.

Télécharger le script

Télécharger l'archive contenant le script : scptVLC4DVD.zip (±212 Ko).

La discussion continue ailleurs

Can i force VLC to launch a DVD when i insert it in

commenter (0)