Applescript giving error 1728 when checking if file exists
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.
Solution 1:
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
Solution 2:
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