OS X: Applescript para expulsar discos duros locales

Para expulsar un disco local, es decir, conectado directamente a tu Mac, simplemente tienes que arrastrarlo a la papelera o seleccionarlo y pulsar el atajo de teclado Comando + tecla E. Pero en ocasiones, o simplemente, para rizar el rizo, puedes usar este Applescript que además de ser útil en ciertas ocasiones es un gran ejemplo de estudio de cómo trabajar con discos  duros y variables.

Para ejecutarlo, copia y pega el Applescript en el Editor Applescript:

set alldisks to paragraphs of (do shell script “df -hlg | awk -F/ ‘/disk*/ {print $5}'”)
try
set item 1 of alldisks to ((characters 1 thru -2 of (path to startup disk as string)) as text)
on error
set alldisks to {((characters 1 thru -2 of (path to startup disk as string)) as text)}
end try

set dur to {}
repeat with m in alldisks
considering case
  if m is not in {“MobileBackups”} then
   set dur to dur & m
  end if
end considering
end repeat

set devnames to paragraphs of (do shell script “df -k | awk -F/ ‘/disk*/ {print $3}'”)
set t to {}
repeat with s in devnames
set t to t & (word 1 of s)
end repeat

set the keylist to dur
set the foundDisks to t
set answer_list to {}

repeat with the_answer in keylist
set theIndex to my CollectUniqueItemIndex(keylist, (the_answer as string))
set theValue to item theIndex of foundDisks
activate
–set answer_list to answer_list & return & ((the_answer & “:” & space & theValue) as string)
–set answer_list to answer_list & ((the_answer & “:” & space & theValue) as string)
set answer_list to answer_list & (the_answer as string)
end repeat


set your_selected_device_id to choose from list answer_list with prompt “Por favor, seleccione los discos a expulsar.” with multiple selections allowed
repeat with b in your_selected_device_id
set theIndex to my CollectUniqueItemIndex(answer_list, (b as string))
set theValue to item theIndex of foundDisks
–display dialog “The device name of a volume you selected is:” & space & “”” & theValue & “”.” & space & “Are you sure you want to unmount it?”
try
  do shell script “diskutil unmount /dev/” & theValue
on error the error_message number the error_number
  display dialog “Error: ” & the error_number & “. ” & the error_message buttons {“OK”} default button 1
end try

end repeat

on CollectUniqueItemIndex(theList, theItem) — the Item can be string, number, constant, app object or list   
local aListMember
repeat with i from 1 to (count theList)
  set aListMember to item i of theList
  if aListMember = theItem then
   set theIndex to i
   exit repeat
  end if
end repeat
return theIndex
end CollectUniqueItemIndex

Al ejecutarlo, te aparecerá un cuadro de diálogo en el que podrás seleccionar varios discos y expulsarlos todos de una tacada. Funciona con Mac OS X 10.6 Snow Leopard y OS X 10.7 Lion.

0 0 votos
Article Rating
Subscribe
Notify of
2 Comments
Oldest
Newest Most Voted
Opiniones Inline
Ver todos los comentarios
juanfal
juanfal
11 years ago

Más fáci!!!

tell application “Finder”
eject (every disk)
end tell

¿no?

SI quieres notificación Growl cuando haya terminado (ya que a veces tarda y el Finder está escondido tras cienes de ventanas:

tell application “Finder”
eject (every disk)
end tell

tell application “System Events” to set Growler to exists application process “GrowlHelperApp”

if Growler then
tell application “GrowlHelperApp” to register as application “MyApp” all notifications {“Note”} default notifications {“Note”} icon of application “MyApp”
tell application “GrowlHelperApp”
notify with name “Note” title “Disks” description “Disks ejected” application name “MyApp” without sticky
end tell
else
tell application “System Events” to display dialog “Every disk ejected” with icon note buttons {“Bye”} default button 1 giving up after 5
end if

2
0
Me encantaría saber tu opinión, por favor, deja un comentariox
()
x