Aiuta a ottenere istruzioni condizionali alla fine di questo script, per funzionare correttamente

2

Fondamentalmente la funzione di questo script è convertire un file video in un formato compatibile con Apple TV e quindi eliminare i file originali al termine della conversione.

Sono assolutamente ai miei "estremi". Nelle ultime tre ore ho provato ogni possibile combinazione che potevo immaginare per ottenere il corretto funzionamento dell'istruzione condizionale alla fine dello script.

Ecco la situazione. Scarico un sacco di file video torrent che risiedono in definitiva all'interno di una cartella sul mio disco rigido chiamato "Finito". Il 90% delle volte, i video scaricati sono all'interno della propria cartella che chiameremo cartella "download video". Ogni tanto, scarica un file video che non è contenuto nella sua cartella. Quindi questo video scaricato ora risiede nella cartella "Finito".

Alla fine di questo script sto cercando di rendere l'istruzione condizionale che fondamentalmente è .. Se il file scaricato è all'interno della cartella "download video" che si trova all'interno della cartella "Finished" ... quindi eliminare il "video scaricato" "Cartella (che contiene anche il file video e qualsiasi altro file) < < -questa parte dello script funziona correttamente, MA .. Se il file video scaricato non è contenuto da una cartella propria ed è ora contenuto dal Cartella "Finito" ... quindi elimina solo quel solo file video. < < - questa parte non funziona correttamente. Non importa cosa cerco, continuo a cancellare la mia cartella "Finished" (che contiene decine di altre cartelle che voglio rimanere intatte)

Come nota a margine, sono abbastanza sicuro di aver aggiunto alcuni passaggi in questo script che non è necessario aggiungere. Ancora nuovo per l'intero processo di 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"

Spero che qualcuno possa indicarmi la giusta direzione.

UPDATE:

Ecco una versione del codice che funziona perfettamente !! Questo è stato preso dal Post di @Hurston che ha fornito le modifiche al mio codice nella sua risposta al mio post

Sono necessarie solo alcune modifiche minori.

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"
    
posta wch1zpink 24.08.2017 - 02:36
fonte

1 risposta

2

Come qualcuno nuovo nello scripting, direi che questo è uno sforzo piuttosto buono. Penso che il problema abbia a che fare con le classi variabili (testo vs alias) e forse qualche confusione causata da variabili ridondanti, quindi ho modificato un po 'il codice e inserito commenti in linea. Non ho installato handbreak, quindi ho commentato questa parte e ho eseguito lo script, e ora elimina correttamente la cartella contenente (non è la cartella finita) o il file stesso. Si prega di vedere sotto.

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"
    
risposta data 24.08.2017 - 16:49
fonte

Leggi altre domande sui tag