Applescript Find File Name Matching String And Delete Small Images

7 min read Oct 02, 2024
Applescript Find File Name Matching String And Delete Small Images

Automating Image Cleanup: Finding and Deleting Small Images with AppleScript

Are you tired of manually sifting through your folders to find and delete small images that clutter your hard drive? AppleScript can be your powerful ally in this task. Let's explore how you can automate the process of finding and deleting images based on their size and name.

Understanding the Challenge

Imagine a scenario where you have a large collection of images, many of which are tiny thumbnails or screenshots that you no longer need. Deleting them manually would be tedious and time-consuming. Wouldn't it be amazing to have a script that automatically finds and deletes those unwanted files?

AppleScript to the Rescue

AppleScript is a scripting language designed for automating tasks on macOS. With it, you can create custom scripts to interact with applications and files, making repetitive tasks a breeze. We will use AppleScript to:

  1. Search for files: We'll define a search criteria, such as file size and file name.
  2. Filter files: We'll use the script to focus only on image files within a specific folder or folders.
  3. Delete files: The script will efficiently remove the selected files from your hard drive.

Step-by-Step Guide

Here's how you can create your own AppleScript to automatically find and delete small images:

1. Open Script Editor:

  • Navigate to Applications > Utilities > Script Editor.

2. Create a New Script:

  • Click on File > New or press Command + N to create a new script file.

3. Write the AppleScript Code:

-- Set the folder path to search
set targetFolder to "/path/to/your/folder"

-- Define file size limit (in kilobytes)
set fileSizeLimit to 100

-- Set the string to match in the file name
set searchString to "thumbnail"

-- Get all files in the target folder
set fileItems to (list folder targetFolder)

-- Loop through each file
repeat with thisFile in fileItems
	-- Get the file name
	set fileName to name of thisFile

	-- Check if file name matches searchString
	if fileName contains searchString then
		-- Get the file size
		set fileSize to (get size of thisFile)

		-- Check if file size is smaller than the limit
		if fileSize < fileSizeLimit then
			-- Delete the file
			delete thisFile
		end if
	end if
end repeat

4. Modify the Script:

  • targetFolder: Replace /path/to/your/folder with the actual path to the folder you want to search.
  • fileSizeLimit: Adjust the fileSizeLimit value to filter files based on their size (in kilobytes).
  • searchString: Replace thumbnail with the string you want the file name to contain.

5. Run the Script:

  • Click on Run in the Script Editor's menu bar or press Command + R.

6. Review and Confirmation:

  • The script will prompt you for confirmation before deleting any files. Double-check the files before confirming.

7. Save the Script:

  • You can save the script by going to File > Save and choose a name for your script. This allows you to reuse the script in the future.

Additional Tips

  • Test it first: Before running the script on a large folder, it's a good idea to test it on a smaller folder or a few sample files to ensure it works as intended.
  • Use Finder: If you're not familiar with AppleScript, you can also use Finder to delete files. Press Command + A to select all files in a folder, then use the Spacebar to preview them and select the ones you want to delete.

Conclusion

By utilizing AppleScript, you can create automated workflows that help manage your images effectively. This script empowers you to remove small, unwanted images with ease, freeing up valuable disk space and keeping your folder organized. While this example focuses on deleting small images based on file size and name, you can customize the script to include additional criteria or actions based on your specific needs. Experiment with AppleScript to see how it can streamline your daily tasks and improve your workflow.