¿Cómo puedo solucionar mi AppleScript para eliminar una selección de canciones en iTunes?
-
12-09-2019 - |
Pregunta
He creado la siguiente AppleScript para borrar todas las pistas seleccionadas:
property okflag : false
-- check if iTunes is running
tell application "Finder"
if (get name of every process) contains "iTunes" then set okflag to true
end tell
if okflag then
tell application "iTunes"
if selection is not {} then
repeat with this_track in selection
try
try
set cla to class of this_track
set floc to (get location of this_track)
delete this_track
on error error_message number error_number
display alert error_message message ("Error number: ") & error_number & "."
end try
if cla is file track then
my delete_the_file(floc)
end if
end try
end repeat
end if
end tell
end if
to delete_the_file(floc)
try
-- tell application "Finder" to delete floc
do shell script "mv " & quoted form of POSIX path of (floc as string) & " " & quoted form of POSIX path of (path to trash as string)
on error
display dialog "Track deleted, but could not be moved to trash" buttons {"Hmm"} default button 1 with icon 1
end try
end delete_the_file
Funciona bien cuando selecciono un solo elemento, pero cuando selecciono más de un recibo: "No se puede obtener Ubicación del artículo 2 de la selección" (número de error -1728). Creo que esto se debe a que mediante la supresión de una pista, el índice de la secuencia de comandos en la selección está dañado.
pensé que iba a tratar de hacer mi propia lista de pistas que desea eliminar en primer lugar:
tell application "iTunes"
if selection is not {} then
set to_delete to {}
repeat with this_track in selection
try
set cla to class of this_track
set floc to (get location of this_track)
if cla is file track then
set pair to {this_track, floc}
set to_delete to to_delete & pair
end if
end try
end repeat
repeat with pair in to_delete
set the_track to item 1 of pair
set floc to item 2 of pair
delete the_track
my delete_the_file(floc)
end repeat
end if
end tell
Pero entonces llego 'No se puede obtener el punto 1 del artículo 1 de selección de aplicación 'iTunes'.' Creo que el problema es "this_track" no es un objeto de la clase de pista, pero un elemento de una selección. ¿Cómo consigo el objeto real de la vía del elemento de selección?
Si no ve la solución, voy a la bienvenida a consejos sobre la depuración o cualquier otra sugerencia.
Solución
El this_track
variable es una referencia a un especificador de objeto. Usted tiene que utilizar la propiedad contents
para obtener el especificador de objeto encerrado. Lo mismo es cierto para el acceso a la variable pair
en el segundo bucle. Vea la sección de referencia de clase de la clase en el reference
guía de lenguaje AppleScript .
Existe otro problema en la forma en que la lista de to_delete
se está construyendo. El set to_delete to to_delete & pair
declaración no producirá una lista de pares, sino una lista plana. Vea la sección de referencia de clase de la clase en el list
guía de lenguaje AppleScript .
Aquí hay una versión de su segundo guión, donde se han eliminado estos errores:
tell application "iTunes"
if selection is not {} then
set to_delete to {}
repeat with this_track in selection
try
set cla to class of this_track
set floc to (get location of this_track)
if cla is file track then
set pair to {contents of this_track, floc}
copy pair to end of to_delete
end if
end try
end repeat
repeat with pair in to_delete
set the_track to item 1 of contents of pair
set floc to item 2 of contents of pair
delete the_track
my delete_the_file(floc)
end repeat
end if
end tell