Applescript Find File Name Matching String And Delete Small Images

Kalali
May 23, 2025 · 4 min read

Table of Contents
Finding and Deleting Small Images with AppleScript: A Comprehensive Guide
This article provides a detailed guide on how to use AppleScript to locate and delete small images based on filename matching. This is particularly useful for cleaning up large folders containing many images, where small, low-resolution duplicates might clutter your storage. We'll cover the script's logic, its implementation, and essential considerations for safe and effective execution.
What this AppleScript does: This script searches a specified folder for image files whose filenames contain a particular string. It then checks the dimensions of each image and deletes those that fall below a specified size threshold. This prevents accidental deletion of larger, important images.
Before You Begin:
- Backup your data: Before running any script that deletes files, always back up your data. This is crucial to prevent data loss.
- Test the script: It's strongly recommended to test the script on a small sample folder before running it on a large collection of images. This allows you to verify its functionality and make adjustments if necessary.
- Understand the code: While we'll provide explanations, familiarizing yourself with basic AppleScript syntax will enhance your understanding and allow for future customization.
The AppleScript:
-- Set the target folder path. Replace "/path/to/your/folder" with the actual path.
set targetFolder to "/path/to/your/folder"
-- Set the string to match in filenames. Replace "thumbnail" with your desired string.
set searchString to "thumbnail"
-- Set the minimum width and height (in pixels) for images to be kept.
set minWidth to 800
set minHeight to 600
tell application "Finder"
set imageFiles to every file of entire contents of folder targetFolder whose name contains searchString
repeat with aFile in imageFiles
tell application "System Events"
tell process "Preview" -- or other suitable image viewer
try
open aFile
tell window 1
set imageWidth to width of image 1
set imageHeight to height of image 1
if imageWidth < minWidth or imageHeight < minHeight then
close window 1 without saving
delete aFile
log "Deleted: " & aFile as text
end if
end tell
on error
log "Error processing: " & aFile as text
end try
end tell
end tell
end repeat
end tell
Explanation:
-
set targetFolder
: Specifies the folder to search. Remember to replace/path/to/your/folder
with the actual path. You can obtain this by dragging the folder onto the AppleScript Editor. -
set searchString
: Defines the string that must be present in filenames for the files to be considered. Replace"thumbnail"
with the relevant string (e.g., "small", "lowres"). -
set minWidth
andset minHeight
: Set the minimum width and height thresholds. Images smaller than these dimensions will be deleted. Adjust these values based on your needs. -
tell application "Finder"
: This block interacts with the Finder application to locate files. -
every file of entire contents of folder targetFolder whose name contains searchString
: This line efficiently selects only the files matching your criteria. -
repeat with aFile in imageFiles
: This loop iterates through each selected file. -
tell application "System Events"
: This section uses System Events to interact with the "Preview" application (or other suitable image viewer) to get image dimensions. Make sure Preview (or your chosen app) is installed. -
try...on error
: This error handling ensures the script doesn't crash if it encounters a problem with a specific file. -
Image Dimension Check and Deletion: This checks the image dimensions. If they are below the thresholds, the image is deleted using
delete aFile
.
Important Considerations:
- Image Viewer: The script uses "Preview" by default. If you prefer a different image viewer (e.g., "ImageMagick"), you need to modify the script accordingly. Ensure the chosen viewer can be controlled via AppleScript.
- File Types: The script currently doesn't explicitly filter by image file type. You might want to add a check to ensure only specific file types (e.g.,
.jpg
,.png
,.gif
) are processed to avoid accidental deletion of non-image files. - Error Handling: The
try...on error
block provides basic error handling. Consider adding more robust error handling for production use.
By carefully reviewing and adapting this script to your specific requirements, you can effectively automate the process of cleaning up your image collection and freeing up valuable storage space. Remember to always prioritize safety and thoroughly test your script before applying it to your primary image folders.
Latest Posts
Latest Posts
-
Gears Of War 4 Trading Weapons
May 23, 2025
-
5 Color Theorem Graph Theory Proof Inductino
May 23, 2025
-
Can I Build My Own Runway
May 23, 2025
-
Captain America I Understood That Reference
May 23, 2025
-
Home Computer Versus Cell Phone Facebook Accounts
May 23, 2025
Related Post
Thank you for visiting our website which covers about Applescript Find File Name Matching String And Delete Small Images . We hope the information provided has been useful to you. Feel free to contact us if you have any questions or need further assistance. See you next time and don't miss to bookmark.