Conditional Statement in AppleScript – Fix Guide

applescript

Basically the function of this script is to convert a video file to Apple TV compatible format and then delete the original files after the conversion is complete.

I'm absolutely at my “whits end”. For the past three hours I've tried every possible combination I could think of to get the conditional statement at the end of the script to function correctly.

Here's the situation. I download a lot of torrent video files which ultimately reside inside of a folder on my hard drive called “Finished”. 90% of the time, the downloaded videos are inside of its own folder which we will call “downloaded video” folder. Every now and then, I download a video file which is not contained in its own folder. So this downloaded video now resides in the “Finished” folder.

At the end of this script I'm trying to make the conditional statement which basically is.. If the downloaded file is inside of “downloaded video” folder which is inside of “Finished” folder… then delete the “downloaded video” folder (which contains the video file and any other files as well) <<—this part of the script works correctly BUT.. If the downloaded video file is not contained by a folder of its own and is now contained by the “Finished” folder… then just delete that one video file only. <<— this part does not work correctly. No matter what I try, I keep deleting my “Finished” folder (which happens to contain dozens of other folders that I want to remain untouched)

As a side note, I'm pretty sure I've added some steps in this script that did not need to be added. Still new to the whole scripting process 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"

I'm hoping someone can point me in the right direction.


UPDATE:

Here is a version of the code that functions perfectly!! This was taken from the Post by @Hurston who provided the edits to my code in his answer to my post

Just a few minor edits were necessary.

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"

Best Answer

As someone new to scripting, I'd say this is a pretty good effort. I think the problem has to do with variable classes (text vs. alias), and perhaps some confusion caused by redundant variables, so I've changed your code up a bit and put inline comments. I don't have handbreak installed, so I commented that part out, and ran the script, and now it correctly deletes either the containing folder (it it's not your finished folder) or the file itself. Please see below.

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"