Como faço para corrigir meu AppleScript para excluir uma seleção de faixas no iTunes?
-
12-09-2019 - |
Pergunta
Eu criei o seguinte AppleScript para excluir todas as faixas selecionadas:
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
Ele funciona muito bem quando eu selecionar um único item, mas quando eu selecionar mais de um eu recebo: "Não é possível obter a localização do item 2 da seleção" (número de erro -1728). Eu acredito que este é porque, excluindo uma faixa, índice do roteiro para a selecção está corrompido.
Eu pensei que eu ia tentar fazer minha própria lista de faixas a serem excluídos primeiro:
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
Mas então eu recebo "Não é possível obter o item 1 do item 1 da seleção de aplicação 'iTunes'. Acho que o problema é "this_track" não é um objeto da classe Track, mas um item de uma seleção. Como faço para obter o objeto pista real do item de selecção?
Se você não ver a solução, eu vou acolher dicas sobre depuração ou quaisquer outras sugestões.
Solução
O this_track
variável é uma referência a um especificador objecto. Você tem que usar a propriedade contents
para obter o especificador objeto fechado. O mesmo é verdadeiro para aceder a pair
variável no segundo laço. Veja a seção de referência classe no reference
classe no href="http://developer.apple.com/mac/library/documentation/AppleScript/Conceptual/AppleScriptLangGuide" rel="nofollow noreferrer"> guia linguagem .
Outro problema existe na maneira como a lista to_delete
está sendo construída. O set to_delete to to_delete & pair
declaração não irá produzir uma lista de pares, mas uma lista simples. Veja a seção de referência classe no list
classe no href="http://developer.apple.com/mac/library/documentation/AppleScript/Conceptual/AppleScriptLangGuide" rel="nofollow noreferrer"> guia linguagem .
Aqui está uma versão do seu segundo script, onde esses bugs foram removidos:
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