My script is to get every file of a selectedFolder and attempt to rename them to the current files md5 checksum.
The problem with the script is, upon trying to rename a file with the current files md5 and that md5 is already written to another file, applescript will produce an error saying the file already exists
my script below
tell application "Finder"
set selectedFolder to choose folder
set fileList to items of selectedFolder
repeat with i from 1 to (count of fileList)
set currentFile to item i of fileList as alias
do shell script "md5 -q " & (POSIX path of currentFile)
set currentFilesMd5 to text of result
set AppleScript's text item delimiters to {"."}
set filename to name of currentFile
set currentFilesExtension to last text item of filename
set currentFilesPeriod to "." as text
set name of currentFile to currentFilesMd5 & currentFilesPeriod & currentFilesExtension
end repeat
end tell
So I decided to tell the script to check if the md5 is already in use in another file and if it is I want it to display a dialog
new code i entered into script is
set ifItExists to exists {selectedFolder & currentFilesMd5 & currentFilesPeriod & currentFilesExtension as string}
if ifItExists = true then
display dialog "file is there"
end if
It seems that the anomaly is linked to the fact that you failed to reset the AppleScript text item delimiters.
tell application "Finder"
set selectedFolder to choose folder
set fileList to items of selectedFolder
repeat with i from 1 to (count of fileList)
set currentFile to item i of fileList as alias
do shell script "md5 -q " & (POSIX path of currentFile)
set currentFilesMd5 to text of result
set AppleScript's text item delimiters to {"."}
set filename to name of currentFile
set currentFilesExtension to last text item of filename
set AppleScript's text item delimiters to {""} -- NEW
set currentFilesPeriod to "." as text
set name of currentFile to currentFilesMd5 & currentFilesPeriod & currentFilesExtension
end repeat
end tell
you forgot to reset the text item delimiters
Here is a shorter version of the script
set selectedFolder to choose folder
tell application "Finder"
repeat with i in (get items of selectedFolder)
set currentFilesMd5 to (do shell script "md5 -q " & (quoted form of POSIX path of (i as alias)))
set name of contents of i to currentFilesMd5 & "." & name extension of i
end repeat
end tell
You both are correct and thank you for the shorter version of script.
But I got one more problem which is veryconfusing to me
the script I have now is
tell application "Finder"
set selectedFolder to choose folder
set fileList to items of selectedFolder
repeat with i from 1 to (count of fileList)
set currentFile to item i of fileList as alias
do shell script "md5 -q " & (POSIX path of currentFile)
set currentFilesMd5 to text of result
set AppleScript's text item delimiters to {"."}
set filename to name of currentFile
set currentFilesExtension to last text item of filename
set AppleScript's text item delimiters to {""} -- NEW
set currentFilesPeriod to "." as text
--
set ifItExists to exists {selectedFolder & currentFilesMd5 & currentFilesPeriod & currentFilesExtension as string}
if ifItExists = true then
display dialog "file is there"
else
--
set name of currentFile to currentFilesMd5 & currentFilesPeriod & currentFilesExtension
end if
end repeat
end tell
the problem now is that the event log is saying
If the file DOES NOT EXIST then why does it say …already an item with the name. ??
edited to say File DOES NOT EXIST (I said exists earlier:()
tell application "Finder" to exists "Macintosh HD:Users:shortname:Desktop:temp:fe17a701a095da900259fb09b120a12d.rtf"
-- or
tell application "Finder" to exists "blah blah blah, i'm just text"
Instead of doing the work yourself, why not make use of that error?
try
set name of currentFile to currentFilesMd5 & currentFilesPeriod & currentFilesExtension
on error errMsg number errNum
display dialog "Error " & errNum & return & return & errMsg with icon caution buttons {"Cancel Script", "Skip Item"} default button 2
if button returned of result is "Cancel Script" then error number -128 -- cancel
end try
tell application "Finder"
set selectedFolder to choose folder
set fileList to items of selectedFolder
repeat with i from 1 to (count of fileList)
set currentFile to item i of fileList as alias
do shell script "md5 -q " & (POSIX path of currentFile)
set currentFilesMd5 to text of result
set AppleScript's text item delimiters to {"."}
set filename to name of currentFile
set currentFilesExtension to last text item of filename
set AppleScript's text item delimiters to {""}
set currentFilesPeriod to "." as text
try
set name of currentFile to currentFilesMd5 & currentFilesPeriod & currentFilesExtension
on error
move currentFile to trash
end try
end repeat
end tell