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.