Get file size to use in embedded script?

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

Moderator: Mr_Noodle

Get file size to use in embedded script? Mon Mar 09, 2020 7:51 am • by mikejandreau
I've got a pretty simple webhook script that I use to post to a Slack channel when an event triggered by Hazel starts and another when it ends.

Is there a way to get the file size of the file triggering the Hazel rule that I can use in my embedded script? (The script is running Handbrake on the file, so I'd like to see its size before and after in Slack, so I know how much size I saved).
mikejandreau
 
Posts: 36
Joined: Sun Sep 09, 2018 8:28 am

Re: Get file size to use in embedded script? Mon Mar 09, 2020 10:51 am • by Mr_Noodle
There is the "Size" attribute. I'm not exactly sure how you are communicating with Slack though so can't say much more than that.
Mr_Noodle
Site Admin
 
Posts: 11235
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

The Slack script is just a webhook, like this:

Code: Select all
LONG_FILENAME="$1"
curl -X POST --data-urlencode "payload={\"channel\": \"#handbrake-processing\", \"username\": \"Handbrake Bot\", \"text\": \"*$(basename $LONG_FILENAME)* has started processing.\", \"icon_emoji\": \":arrow_forward:\"}" https://hooks.slack.com/services/XXXX/XXXX/YYYY


Since I'm using the embedded script, I don't see an option to use the "Size" attribute. Is there some method like the LONG_FILENAME="$1" bit I'm using? (I found that on the forum last week.)
mikejandreau
 
Posts: 36
Joined: Sun Sep 09, 2018 8:28 am

Re: Get file size to use in embedded script? Tue Mar 10, 2020 11:03 am • by Mr_Noodle
If you're using a shellscript you can't pass any info from Hazel to it besides the file path. Instead, you can use a command in the shellscript to get the size. Probably some combination of "ls" and "awk" can do that.
Mr_Noodle
Site Admin
 
Posts: 11235
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

What's the right syntax to have it run the ls command?

I tried
Code: Select all
FILESIZE=`ls -l --block-size=M`
and it's not outputting anything correctly.

I tried regular quotes, no quotes, and single quotes. None gave me any output.
mikejandreau
 
Posts: 36
Joined: Sun Sep 09, 2018 8:28 am

Re: Get file size to use in embedded script? Thu Mar 12, 2020 11:32 am • by Mr_Noodle
You can do "man ls" on the commandline to see all the options. Also note that you need to include "$1" as that is the argument which contains the path to the file that Hazel sends in to your script.
Mr_Noodle
Site Admin
 
Posts: 11235
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

My command in Terminal works perfectly. I just don't know how to make it work in Hazel.

This is what I'm doing now:

Code: Select all
LONG_FILENAME="$1"
FILESIZE=`ls -lh *.mp4 $1`
curl -X POST --data-urlencode "payload={\"channel\": \"#handbrake-processing\", \"username\": \"Handbrake Bot\", \"text\": \"*$(basename $LONG_FILENAME)* has started processing. It started as $(basename $FILESIZE)\", \"icon_emoji\": \":arrow_forward:\"}" https://hooks.slack.com/services/XXX/YYY/ZZZ


In terminal I get the right size for my .mp4 (which is what I'm targeting). But in Hazel, it's running on the whole contents of the directory, which makes me think the
Code: Select all
FILESIZE=`ls -lh *.mp4 $1`
is wrong.

How do I pass that into Hazel? Quotes? Double Quotes? Triple Quotes? No quotes? That's what I can't find any documentation on.
mikejandreau
 
Posts: 36
Joined: Sun Sep 09, 2018 8:28 am

Re: Get file size to use in embedded script? Fri Mar 13, 2020 11:21 am • by Mr_Noodle
You should probably use double quotes, or use the LONG_FILENAME variable which you already have set.
Mr_Noodle
Site Admin
 
Posts: 11235
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City


Return to Support

cron