Error Handler For Duplicate Files?

Hello, I’m writing an Applescript that moves files based on their extension. However, I’m having several problems with it. First, I am trying to make a duplicate file handler and I’m not sure how to implement it. Second, I’m curious if I can make a file for the AppleScript access which contains the path to the download folder which it is to organize even after closing. Finally, can I add functionality for folders?

Thanks!

--Properties, adjust to your own specifications.
property graphicExtList : {"jpeg", "jpg", "gif", "psd", "png"}
property movieExtList : {"mov", "mpg", "mp4"}
property filesExtList : {"zip", "dmg", "exe", "pkg", "docx", "jar"}
property appExtList : {"app"}
property bookExtList : {"epub", "mobi"}
property musicExtList : {"mp3", "m4a", "ALAC"}
# I'll add more formats later on and add the ability to modify them and where they're moved.
set base to choose folder with prompt "Select the folder you'd like to organize."


set f1 to (base & "Files") as text
set f2 to (base & "Pics") as text
set f3 to (base & "Videos") as text
set f4 to (base & "Misc") as text
set f5 to (base & "Books") as text
set listOfNames to {}
set uname to system attribute "USER"

tell application "Finder"
	if not (exists folder f1) then ¬
		make new folder at base with properties {name:"Files"}
	if not (exists folder f2) then ¬
		make new folder at base with properties {name:"Pics"}
	if not (exists folder f3) then ¬
		make new folder at base with properties {name:"Videos"}
	if not (exists folder f4) then ¬
		make new folder at base with properties {name:"Misc"}
	if not (exists folder f5) then ¬
		make new folder at base with properties {name:"Books"}
end tell

tell application "Finder"
	set filelist to every file of the base
	repeat with currentFile in filelist
		if name extension of currentFile is in the filesExtList then
			move currentFile to f1
			
		else if name extension of currentFile is in the graphicExtList then
			move currentFile to f2
			
		else if name extension of currentFile is in the appExtList then
			move currentFile to f3
			
		else if name extension of currentFile is in the bookExtList then
			move currentFile to f4
			
		else
			move currentFile to f5
			
		end if
	end repeat
	
end tell

What do you want the duplicate command to do?


property graphicExtList : {"jpeg", "jpg", "gif", "psd", "png"}
property movieExtList : {"mov", "mpg", "mp4"}
property filesExtList : {"zip", "dmg", "exe", "pkg", "docx", "jar"}
property appExtList : {"app"}
property bookExtList : {"epub", "mobi"}
property musicExtList : {"mp3", "m4a", "ALAC"}
# I'll add more formats later on and add the ability to modify them and where they're moved.
set base to (choose folder with prompt "Select the folder you'd like to organize.")
do shell script "mkdir -p " & (quoted form of (POSIX path of base)) & "{\"Files\",\"Pics\",\"Videos\",\"Misc\",\"Books\"}"

tell application "Finder"
	move (every file of base whose name extension is in filesExtList) to (base & "Files") as text
	move (every file of base whose name extension is in graphicExtList) to (base & "Pics") as text
	move (every file of base whose name extension is in appExtList) to (base & "Videos") as text
	move (every file of base whose name extension is in bookExtList) to (base & "Misc") as text
	move every file of base to (base & "Books") as text
end tell

Wow! You really cleaned up my code! Thanks!! :smiley: I would like the duplicate file handler when moving a file with the same name to a folder to rename it like Finder does. So if there was a Vatican.jpg in one folder it’d be renamed Vatican1.jpg. Is that possible? I’ve spent a long time looking for something that does that but couldn’t find it. :confused:

Hello!

Experiment in a test folder with this:


to uniqueFilename(pthToFol, afilename)
	local fnend, mfile, msuf, i, numext, newname
	
	if class of pthToFol = alias then set pthToFol to pthToFol as text
	if text -1 of pthToFol is not ":" then set pthToFol to pthToFol & ":"
	if class of afilename = alias then set afilname to afilename as text
	
	set fnend to (length of afilename) - (offset of "." in (reverse of characters of afilename as text))
	set mfile to text 1 thru fnend of afilename
	
	set msuf to text (fnend + 1) thru -1 of afilename
	
	set i to 0
	set numext to ""
	try
		repeat
			
			set tf to (pthToFol & mfile & numext & msuf)
			set asa to tf as alias
			set i to i + 1
			set numext to " " & i
		end repeat
		
	on error
		set newname to (mfile & numext & msuf)
	end try
	return newname
end uniqueFilename

Hello, see if you can use this!




property graphicExtList : {"jpeg", "jpg", "gif", "psd", "png"}
property movieExtList : {"mov", "mpg", "mp4"}
property filesExtList : {"zip", "dmg", "exe", "pkg", "docx", "jar"}
property appExtList : {"app"}
property bookExtList : {"epub", "mobi"}
property musicExtList : {"mp3", "m4a", "ALAC"}
# I'll add more formats later on and add the ability to modify them and where they're moved.
set base to (choose folder with prompt "Select the folder you'd like to organize.")
do shell script "mkdir -p " & (quoted form of (POSIX path of base)) & "{\"Files\",\"Pics\",\"Videos\",\"Misc\",\"Books\"}"

tell application "Finder"
	set filesList to (every file of base whose name extension is in filesExtList)
	set grxList to (every file of base whose name extension is in graphicExtList)
	set vidList to (every file of base whose name extension is in appExtList)
	set bookList to (every file of base whose name extension is in bookExtList)
	set restList to (every file of base whose name extension is not in filesExtList or name extension is not in graphicExtList or name extension is not in appExtList or name extension is not in bookExtList)
end tell

set destFols to {(base & "Files") as text, (base & "Pics") as text, (base & "Videos") as text, (base & "Misc") as text, (base & "Books") as text}
set i to 1
repeat with al in {filesList, grexList, vidList, bookList, restList}
	process for al to (item i of destFols) by base
	set i to i + 1
end repeat

to process for aFileList to aDestination by aBaseDir
	set aBaseDir to aBaseDir as text
	
	repeat with aFile in aFileList
		set fnToUse to itemNameOfAnythingAsText(contents of aFile)
		set okFn to uniqueFilename(aDestination, fnToUse)
		tell application "Finder"
			set theResult to duplicate file contents of aFile to folder aBaseDir replacing no
			set name of theResult to okFn
			move theResult to aDestination
                        delete contents of aFile
		end tell
		
	end repeat
end process

to uniqueFilename(pthToFol, afilename)
	local fnend, mfile, msuf, i, numext, newname
	
	if class of pthToFol = alias then set pthToFol to pthToFol as text
	if text -1 of pthToFol is not ":" then set pthToFol to pthToFol & ":"
	if class of afilename = alias then set afilname to afilename as text
	
	set fnend to (length of afilename) - (offset of "." in (reverse of characters of afilename as text))
	set mfile to text 1 thru fnend of afilename
	
	set msuf to text (fnend + 1) thru -1 of afilename
	
	set i to 1
	set numext to ""
	try
		repeat
			
			set tf to (pthToFol & mfile & numext & msuf)
			set asa to tf as alias
			set i to i + 1
			set numext to " " & i
		end repeat
		
	on error
		set newname to (mfile & numext & msuf)
	end try
	return newname
end uniqueFilename

on itemNameOfAnythingAsText(fileOrFolderPath)
	-- whether it is a file, a folder,application or bundle. quoted,  with intrinsic quotes ...
	local tids, theFile, lastItem, tidsToUse, singleQuoteCheck -- Thanks to Yvan Koenig :)
	set singleQuoteCheck to true
	set tidsToUse to ":"
	try
		(get class of fileOrFolderPath)
	on error number -1728 -- it was a filereference
		set fileOrFolderPath to fileOrFolderPath as alias
	end try
	
	set fileOrFolderPath to "" & fileOrFolderPath -- doesn't harm.
	if fileOrFolderPath starts with "'" and fileOrFolderPath ends with "'" then
		set fileOrFolderPath to text 2 thru -2 of fileOrFolderPath
		set singleQuoteCheck to true
	end if
	if fileOrFolderPath starts with "/" then set tidsToUse to "/"
	set lastItem to -1
	if fileOrFolderPath ends with ":" then set lastItem to -2
	set {tids, AppleScript's text item delimiters} to {AppleScript's text item delimiters, tidsToUse}
	set fileName to text item lastItem of (fileOrFolderPath as text)
	if singleQuoteCheck is true then
		set AppleScript's text item delimiters to "'\\''"
		set fileName to text items of fileName -- as list
		set AppleScript's text item delimiters to "'"
	end if
	set fileName to "" & fileName
	set AppleScript's text item delimiters to tids
	return fileName
end itemNameOfAnythingAsText


for the dephts of the Everest that is my desk:

on movefiles(flist, dest)
	tell application "System Events"
		set dest to folder dest
		
		set destp to path of dest
		
		repeat with f in flist
			set f to file f
			if the visible of f then
				try
					move f to end of items of dest --without replacing
				on error e number n
					if n = -48 then
						set xt to name extension of f
						set onam to name of f
						set ix to (count of xt) + 1
	
						repeat with k from 1 to 100
							set newnam to text 1 thru -ix of onam & k & "." & xt
							((path of dest) & newnam)
							if not (item (destp & newnam) exists) then
								exit repeat
							end if
						end repeat
						set pat to path of container of f
						set name of f to newnam
						set f to file (pat & newnam)
						move f to end of items of dest
					end if
				end try
			end if
		end repeat
		
	end tell
end movefiles

flist is a list of file paths, as text, dest the destination folder, as text.

used to work…

Thanks everyone! :smiley: I’ll try them all out over the weekend!