Ayuda a obtener una declaración condicional al final de este script, para que funcione correctamente

2

Básicamente, la función de este script es convertir un archivo de video al formato compatible con Apple TV y luego eliminar los archivos originales una vez que se completa la conversión.

Estoy absolutamente en mi "final de whits". Durante las últimas tres horas he intentado todas las combinaciones posibles que se me ocurren para lograr que la sentencia condicional al final de la secuencia de comandos funcione correctamente.

Aquí está la situación. Descargo muchos archivos de video de torrent que, en última instancia, residen dentro de una carpeta en mi disco duro llamada "Finalizado". El 90% de las veces, los videos descargados están dentro de su propia carpeta que llamaremos carpeta de "video descargado". De vez en cuando, descargo un archivo de video que no está contenido en su propia carpeta. Así que este video descargado ahora reside en la carpeta "Finalizado".

Al final de este script, estoy tratando de hacer la declaración condicional que básicamente es ... Si el archivo descargado está dentro de la carpeta "video descargado" que está dentro de la carpeta "Finalizado" ... luego elimine el "video descargado" ”(Que contiene el archivo de video y cualquier otro archivo también) < < —esta parte del script funciona correctamente PERO ... Si el archivo de video descargado no está contenido en una carpeta propia y ahora está contenido en la carpeta Carpeta "finalizada" ... luego solo elimine ese archivo de video solamente. < < - esta parte no funciona correctamente. No importa lo que intente, sigo eliminando mi carpeta "Terminada" (que contiene docenas de otras carpetas que quiero que permanezcan intactas)

Como nota al margen, estoy bastante seguro de que he agregado algunos pasos en este script que no fue necesario agregar. Todavía nuevo en todo el proceso de scripting LOL

property creationDate : (current date) - (minutes * 10)
property inputPath1 : alias "Macintosh HD:Users:Smokestack:Documents:Vuze Downloads:Finished:"
property outputPath1 : alias "Macintosh HD:Users:Smokestack:Music:iTunes:iTunes Media:Home Videos:"


set inputPath to POSIX path of inputPath1
set outputPath to POSIX path of outputPath1
set theIcon to path to resource "Apple_TV_Logo.png"

display dialog "     CONVERT VIDEO FOR APPLE TV" buttons {"Cancel", "Choose File"} default button 2 with icon theIcon
if the button returned of the result is "Choose File" then
    set theProcess to choose file with prompt "Choose Video Files To Convert For Apple TV" default location inputPath1
    set theFile to the result -- sets the variable to the name of the chosen file
    set theFile1 to theFile
    set deleteOriginalFolder to theFile -- used at the end of the script to delete the original video chosen
    tell application "System Events"
        set theName to name of theProcess -- get the name of the file to insert its value in the following lines
    end tell
    set outputPath2 to (outputPath & theName & ".m4v")
    display dialog "PLEASE BE PATIENT.  THIS PROCESS COULD TAKE UP TO 30 MINUTES TO COMPLETE" buttons {"OK"} default button 1 with icon theIcon giving up after 7

    set theFile to POSIX path of theFile
    set theFile to "-i " & quoted form of theFile & " -o " & quoted form of outputPath & quoted form of theName & ".m4v"
    do shell script "/Applications/HandBrakeCLI -Z \"AppleTV 3\" " & theFile
else
    return
end if

-- The Next Command... For Videos Added To iTunes To Be Immediately Available In Apple Tv, The Video Must Be Plyed, For At Least A Brief Second, First In iTunes

tell application "Finder"
    open (every item of outputPath1 whose creation date > creationDate)
end tell

delay 2

-- The Next Command... Closes The Video Window In iTunes

tell application "System Events"
    keystroke "." using command down
end tell

-- Below Is Where I'm Jammed Up

tell application "Finder"
    set deleteOriginalFolder to the container of deleteOriginalFolder
    if deleteOriginalFolder is not equal to inputPath1 then
        delete deleteOriginalFolder -- This Deletes The Original File And It's Containing Folder if it is located inside alias "Macintosh HD:Users:Smokestack:Documents:Vuze Downloads:Finished:"
    else
        if theFile1 is in inputPath1 then -- this is supposed to delete the original file only..  If it's container is  alias "Macintosh HD:Users:Smokestack:Documents:Vuze Downloads:Finished:"
            delete theFile1
        end if
    end if
end tell

display notification "Your Video Is Now In Your iTunes Home Videos Folder And Is Available To Be Watched With Apple Tv" with title "YOUR VIDEO CONVERSION HAS COMPLETED" sound name "submarine"

Espero que alguien pueda apuntarme en la dirección correcta.

ACTUALIZAR :

¡Aquí hay una versión del código que funciona perfectamente! Esto fue tomado de la publicación de @Hurston que proporcionó las modificaciones a mi código en su respuesta a mi publicación

Solo fueron necesarias algunas ediciones menores.

property creationDate : (current date) - (minutes * 10)
property inputPath1 : (path to documents folder as text) & "Vuze Downloads:Finished:"
property outputPath1 : (path to music folder as text) & "iTunes:iTunes Media:Home Videos:"

set inputPath to POSIX path of inputPath1
set outputPath to POSIX path of outputPath1
set theIcon to path to resource "Apple_TV_Logo.png"

