Take the 2-minute tour ×
Ask Different is a question and answer site for power users of Apple hardware and software. It's 100% free, no registration required.

I'm trying to make compressing a video file using HandBrake easier through applescript and have been basing most of the code off of templates I've found.
At this point in the code, the user has specified the source file and the destination folder.
Instead of HandBrakeCLI just blindly overwriting a file with the same name in the destination folder, I wanted Finder to do a check and, if a file with the same does already exist in the destination folder, then I wanted to have the filename appended with the current date and time before the extension.
Everything worked fine until I started adding the "if exists" code.

Here's the chunk of code:

tell application "Finder"
    set newFilepathPOSIX to POSIX path of dest_folder
    set newFilepathPOSIX to newFilepathPOSIX & "video.mp4"
    -- Check if file with same name exists
    if exists file newFilepathPOSIX then
        display dialog "There is already a file named \"video.mp4\". Do you want to replace the file?" buttons {"Quit", "No", "Yes"} cancel button 1
        if result = {button returned:"No"} then
            set newFilepathPOSIX to POSIX path of dest_folder
            set newFilepathPOSIX to newFilepathPOSIX & "video" & getDateAsString & ".mp4"
        end if
    end if
    set newFilepathPOSIX to quoted form of newFilepathPOSIX
    set shellCommand to "/Volumes/Flash Drive/HandBrakeCLI -i " & origFilepathPOSIX & " -o " & newFilepathPOSIX & " --preset=\"Classic\""
end tell

to getDateAsString from t
    set creationDateString to year of t as string
    set creationDateString to creationDateString & text -2 thru -1 of ("0" & getMonthNum(t) as string)
    set creationDateString to creationDateString & text -2 thru -1 of ("0" & day of t as string)
    set creationDateString to creationDateString & text -2 thru -1 of ("0" & hours of t as string)
    set creationDateString to creationDateString & text -2 thru -1 of ("0" & minutes of t as string)
    set creationDateString to creationDateString & text -2 thru -1 of ("0" & seconds of t as string)
    return creationDateString
end getDateAsString

Here's what it returns:

exists file "/Users/me/Box Documents/My Folder/video.mp4"
    --> false

If I add "as POSIX file" to the "if exists" line, this is what it returns:

get file "/Users/me/Box Documents/My Folder/video.mp4"
    --> error number -1728 from file "/Users/me/Box Documents/My Folder/video.mp4"
exists file "My HD:Users:me:Box Documents:My Folder:video.mp4"
    --> true
display dialog "There is already a file named \"video.mp4\". Do you want to replace the file?" buttons {"Quit", "No", "Yes"} cancel button 1
    --> {button returned:"No"}

After this, it errored out.
Totally new at this so hopefully I've given enough detail without giving away way too much detail.

share|improve this question
add comment

3 Answers 3

Thanks for the response, adayzdone.
Hopefully I'm responding in an acceptable manner.
I tried it your way (straightforward), then I tried it the way I think HandBrakeCLI would want it and both results were the same.

Here's the updated chunk that I think HandBrakeCLI needs to work:

tell application "Finder"
    set dest_folder2 to POSIX path of dest_folder
    set newFilepathPOSIX to dest_folder2 & "video.mp4"
    -- Check if file with same name exists
    tell application "System Events"    
        if exists file newFilepathPOSIX then
            display dialog "There is already a file named \"video.mp4\". Do you want to replace the file?" buttons {"Quit", "No", "Yes"} cancel button 1
            if result = {button returned:"No"} then
                tell application "Finder"            
                    set newFilepathPOSIX to POSIX path of dest_folder
                    set newFilepathPOSIX to newFilepathPOSIX & "video" & getDateAsString & ".mp4"
                end tell
            end if
        else
            tell application "Finder"
                set newFilepathPOSIX to POSIX path of dest_folder
                set newFilepathPOSIX to newFilepathPOSIX & "video" & ".mp4"
            end tell
        end if
    end tell
    display dialog newFilepathPOSIX -- Test to see if it could get this far
    set newFilepathPOSIX to quoted form of newFilepathPOSIX
    set shellCommand to "/Volumes/Flash Drive/HandBrakeCLI -i " & origFilepathPOSIX & " -o " & newFilepathPOSIX & " --preset=\"Classic\""
end tell

Here's the result:

tell application "System Events"
    exists file "/Users/me/Box Documents/My Folder/video.mp4"
        --> true
    display dialog "There is already a file named \"video.mp4\". Do you want to replace the file?" buttons {"Quit", "No", "Yes"} cancel button 1
        --> error number -1708
    «event ascrgdut»
        --> error number -1708
    display dialog "There is already a file named \"video.mp4\". Do you want to replace the file?" buttons {"Quit", "No", "Yes"} cancel button 1
        --> {button returned:"No"}
