Below is a simple AppleScript that does the following:
Asks for a folder location: Prompts the user to select a directory where the zip files are located.
Extracts (unzips) all zip files in the selected folder using unzip, which is more compatible with various zip file structures.
Deletes the original zip files after extraction.
Please note, this script assumes you have unzip installed on your macOS. If not, you can install it using Homebrew by running brew install unzip in the Terminal.
Here's the AppleScript:
applescript
-- Prompt the user to select a folder
set theFolder to (choose folder with prompt "Select a folder containing zip files to extract and then delete:")
-- Check if a folder was selected
if theFolder is not false then
-- Construct the full path for the shell command
set folderPath to POSIX path of theFolder
-- Shell script to find all zip files, unzip them, and then delete the originals
set shellCommand to "find '" & folderPath & "' -type f -name '*.zip' -exec unzip -d " & folderPath & " {} \\; -exec rm {} \\;"
try
-- Execute the shell command
do shell script shellCommand
display dialog "Zip files in the selected folder have been extracted and deleted." buttons {"OK"} default button "OK"
on error theError
display dialog "An error occurred: " & theError buttons {"OK"} default button "OK"
end try
else
display dialog "No folder was selected. Operation cancelled." buttons {"OK"} default button "OK"
end if
How to Use This Script:
Open Apple's Script Editor:
You can find it in Applications/Utilities/ or use Spotlight to search for "Script Editor".
Copy the Script:
Select all the text in the script box above and copy it (Cmd+C).
Paste into Script Editor:
Open a new document in Script Editor (File > New, or Cmd+N) and paste the script (Cmd+V).
Run the Script:
Click the "Run" button in the toolbar or press Cmd+R to execute the script.
Follow the prompt to select the folder containing your zip files.
Important Considerations:
Backup: Always ensure you have backups of your important files before running scripts that delete data.
Permissions: The script must have the necessary permissions to read the zip files and write the extracted contents to the same directory. If you encounter permission issues, consider running the script with elevated privileges or adjusting the folder's permissions.
Zip File Structure: This script extracts zip files in place, meaning the contents will be extracted to the same directory as the zip file. If your zip files are structured with a root folder inside the zip, the contents will be extracted with that root folder. If not, the files will be extracted directly into the selected folder.
Error Handling: The script now includes basic error handling to display any errors that occur during execution, such as permission issues or problems with the unzip command.
Alternative: Using a Bash Script
If you prefer a more traditional scripting approach or need more advanced features, you can use a Bash script instead:
bash
#!/bin/bash
# Prompt user to select a folder
echo "Select a folder containing zip files to extract and then delete:"
read -r folderPath
# Check if folder exists
if [ -d "$folderPath" ]; then
# Find zip files, extract, and delete originals
find "$folderPath" -type f -name '*.zip' -exec unzip -d "$folderPath" {} \; -exec rm {} \;
echo "Zip files extracted and deleted successfully."
else
echo "Invalid folder path. Operation cancelled."
fi
To use this Bash script, save it to a file (e.g., extract_and_delete.sh), give it execute permissions with chmod +x extract_and_delete.sh, and then run it with ./extract_and_delete.sh.