display dialog "CONVERT VIDEO FOR APPLE TV" buttons {"Cancel", "Choose File"} default button 2 with icon theIcon
if the button returned of the result is "Choose File" then
    set theFile to choose file with prompt "Choose Video Files To Convert For Apple TV" default location (inputPath1 as alias)

    tell application "System Events"
        set theName to name of theFile
    end tell

    set outputPath2 to (outputPath & theName & ".m4v")

    display dialog "PLEASE BE PATIENT.  THIS PROCESS COULD TAKE UP TO 30 MINUTES TO COMPLETE" buttons {"OK"} default button 1 with icon theIcon giving up after 7
    set posixFile to POSIX path of theFile
    set posixFile to "-i " & quoted form of posixFile & " -o " & quoted form of outputPath2
    do shell script "/Applications/HandBrakeCLI -Z \"AppleTV 3\" " & posixFile
else
    return
end if

tell application "Finder"
    open (every item of folder outputPath1 whose creation date > creationDate)
end tell

delay 2

tell application "System Events"
    keystroke "." using command down
end tell

tell application "Finder"
    set deleteOriginalFolder to the container of theFile
    if (deleteOriginalFolder as text) is not equal to inputPath1 then
        delete deleteOriginalFolder
    else
        delete theFile
    end if
end tell

display notification "Your Video Is Now In Your iTunes Home Videos Folder And Is Available To Be Watched With Apple Tv" with title "YOUR VIDEO CONVERSION HAS COMPLETED" sound name "submarine"
    
pregunta wch1zpink 24.08.2017 - 02:36

1 respuesta

2

Como alguien nuevo en scripting, diría que este es un esfuerzo bastante bueno. Creo que el problema tiene que ver con las clases de variables (texto vs. alias), y quizás con la confusión causada por las variables redundantes, así que cambié un poco su código y puse comentarios en línea. No tengo el handbreak instalado, así que comenté esa parte, corrí la secuencia de comandos y ahora elimina correctamente la carpeta que contiene (no es su carpeta terminada) o el archivo en sí. Por favor vea abajo.

property creationDate : (current date) - (minutes * 10)
property inputPath1 : (path to documents folder as text) & "Vuze 
Downloads:Finished:" --I used the built-in path to command in case something in the path changes, and set this to text rather than an alias, because text is easier to manipulate. Also, it wouldn't compile on my system because that path doesn't exist, but as text, it will compile.
property outputPath1 : (path to music folder as text) & "iTunes:iTunes Media:Home Videos:" --same as above

set inputPath to POSIX path of inputPath1
set outputPath to POSIX path of outputPath1
set theIcon to path to resource "Apple_TV_Logo.png"

display dialog "CONVERT VIDEO FOR APPLE TV" buttons {"Cancel", "Choose File"} default button 2 with icon theIcon
if the button returned of the result is "Choose File" then
    set theFile to choose file with prompt "Choose Video Files To Convert For Apple TV" default location (inputPath1 as alias) --I changed theProcess to theFile, for simplicity later on
    --set theFile to the result --What was theProcess is already this alias, you set it in the step above. No need to create another variable with the exact same data
    --set theFile1 to theFile --This is the third time you've assigned a variable to the same thing. Unnecessary.
    --set deleteOriginalFolder to theFile -- This is the fourth time... unnecessary

tell application "System Events"
    set theName to name of theFile
end tell

set outputPath2 to (outputPath & theName & ".m4v")

display dialog "PLEASE BE PATIENT.  THIS PROCESS COULD TAKE UP TO 30 MINUTES TO COMPLETE" buttons {"OK"} default button 1 with icon theIcon giving up after 7
set posixFile to POSIX path of theFile --changed this from theFile to posixFile for clarity, and in case you need theFile later on.
set posixFile to "-i " & quoted form of posixFile & " -o " & quoted form of outputPath2 --same as above
do shell script "/Applications/HandBrakeCLI -Z \"AppleTV 3\" " & posixFile --same as above 
else--else statement is unnecessary, because the "Cancel" button returns a "User Cancelled" message to the script, but I left this
    return
end if

-- The Next Command... For Videos Added To iTunes To Be Immediately Available In Apple Tv, The Video Must Be Plyed, For At Least A Brief Second, First In iTunes

tell application "Finder"
    open (every item of folder outputPath1 whose creation date > creationDate) --added the word folder because outputPath1 is now text
end tell

delay 2
-- The Next Command... Closes The Video Window In iTunes

tell application "System Events"
    keystroke "." using command down
end tell

-- Below Is Where I'm Jammed Up

tell application "Finder"
    set deleteOriginalFolder to the container of theFile --changed deltedOriginalFolder to theFile
    if (deleteOriginalFolder as text) is not equal to inputPath1 then 
--I added the "as text" to make sure the comparison would work
        delete deleteOriginalFolder -- This Deletes The Original File And It's Containing Folder if it is located inside alias "Macintosh HD:Users:Smokestack:Documents:Vuze Downloads:Finished:"
    else
        --if (theFile as text) inputPath1 then -- don't need the if statement because you've already checked above to see if it's in it's own folder. just delete the file
        delete theFile
        --end if
    end if
end tell

display notification "Your Video Is Now In Your iTunes Home Videos Folder And Is Available To Be Watched With Apple Tv" with title "YOUR VIDEO CONVERSION HAS COMPLETED" sound name "submarine"
    
respondido por el Hurston 24.08.2017 - 16:49

Lea otras preguntas en las etiquetas