end tell

As you can see in the result, the script never did get to display the test dialog.
I'm probably messing something up because of how new I am to coding in general.
I anticipate that my updated chunk will be seen as an abomination to applescript, but I really don't know how to make it more efficient.
Maybe you or someone else can offer a different approach or fix my updated chunk?

share|improve this answer
    
You should amend your original question and delete this "answer". I took a few guesses to fill in the blanks... see my EDIT –  adayzdone Feb 11 '13 at 19:20
add comment

Try:

set dest_folder to POSIX path of dest_folder
set newFilepathPOSIX to dest_folder & "video.mp4"

tell application "System Events" to exists file newFilepathPOSIX
if the result then
    display dialog "There is already a file named \"video.mp4\". Do you want to replace the file?" buttons {"Quit", "No", "Yes"} cancel button 1
    if button returned of the result = "No" then
        beep
        --Insert your code here
    end if
end if

EDIT

 set handPath to "/Volumes/Flash Drive/HandBrakeCLI"
set dest_folder to POSIX path of dest_folder
if text -1 of dest_folder ≠ "/" then set dest_folder to dest_folder & "/"
set newFilepathPOSIX to dest_folder & "video.mp4"

tell application "System Events" to exists newFilepathPOSIX
if the result then
    display dialog "There is already a file named \"video.mp4\". Do you want to replace the file?" buttons {"Quit", "No", "Yes"} cancel button 1
    if button returned of the result = "No" then set newFilepathPOSIX to dest_folder & "video" & getDateAsString(current date) & ".mp4"
end if

set shellCommand to quoted form of handPath & " -i " & quoted form of origFilepathPOSIX & " -o " & quoted form of newFilepathPOSIX & " --preset=\"Classic\""
do shell script shellCommand

on getDateAsString(myDate)
    set creationDateString to year of myDate as string
    set creationDateString to creationDateString & text -2 thru -1 of ("0" & (month of myDate as integer) as text)
    set creationDateString to creationDateString & text -2 thru -1 of ("0" & day of myDate as text)
    set creationDateString to creationDateString & text -2 thru -1 of ("0" & hours of myDate as text)
    set creationDateString to creationDateString & text -2 thru -1 of ("0" & minutes of myDate as text)
    set creationDateString to creationDateString & text -2 thru -1 of ("0" & seconds of myDate as text)
end getDateAsString
share|improve this answer
    
Thanks, but now I'm getting 1708 errors (see my response "answer" below). –  thefigg88 Feb 11 '13 at 15:35
add comment

You could just write the files out with the time stamp always included. And you do not need all that code to do it.

set theDate to do shell script "date +%Y_%m_%d_%H%M%S" -- use the unix  date command to get a formated text date

set newFilepathPOSIX to POSIX path of dest_folder
set newFilepathPOSIX to newFilepathPOSIX & "video_" & theDate & ".mp4"

Also only put code between tell blocks that needs to be interpreted by a given Application.

I.e the code below doe not need a tell block.

None of the below example code is in a tell block but works fine.

  set newFilepathPOSIX to "/Users/USERNAME/Movies/CLI/"
    set theDate to do shell script "date +%Y_%m_%d_%H%M%S"
   set the sourceFile to "/Users/USERNAME/Movies/The Studio.m4v"
    set newFilepathPOSIX to newFilepathPOSIX & "video_" & theDate & ".mp4"

   set shellCommand to "/Volumes/HandBrake-0.9.8-MacOSX.6_CLI_x86_64/HandBrakeCLI -i " & quoted form of sourceFile & " -o " & quoted form of newFilepathPOSIX & " --preset=\"Classic\""

    do shell script shellCommand

Putting code erroneously between them (like your doing with the finder) will raise unexpected errors or results.

You can also get the title of a video if it has one use the mdls command

example: This will look for the video's first title. If it does not find one then it will use "video"

set theDate to do shell script "date +%Y_%m_%d_%H%M%S"
set sourceFile to "/Users/USERNAME/Desktop/Desktop--MOVIES/The Studio2.mov"

set nulmaker to "Video"
set title to (do shell script "mdls   -name  kMDItemTitle -raw -nullMarker" & space & quoted form of nulmaker & space & quoted form of sourceFile) -- get the first title name or if none set to "Video"
set newFilepathPOSIX to newFilepathPOSIX & title & "_" & theDate & ".mp4"



set shellCommand to "/Volumes/HandBrake-0.9.8-MacOSX.6_CLI_x86_64/HandBrakeCLI -i " & quoted form of sourceFile & " -o " & quoted form of newFilepathPOSIX & " --preset=\"Classic\""
do shell script shellCommand
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.