118: Ask the Automators

1 Like

Re. the question about filing bracketed sets of images, I wouldn’t jump straight in on Keyboard Maestro personally.

Shell scripting was created back in the days when operating systems were more focused on being disk operating systems, and managing files. I might well use Keyboard Maestro to trigger a shell script (irt tends to be my default launcher for scripts), but generally I only switch to Keyboard Maestro as the engine when I need some user interface interaction too. There’s also these forums to call upon as well as Keyboard Maestro. Unless you specifically have a Keyboard Maestro focused request (which I would argue this was not), then I would propose this forum could be an even better starting point for the query.

Back to the automation; by opting for a shell script, I can tie it to Keyboard Maestro, use it in an Apple Shortcut or Automator (even accessing it as a service/quick action in Finder), Alfred’s file management options, maybe pop it into FastScripts, or, of course, use it from the terminal, as well as tie it to a myriad of other tools and options.

All that being said, there are almost always multiple options to choose from and the “right” tool is the one you can use and maintain. So Keyboard Maestro (and I am an avid user of Keyboard Maestro) may well be an ideal option for many people. But, getting a bit of a shell scripting in your arsenal tends to give you many more options down the road, particularly when it comes to moving files around.

To that end, here’s a shell script that I think would do what Kirk was asking for that could be dropped into any number of trigger scenarios/apps. I’ve tried to use clear variable names and numerous comments to explain what it is doing.

#!/bin/zsh

#-----------------------------------------------------------------------------
# Run this script in the directory containing the image files.
# The files will be batched and moved into folders within that directory.
# Folders that do not exist will be created.
#-----------------------------------------------------------------------------

# Settings
intBatchSize=5
strFolderNamePrefix="Folder_"

# Initialise
intCounter=1
intFolder=1
intFileCount=$(find . -maxdepth 1 -type f -not -name ".*" | wc -l | xargs)

if [[ $intFileCount -eq 0 ]] then
	echo "No files to process in $(pwd)."
else
	# Process files in current directory
	for file in *.*(Om);
	do
		# Generate folder name - padding the folder number and appending it to "Folder_"
		strFolder="$strFolderNamePrefix${(l:3::0:)intFolder}"
		
		# Make the new folder
		if [[ $intCounter -eq 1 ]] then
			mkdir -p "$strFolder"
		fi
		
		# Move the file to the new folder
		mv "$file" "$strFolder"
		
		## Increment the file counter
		((intFileCounter++))
		
		# Carry out increment/reset on file counter based on batch size
		if [[ $intCounter -lt $intBatchSize ]] then
			((intCounter++))
		else
			# Reset file counter and increment folder number
			intCounter=1
			((intFolder++))
		fi
	done
	
	echo "$intFileCount files processed from $(pwd)."
fi

If you always want to run it on the same directory, add a change directory command before the # Settings. For example, for my testing I included this line in the script to ensure it was working on my test folder of images.

cd /Users/stephen/scripts/zsh/pics

From my testing it seems to work smoothly, taking files in chronological order, and dealing with existing directories, making directories, noting an empty (of files) starting directory, etc.

I set it to process all files as it isn’t known what format the camera images are being saved as (JPG, JPEG, RAW, etc.), but that could be refined if a common file extension is used and other files need in the same directory need to be filtered out.

There may be some edge cases it doesn’t deal with, but I think it should be a good starting point.

@RosemaryOrchard
The Smart Energy Monitoring Plug segment was fascinating at the show’s end. I work from home with a significant amount of tech gear, and my electricity usage has steadily increased over the years. When you said you had found some good energy monitoring plugs that don’t send all the data straight to their mothership, I grabbed my notebook to write down the recommendation, but you never named the product you settled on. Can you share that?

Also: I’m the Robert who submitted the question about replacing the Harmony remote with some automation/smart home setup. Thanks for the Home Assistant advice. I suspected you might push me in that direction, but having never used HA, it’s nice to get confirmation before I jump in. The effort to configure HA for my usage does not look insignificant. I’d hate to do all the work only to find it isn’t really up to the task.

@RosemaryOrchard you mentioned SwitchBot actions in Shortcuts. I looked in the Shortcuts app on both my Mac and iPhone and while the SwitchBot app did show up, there were no actions. I am sad.

So how do I find these actions? Thanks.

I just use the SwitchBot API as I have never managed to get the actions to show in Shortcuts eitheer.

Here is a link to the documentation for the API as well as some Shortcut templates to get you started :slight_smile:

SwitchBot API Documentation

Get SwitchBot Devices

Send Command to SwitchBot Device

1 Like

I don’t have a SwitchBot, but I downloaded the iOS app to see if I could get any actions to show up.

I couldn’t register any devices, but I did set up a dummy scene that triggers a notification on manual execution.

I checked the Shortcuts app and the Switchbot app was there, but had no actions.

Under Cloud Services, I could see Siri Shortcuts was listed.

I selected that, and tapped the big red “Execute” button, and recorded a trigger phrase.

Now if I return to Shortcuts, I get the scene listed as an available action.

I remember a few early Shortcuts supporting apps having somewhat similar registration mechanisms.

I can then add the action to my shortcut.

When I run the shortcut, I receive my notification, so it seems to work.

Hope that helps.

3 Likes

@AidanMJ25 @sylumer

Thank you both. Playing with the API has been on my todo list for a while, I just need to find the time.

I should have the time to play with the API and Shortcuts in the near future, as plans have changed and it looks as if I’ll have a free weekend. (I can’t wait until I’m retired! I so prefer not working …). Thank you again for the helpful info and pointers.

1 Like