Yeah - I know…old system.
Working on a research project for a book and Apple dumbed down newer versions of Pages after Pages '09. So until I finish the project, I won’t update. Learning a new word processor is not an option when I have 3/4 of the way through the project which gets worked on sporadically!
background - once was adept in Unix for the Amiga computer system (Arrex). Yep - its been that long ago I was into scripting. I dropped it after that and am wanting to get back into it.
I want SIPS to resize JPGs into 32X32 JPGs (sprite sized) in a folder of subfolders.
I set up a test/work folder named Paint on my desktop. Hierarchy of the test folders
/Users/Landru_/Desktop/Paint/sub1/sub2/sub3
Within Paint and each sub are 3 JPGs.
# Script resizes all pics it finds in folder (and subfolders)
initial_folder="/Users/Landru_/Desktop/Paint "
all_images=$(find -E $initial_folder -iregex ".*\.(jpg|gif|png|jpeg)")
while read -r image_full_path; do
filename=$(basename "$image_full_path");
source_folder=$(dirname "$image_full_path");
say $filename
sips -z 32 32 $filename
done <$all_images
Been at this since morning and its 5:24 PM!
I have tackled it by making one change at a time to get through errors.
-
It was recognizing the PAINT directory and then quit! I assume something in the above syntax was a mistake I made by hitting a key or something, but cannot figure out what!
-
When the script WAS “seeing” the directory (initially dragged and dropped from the desktop), it was finding the files inside (and subs) but giving invalid file type error messages for each one. They are all JPGs.
-
I guess my problem is not being able to find how to pass a variable (in this case filename) to the SIPS command and have it work?
-
The say command was there just to give feedback.
BTW - I did get a much larger script from online and cut it down to this. Like I said, it was finding everything and just not converting.
So admittedly I have not had time to look up all of the logic of the line containing the find command.
The read -r is something I can look up, but the loop was working fine, so I tacled the other problems.
Help! So I can kick myself for what I missed LOL!
Thanks
Hi. It’s late here, and I don’t really have the time to scrutinize the sample code to see what went wrong in that approach, although my initial suspicion would be quoting issues. An alternate could be:
do shell script "find -E " & my (choose folder with prompt "Locate the SOURCE folder:")'s POSIX path & " -iregex '.*(jpg|jpeg|gif|png)' -exec sh -c " & "sips -z 32 32 \"$0\" --out \"$0\" "'s quoted form & " {} \\;"
Caveat: My code expressly overwrites any file(s) it encounters, so be careful where you point it.
I tested Marc Anthony’s suggestion. It works well and is probably what the OP wants.
FWIW. I’ve included a Finder suggestion below. It will work well if the number of files is small but will be slow or even fail in the number of files is large. It appears that the OP wants existing files to be overwritten, and that’s what this script does. The script should be tested on copies of image files to insure it’s working as desired.
BTW, the OP states that he/she has an old system but doesn’t indicate the exact OS version. It’s easy to test my script to see if it works.
set theFolder to ((path to desktop as text) & "Paint") as alias
set theExtensions to {"jpg", "gif", "png", "jpeg"}
tell application "Finder" to set theImages to (every file of the entire contents of theFolder whose name extension is in theExtensions) as alias list
set imageCount to (count theImages) as text
repeat with aFile in theImages
set the contents of aFile to (quoted form of POSIX path of aFile) & space
end repeat
display alert imageCount & " image files found in " & linefeed & (POSIX path of theFolder) message "This script will resize " & imageCount & " image files and will OVERWRITE the original files. This action cannot be undone." buttons {"Cancel", "Proceed"} default button "Cancel" as warning
if button returned of result = "Cancel" then error number -128
do shell script "sips -z 32 32 " & theImages
It’s not entirely clear if @Lbrewer42 is looking for an AppleScript solution or a solution solely based on Unix shell scripting.
But here’s a recursive AppleScript solution, albeit with SIPS thrown in. (Does Image Events Scripting still work? I struggled with it.)
It will distort the original images if they’re not square, and it will overwrite the originals with the 32x32 versions. Use with caution, test on a copy…
set chosenFolder to choose folder
my scaleTo32(chosenFolder)
on scaleTo32(thisFolder)
tell application "System Events"
set theItems to items of thisFolder
repeat with eachItem in theItems
set eachItem to POSIX path of eachItem
set itemExtension to name extension of item eachItem
if itemExtension is in {"jpg", "gif", "png", "jpeg"} then
do shell script "sips -z 32 32 " & quoted form of eachItem
end if
if class of item eachItem is folder then
my scaleTo32(item eachItem)
end if
end repeat
end tell
end scaleTo32
I want to thank all of you for your help. I have my sprites!
I can see I have a lot of studying to do and will start with the tutorials on the forum.