Hazel Script to Sort Square Images

Get help. Get answers. Let others lend you a hand.

Moderator: Mr_Noodle

Hazel Script to Sort Square Images Thu Dec 13, 2012 11:57 am • by malx
Hi, Everyone.

I am new to Scripting but I am trying to write a Hazel script that will compare an image height, and an image width, and if they match (because the image is square) I'd like to return true so I can perform actions. This is my attempt to sort out any Instagram photos I have pushed to my computer.



Was working with something like this:


set imageFile to file theFile

tell application "Image Events"

launch

set my_image to open imageFile

set my_dimensions to dimensions of my_image

close my_image

set my_x to item 1 of my_dimensions as integer

set my_y to item 2 of my_dimensions as integer

if my_x is my_y then true

end tell





But this may be complete wrong.



Let me know if someone can help.
-Joey
malx
 
Posts: 3
Joined: Thu Dec 13, 2012 11:55 am

Re: Hazel Script to Sort Square Images Thu Dec 13, 2012 2:14 pm • by Mr_Noodle
I feel like someone posted about this before. I suggest searching the forums.

Otherwise, try debugging your script in AppleScript Editor and seeing if it works there.
Mr_Noodle
Site Admin
 
Posts: 11195
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

Re: Hazel Script to Sort Square Images Thu Dec 13, 2012 4:42 pm • by malx
I've searched "Square Images" and "Instagram" and didn't find anything. What do you recommend searching?
malx
 
Posts: 3
Joined: Thu Dec 13, 2012 11:55 am

Re: Hazel Script to Sort Square Images Thu Dec 13, 2012 5:22 pm • by a_freyer
If you're just interested in instagram photos, all the ones that I have are 612 high and 612 wide. Performing actions based on those two criteria is easy:

Code: Select all
if (all) of the following conditions are met for (the file or folder being matched)
    kind is jpg
    Pixel Height is 612
    Pixel Width is 612

Do the following to the matched file or folder:
    ....



If you want to test for all square images, then run this script as an embedded shell script passing condition:

Code: Select all
if [ "$(mdls -name kMDItemPixelWidth "$1" | awk -F " = " '{print $2}')" -eq "$(mdls -name kMDItemPixelHeight "$1" | awk -F " = " '{print $2}')" ]; then exit 0;fi

exit 1


Code: Select all
if (all) of the following conditions are met for (the file or folder being matched)
    passes shell script (embedded script)

Do the following to the matched file or folder:
    ....



This is much cleaner than mucking about with AppleScript.
a_freyer
 
Posts: 631
Joined: Tue Sep 30, 2008 9:21 am
Location: Colorado

Re: Hazel Script to Sort Square Images Thu Dec 13, 2012 8:27 pm • by malx
That is a bit over my head, however, your instructions were perfect, and I got exactly what I needed. Not all of the Instagram photos I store are 612x612, but since the only square images I take from my phone that end up in PhotoStream are Instagrams, this is perfect!

Thanks so much!
malx
 
Posts: 3
Joined: Thu Dec 13, 2012 11:55 am

Re: Hazel Script to Sort Square Images Thu Dec 13, 2012 8:30 pm • by a_freyer
Excellent! Great to hear!
a_freyer
 
Posts: 631
Joined: Tue Sep 30, 2008 9:21 am
Location: Colorado

Re: Hazel Script to Sort Square Images Wed Feb 19, 2014 2:32 pm • by Zettt
Thanks for all the code in this thread, it was a great help for my workflow. I wanted something similar, but the problem for me was that the images I have are sometimes not 100% square. One app I use crops the images by -1 pixel on one side. (bug reported)

However, this is my solution. I'm not a huge fan of too complicated scripts either.

Code: Select all
(Passes shell script)


Code: Select all
#!/bin/sh

width=$(mdls -name kMDItemPixelWidth "$1" | awk -F " = " '{print $2}')
height=$(mdls -name kMDItemPixelHeight "$1" | awk -F " = " '{print $2}')

underwidth=$(echo $width-10 | bc)
overwidth=$(echo $width+10 | bc)

#echo $height
#echo $width
#echo $underwidth
#echo $overwidth

if [[ $height -eq $width ]]; then
   echo "square"
   exit 0
elif [[ $height -gt $underwidth && $height -lt $overwidth ]]; then
   echo "somewhat square"
   exit 0
else
   echo "not square at all"
   exit 1
fi


You can remove the echo's. They are unnecessary fluff. I left them in in case I need to debug this in the future. Also note that I'm checking for ±10 pixels.

Enjoy!

EDIT: This script failed sometimes, I updated the code and I don't know why it was not working. I keep my eyes peeled, but it should be working now.
Zettt
Zettt
 
Posts: 30
Joined: Wed Aug 29, 2007 8:50 am


Return to Support