You can make a difference in the Apple Support Community!

When you sign up with your Apple Account, you can provide valuable feedback to other community members by upvoting helpful replies and User Tips.

Script: How to find videos and photos in your Photos Library larger or smaller than a given file size

by: 
Last modified: Jan 19, 2019 11:49 AM
2 7650 Last modified Jan 19, 2019 11:49 AM

The Photos.app does not allow us to sort albums by the file size of the items ins an album. But an AppleScript can read the file size of a media item in Photos and create albums based on the file size.


if you want to find files, that are too large or to small among a set of media items in Photos, select the videos or photos you want to check for their size and run the script below.

It will ask for a file size threshold - you could enter 512 kB, for example, then run the script. It will create two albums, one with the items smaller or equal than 512kB, one with the items larger than 512kB. Now you can select easily the items that are too small or too large in one of the albums.



-- ✄ ✄ -- snip -- copy from here

(* How to use this script:


This script will split the selection into two albums -

- one album with pictures or videos larger than the given file size threshold

- one album with pictures or videos smaller than the given file size threshold


Open this script in Script Editor. Launch Photos.

Select the photos and videos you want to distribute between the albums.


When all all photo are selected, press the "Run" button in Script Editor.


Author: léonie

*)


--the file size threshold in kB

set defaultFileSizeThreshold to 10240 -- 10 MB, change this to the file size threshold you want for a photo to be counted as small


set dialogResult to display dialog ¬

"Enter the file size threshold for small photos or videos in kB : " buttons {"Cancel", "OK"} ¬


default answer (defaultFileSizeThreshold as text)

set FileSizeThresholdkB to (text returned of dialogResult) as integer-- file size in kB

set FileSizeThreshold to FileSizeThresholdkB * 1024 -- file size in Byte



set smallAlbumName to "smallerThan" & FileSizeThresholdkB-- the album to collect the small photos


set largeAlbumName to "largerThan" & FileSizeThresholdkB-- the album to collect the larger photos


tell application "Photos"


activate


-- Ensure that the albums do exist

try

set imageSel to (get selection)

on error errTexttwonumbererrNumtwo

display dialog "Cannot get the selection: " & errNumtwo & return & errTexttwo

end try


-- return the size of the first item of imageSel


try

if not (exists container smallAlbumName) then


makenewalbumnamedsmallAlbumName

end if

set theSmallAlbum to containersmallAlbumName


if not (exists container largeAlbumName) then


makenewalbumnamedlargeAlbumName

end if

set theLargeAlbum to containerlargeAlbumName


if not (exists container "SkippedPhotos") then

make new album named "SkippedPhotos"

end if

set theSkippedAlbum to container "SkippedPhotos"



on error errTexttwonumbererrNumtwo

display dialog "Cannot open albums: " & errNumtwo & return & errTexttwo

end try



-- process the selected photos from the All Photos album


set smallPhotos to {} -- the list of small photos

set largePhotos to {} -- the list of larger photos

set skippedPhotos to {} -- the list of skipped photos




-- check, if the album or the selected photos do contain images


if imageSel is {} then

error "Please select some images."

else

repeat with im in imageSel

try


tell im--get the file size

set s to its size

end tell

on error errText number errNum

display dialog "Error: " & errNum & return & errText & "Trying again"

try

delay 2

tell im

set s to its size


end tell

on error errTexttwonumbererrNumtwo

display dialog "Skipping image due to repeated error: " & errNumtwo & return & errTexttwo

end try


end try

set noSize to (s is missing value)

if noSize then

set skippedPhotos to {im} & skippedPhotos

else

if (sFileSizeThreshold) then

set smallPhotos to {im} & smallPhotos

else

set largePhotos to {im} & largePhotos


end if

end if


end repeat



addsmallPhotostotheSmallAlbum


addlargePhotostotheLargeAlbum


addskippedPhotostotheSkippedAlbum


return "small photos: " & (length of smallPhotos) & ", larger photos : " & (length of largePhotos) & ", skipped: " & (length of skippedPhotos)


end if


end tell


This user tip was generated from the following discussion: How do I sort videos by length of time or by file size?

Comments

Jan 14, 2019 4:07 PM

This does not work

(Copy/Paste)ing the script given above only produces errors due to additional line breaks and missing spaces between words.


Old Toad's compiled version does this:

Jan 14, 2019 4:07 PM

Jan 19, 2019 11:28 AM

The transition of the ASC forums to a new platform messed up the code of the scripts we posted.

I tried to post a corrected version, but the forum software does not allow this - I get the error "Reduce the length of your post below 5000 characters.


Jan 19, 2019 11:28 AM

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.