2

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"
2

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"
  • I had to tweak the code in your response just slightly… I cannot tell you how much I appreciate your help with this script. Definitely +1 and I am accepting your post as the solution to my question! – wch1zpink Aug 24 '17 at 22:21
  • I'm still trying to get a handle on which situations to use and or not to use the terms “as string, as text, as alias, as file…” as well as trying to get a handle on which situations to use and or not to use “alias, POSIX path, POSIX file…” Those examples right there is where I get jammed up. Endless hours trying statements using “as alias” then.. Nope that didn't work let me try “as text” … etc. Until I finally stumble on the correct sequence. – wch1zpink Aug 25 '17 at 1:57
  • 1
    Yes, the class of the variable is tricky. For clarification, text and string are practically the same. I normally like to make file paths "as text" for two reasons: 1. If you make it as alias, and the file doesn't exist yet, you will get an error. 2. You can modify the path when it's as text by adding more text (e.g. myPath & "file.txt"). However, if you want the finder to open it, it won't recognize text, so you can do 'open myPath as alias'. A good check is to log the class of the variable, and make sure it's what you think it is... 'log class of myPath' – Hurston Aug 28 '17 at 18:53
  • Also, posix path of myPath returns the path with slashes. e.g. /macHD/username/file.txt. POSIX file converts a posix path (with slashes) to standard HFS pathing e.g. macHD:username:file.txt. Posix paths are good to use with the terminal, which doesn't recognize HFS paths. – Hurston Aug 28 '17 at 19:00

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .