How to Automate File Organization with AppleScript?

Hi everyone,

I’m fairly new to AppleScript and looking to automate some file organization tasks on my Mac. I have a folder where I regularly download files, and I’d like to create a script that moves files into specific subfolders based on their file types.
I also check this : https://www.macscripter.net/t/automating-file-organsnowflake But I have not found any solution.

Could anyone guide me on how to get started with this? What are the basic commands I should be aware of, and are there any sample scripts available that I could learn from?

Thanks in advance for your help.

You can accomplish what you want either by using Finder or System Events. The former is easier to use, and I’d recommend that for now. To get a feel for Finder and its commands and properties, open Script Editor and select File > Open Dictionary > Finder. You can also consult the AppleScript Language Guide here. I’ve included below a simple description of each step of my script suggestion:

  1. Set a variable to the source folder, which you will need to change to whatever is applicable on your computer. You could instead use a choose-folder command.
  2. Start the Finder tell statement, which tells the script to direct all commands within the statement to Finder.
  3. Get a list of all files in sourceFolder.
  4. Start a repeat loop to iterate through each file in the file list.
  5. Get the file extension of each file.
  6. Set the target folder using the concatenation operator, which adds together strings and other objects. As written, the script assumes that the target folders exist and that their names consist of the applicable file extension and a space and “files” (e.g. “txt files”).
  7. Check if the target folder exists, and, if not, report an error.
  8. Copy the file to the target folder using Finder’s duplicate command.

My script suggestion accomplishes the basics of what you want to do and works on my Sonoma computer. However, you will need to expand the script to do address other issues, such as:

  • What if the source folder does not contain any files.
  • What if a file already exists in a target folder.
  • Do you want to create a target folder if it doesn’t exist.
  • How do you modify the script to use file kind instead of file extension to classify and copy the files.
  • And so on.

The basic script:

set sourceFolder to "Macintosh HD:Users:Robert:Downloads:" -- set to correct value

tell application "Finder"
	set sourceFiles to every file in folder sourceFolder
	repeat with aFile in sourceFiles
		set fileType to name extension of aFile
		set targetFolder to sourceFolder & fileType & " files" & ":"
		if (folder targetFolder exists) is false then display dialog "The " & quote & fileType & quote & " target folder does not exist" buttons {"OK"} cancel button 1 default button 1
		duplicate aFile to folder targetFolder -- use move command if desired
	end repeat
end tell

FWIW, the following is a slightly enhanced version that creates the target folders if they don’t exist. Also, the file extension is uppercased to make the target folder names a bit more presentable (IMO) and provision is made for files with no extension. The script throws an error if a file exists in a target folder, and that needs to be handled.

use framework "Foundation"
use scripting additions

set sourceFolder to "Macintosh HD:Users:Robert:Downloads:" -- set to correct value

tell application "Finder"
	set sourceFiles to every file in folder sourceFolder
	repeat with aFile in sourceFiles
		set fileType to name extension of aFile
		if fileType is "" then set fileType to "No Extension"
		set fileType to getUppercase(fileType) of me -- uppercase file extension if desired
		set targetFolderName to fileType & " Files"
		set targetFolder to sourceFolder & targetFolderName & ":"
		if (folder targetFolder exists) is false then make new folder with properties {name:targetFolderName} at folder sourceFolder
		duplicate aFile to folder targetFolder -- use move command if desired
	end repeat
end tell

on getUppercase(lowercaseString)
	set lowercaseString to current application's NSString's stringWithString:lowercaseString
	set uppercaseString to lowercaseString's uppercaseString() as text
end getUppercase

That is a lot of great info from @peavine !
It sounds like you were thinking of this script as something you would run manually, when you want to move/sort the files.
But, if you later want to make it something that runs on an automatic recurring basis, that brings up the question of making sure a file isn’t still in the process of being downloaded. That isn’t always easy to do.

Krioni. I did assume that the script would be run manually. I don’t know what would happen if a file was being downloaded when the script was run. This is a difficult thing to test for, given today’s fast internet connections, but I assume that the file would be skipped or an error would be thrown.

1 Like