Evaluating Image Dimensions

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

Moderator: Mr_Noodle

Evaluating Image Dimensions Mon Feb 06, 2017 4:51 pm • by jlllllll
I'm attempting to have a rule that will evaluate a folder of images and determine if the image is mobile or desktop wallpaper. I'm using the sips utility to extract the height and width of the image then just doing a simple comparison. The last bit of the rule just applies a tag to the file - in this case it applies a "Mobile WP" tag. I have a similar rule for "Desktop WP" that basically does the same thing except if height/width is switched.

Image

The script runs fine when ran from the command-line with the image file set as the argument. When forcing the rules to run from Hazel, nothing appears to happen, but upon checking the Console there do appear to be errors.

Image



Code: Select all
fn=$1

pixh=$(sips -g pixelHeight $fn)
pixw=$(sips -g pixelWidth $fn)

declare -i pixhval=$(echo ${pixh} | awk -F: ' { print $2 }')
declare -i pixwval=$(echo ${pixw} | awk -F: ' { print $2 }')

if [ $pixhval -gt $pixwval ]; then echo 'mobile'; exit 0; else echo 'desktop'; exit 1; fi


Any assistance would be much appreciated.
jlllllll
 
Posts: 2
Joined: Mon Feb 06, 2017 3:38 pm

Re: Evaluating Image Dimensions Tue Feb 07, 2017 11:52 am • by Mr_Noodle
If you turn on debug mode as described here, you can see output of the script: https://www.noodlesoft.com/kb/hazel-debug-mode/

Also, keep in mind that scripts executing in Hazel are in a different environment. As a result, you can't rely on certain environment variables being set in the same way as in Terminal. For instance, $PATH may not be the same so it's usually best to use full paths to access any programs you call.
Mr_Noodle
Site Admin
 
Posts: 11872
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

Re: Evaluating Image Dimensions Mon Feb 13, 2017 1:24 pm • by jlllllll
Okay, I was able to revisit this and after evaluating the logs and you mentioning that Hazel was running in a different environment I was able to figure out what was causing the issue. It appeared that the sips utility wasn't able to process the file path because of the spaces throughout the path. Hazel was passing the file with the path and sips wasn't able to find the file. So this was a pretty easy fix of quoting the file path being passed to sips.

Image

Code: Select all
fn=$1

pixh=$(sips -g pixelHeight "$fn")
pixw=$(sips -g pixelWidth "$fn")

declare -i pixhval=$(echo ${pixh} | awk -F: ' { print $2 }')
declare -i pixwval=$(echo ${pixw} | awk -F: ' { print $2 }')

if [ $pixhval -gt $pixwval ]; then echo 'mobile'; exit 0; else echo 'desktop'; exit 1; fi
jlllllll
 
Posts: 2
Joined: Mon Feb 06, 2017 3:38 pm


Return to Support