Come posso risolvere il mio AppleScript per cancellare una selezione di brani in iTunes?
-
12-09-2019 - |
Domanda
Ho creato il seguente AppleScript per l'eliminazione di tutte le tracce selezionate:
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
Funziona bene quando si seleziona un singolo elemento, ma quando seleziono più di un'ottengo: "Impossibile ottenere la posizione del punto 2 di selezione" (numero di errore -1728). Credo che questo sia perché per l'eliminazione di un brano, indice dello script nella selezione è danneggiato.
ho pensato di provare a fare la mia lista delle tracce da eliminare prima:
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
Ma poi ho 'Non è possibile ottenere punto 1 punto 1 della selezione di applicazione 'iTunes'.' Credo che il problema è "this_track" non è un oggetto di traccia di classe, ma un elemento di una selezione. Come posso ottenere l'oggetto pista reale dalla voce di selezione?
Se non si vede la soluzione, sarò il benvenuto consigli su debug o altri suggerimenti.
Soluzione
Il this_track
variabile è un riferimento ad un oggetto specificatore. Devi usare la proprietà contents
per ottenere l'identificatore di oggetto chiuso. Lo stesso vale per l'accesso al pair
variabile nel secondo ciclo. Vedere la sezione di riferimento di classe sulla reference
classe nel guida linguaggio AppleScript .
Un altro problema esiste nel modo in cui la lista to_delete
è in costruzione. Il set to_delete to to_delete & pair
dichiarazione non produrrà un elenco di coppie, ma una semplice lista. Vedere la sezione di riferimento di classe sulla list
classe nel guida linguaggio AppleScript .
Ecco una versione del secondo script, dove sono stati rimossi questi bug:
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