# MacOS Image Background remove with Automator

- URL: https://www.heissenberger.at/en/blog/macos-image-background-remove-batch-automator/
- Language: en
- Published: 2025-01-10
- Tags: Tools, MacOS, Image, Automator, AppleScript, Batch Job, Howto

> Remove image background from multiple images with Automator on MacOS with builtin tools.

If you need to remove the background from multiple images, MacOS Automator does not provide a background removal action out of the box but the existing tools from MacOS can be combined to provide a solution to create a flexible batch job.

Since MacOS 15.x (Sequoia) provides the following manual options to remove the background from Images:

**Preview App**

*Figure: MacOS Preview App*

**Finder Shortcut "Remove Background"**

*Figure: Finder Shortcut Remove Background*

> **Note: the Finder Shortcut supports the transformation of multiple selected images! Use this if you do not need any control on output folder or file name.

## Automator Solution

1. Open **Automator** and create a new document
2. Choose **Application** as the document type
3. the first **Action** will be a short AppleScript to move an existing target folder to the trash folder. Add **Action** `Utilities -> Execute AppleScript`

```applescript
on run {input, parameters}

	tell application "Finder"
		-- Define the folder path
		set folderPath to (path to downloads folder as text) & "Images with removed Background"

		-- Check if the folder exists
		if exists folder folderPath then
			-- Delete the folder
			delete folder folderPath
		end if
	end tell

	return input
end run
```

4. Add **Action** `Files & Folders -> New Folder` to create a new Folder `Images with removed Background` in the downloads folder. Any Image or Folder dragged on this Automation will be copied to this folder.
5. Add **Action** `Files & Folders -> Get Folder Contents` and select the option `Repeat for each subfolder found`
6. Add **Action** `Photos -> Change Type of Image`. Cancel the dialog which asks you to add an additional action to copy the images before changing the type. This is not needed, as the images in the new folder are already copies of the original images. The format change is required as the **Preview.app** will replace the removed background with a transparent layer which is not supported by the JPEG format.
7. Add **Action** `Utilities -> Execute AppleScript`

```applescript
-- Wichtig! In Systemeinstellungen -> Datenschutz & Sicherheit
-- muss die App "Bilderfreistellen" unter Bedienungshilfen und
-- die App "Vorschau" unter Festplattenvollzugriff gelistet sein
on run {input, parameters}
	tell application "Preview"
		activate
		repeat with imgFile in input
			-- Open the image file in Preview
			open imgFile

			-- Wait for Preview to fully open the file
			delay 1

			-- Send the "Shift + Command + K" keyboard shortcut
			tell application "System Events"
				keystroke "k" using {shift down, command down}
			end tell

			-- Wait for the command to finish processing
			delay 1

			-- Save and close the file
			tell application "Preview"
				close document 1 saving yes
			end tell
		end repeat
	end tell
	tell application "Finder"
		-- Define the folder path
		set folderPath to (path to downloads folder as text) & "Images with removed Background"

		-- Check if the folder exists
		if exists folder folderPath then
			-- Show the folder
			open folderPath
		end if
	end tell
	return input
end run
```

8. Save the Automator document under `Image Background Removal`

The last AppleScript will iterate over all images in all subfolders. The background ist removed with the tool provided by the **Preview.app**.
When finished the the created folder with the transformed images will be opened in the Finder.

To provide this Automation with the **required access rights** you will need to:

1. add the `Image Background Removal` App to `System Preferences -> Privacy & Security -> Accessibility` (you will need to redo this if you change your Automation App!)
2. add the App `Preview.app` to `System Preferences -> Privacy & Security -> Full Disk Access`
3. allow your `Image Background Removal` App to control Finder, Preview and System Events - which will add it to `System Preferences -> Privacy & Security -> Automation`

To use this Automation drag one or multiple images or folders with images on this automation.
