For files which are not included in project, but just hang-around in the folder, you can press
cmd ⌘ + alt ⌥ + A
and they won't be grayed out.
For files which are not referenced neither in xib nor in code, something like this might work:
#!/bin/sh
PROJ=`find . -name '*.xib' -o -name '*.[mh]'`
find . -iname '*.png' | while read png
do
name=`basename $png`
if ! grep -qhs "$name" "$PROJ"; then
echo "$png is not referenced"
fi
done
I tried Roman's solution, and I added a few tweaks to handle retina images. It works well, but remember that image names can be generated programmatically in code, and this script would incorrectly list these images as unreferenced. For example, you might have
This is a more robust solution - it checks for any reference to the basename in any text file. Note the solutions above that didn't include storyboard files (completely understandable, they didn't exist at the time).
Ack makes this pretty fast, but there are some obvious optimizations to make if this script runs frequently. This code checks every basename twice if you have both retina/non-retina assets, for example.
#!/bin/bash
for i in `find . -name "*.png" -o -name "*.jpg"`; do
file=`basename -s .jpg "$i" | xargs basename -s .png | xargs basename -s @2x`
result=`ack -i "$file"`
if [ -z "$result" ]; then
echo "$i"
fi
done
# Ex: to remove from git
# for i in `./script/unused_images.sh`; do git rm "$i"; done
I made a very slight modification to the excellent answer provided by @EdMcManus to handle projects utilizing asset catalogs.
#!/bin/bash
for i in `find . -name "*.imageset"`; do
file=`basename -s .imageset "$i"`
result=`ack -i "$file" --ignore-dir="*.xcassets"`
if [ -z "$result" ]; then
echo "$i"
fi
done
I don't really write bash scripts, so if there are improvements to be made here (likely) let me know in the comments and I'll update it.
Only this script is working for me which is even handling the space in the filenames:
Edit
Updated to support swift files and cocoapod. By default it's excluding the Pods dir and check only the project files. To run to check the Pods folder as well, run with --pod attrbiute :
/.finunusedimages.sh --pod
Here is the actual script:
#!/bin/sh
#varables
baseCmd="find ."
attrs="-name '*.xib' -o -name '*.[mh]' -o -name '*.storyboard' -o -name '*.mm' -o -name '*.swift'"
excudePodFiles="-not \( -path */Pods/* -prune \)"
imgPathes="find . -iname '*.png' -print0"
#finalize commands
if [ "$1" != "--pod" ]; then
echo "Pod files excluded"
attrs="$excudePodFiles $attrs"
imgPathes="find . $excudePodFiles -iname '*.png' -print0"
fi
#select project files to check
projFiles=`eval "$baseCmd $attrs"`
echo "Looking for in files: $projFiles"
#check images
eval "$imgPathes" | while read -d $'\0' png
do
name=`basename -s .png "$png"`
name=`basename -s @2x $name`
name=`basename -s @3x $name`
if grep -qhs "$name" $projFiles; then
echo "(used - $png)"
else
echo "!!!UNUSED - $png"
fi
done
It is heavily influenced by jeffhodnett‘s Unused, but honestly Unused is very slow, and the results are not entirely correct. So I made some performance optimization, the search speed is more faster than Unused.
Using the other answers, this one is a good example of how to ignore images on two directories and do not search occurrences of the images on the pbxproj or xcassets files (Be careful with the app icon and splash screens). Change the * in the --ignore-dir=*.xcassets to match your directory:
#!/bin/bash
for i in `find . -not \( -path ./Frameworks -prune \) -not \( -path ./Carthage -prune \) -not \( -path ./Pods -prune \) -name "*.png" -o -name "*.jpg"`; do
file=`basename -s .jpg "$i" | xargs basename -s .png | xargs basename -s @2x | xargs basename -s @3x`
result=`ack -i --ignore-file=ext:pbxproj --ignore-dir=*.xcassets "$file"`
if [ -z "$result" ]; then
echo "$i"
fi
done
Works damn well! Only 2 places I saw issues are when image names are from server and when the image asset name is different from the name of the image inside the asset folder...
It is important to pass project folder path as first argument, assets
folder path as second argument
It is assumed that all the images are maintained within Assets.xcassets folder and are used either within swift files or within storyboards
Limitations in first version:
Doesn't work for objective c files
I will try to improve it over the time, based on feedback, however the first version should be good for most.
Please find below the code. The code should be self explanatory as I have added appropriate comments to each important step.
# Usage e.g.: python3 unused_assets.py '/Users/DevK/MyProject' '/Users/DevK/MyProject/MyProject/Assets/Assets.xcassets'
# It is important to pass project folder path as first argument, assets folder path as second argument
# It is assumed that all the images are maintained within Assets.xcassets folder and are used either within swift files or within storyboards
"""
@author = "Devarshi Kulshreshtha"
@copyright = "Copyright 2020, Devarshi Kulshreshtha"
@license = "GPL"
@version = "1.0.1"
@contact = "kulshreshtha.devarshi@gmail.com"
"""
import sys
import glob
from pathlib import Path
import mmap
import os
import time
# obtain start time
start = time.time()
arguments = sys.argv
# pass project folder path as argument 1
projectFolderPath = arguments[1].replace("\\", "") # replacing backslash with space
# pass assets folder path as argument 2
assetsPath = arguments[2].replace("\\", "") # replacing backslash with space
print(f"assetsPath: {assetsPath}")
print(f"projectFolderPath: {projectFolderPath}")
# obtain all assets / images
# obtain paths for all assets
assetsSearchablePath = assetsPath + '/**/*.imageset' #alternate way to append: fr"{assetsPath}/**/*.imageset"
print(f"assetsSearchablePath: {assetsSearchablePath}")
imagesNameCountDict = {} # empty dict to store image name as key and occurrence count
for imagesetPath in glob.glob(assetsSearchablePath, recursive=True):
# storing the image name as encoded so that we save some time later during string search in file
encodedImageName = str.encode(Path(imagesetPath).stem)
# initializing occurrence count as 0
imagesNameCountDict[encodedImageName] = 0
print("Names of all assets obtained")
# search images in swift files
# obtain paths for all swift files
swiftFilesSearchablePath = projectFolderPath + '/**/*.swift' #alternate way to append: fr"{projectFolderPath}/**/*.swift"
print(f"swiftFilesSearchablePath: {swiftFilesSearchablePath}")
for swiftFilePath in glob.glob(swiftFilesSearchablePath, recursive=True):
with open(swiftFilePath, 'rb', 0) as file, \
mmap.mmap(file.fileno(), 0, access=mmap.ACCESS_READ) as s:
# search all the assests within the swift file
for encodedImageName in imagesNameCountDict:
# file search
if s.find(encodedImageName) != -1:
# updating occurrence count, if found
imagesNameCountDict[encodedImageName] += 1
print("Images searched in all swift files!")
# search images in storyboards
# obtain path for all storyboards
storyboardsSearchablePath = projectFolderPath + '/**/*.storyboard' #alternate way to append: fr"{projectFolderPath}/**/*.storyboard"
print(f"storyboardsSearchablePath: {storyboardsSearchablePath}")
for storyboardPath in glob.glob(storyboardsSearchablePath, recursive=True):
with open(storyboardPath, 'rb', 0) as file, \
mmap.mmap(file.fileno(), 0, access=mmap.ACCESS_READ) as s:
# search all the assests within the storyboard file
for encodedImageName in imagesNameCountDict:
# file search
if s.find(encodedImageName) != -1:
# updating occurrence count, if found
imagesNameCountDict[encodedImageName] += 1
print("Images searched in all storyboard files!")
print("Here is the list of unused assets:")
# printing all image names, for which occurrence count is 0
print('\n'.join({encodedImageName.decode("utf-8", "strict") for encodedImageName, occurrenceCount in imagesNameCountDict.items() if occurrenceCount == 0}))
print(f"Done in {time.time() - start} seconds!")