Skip to content

Instantly share code, notes, and snippets.

View zkarj735's full-sized avatar

Allister Jenks zkarj735

View GitHub Profile
@zkarj735
zkarj735 / gist:faf79e03c12963ecf8e8b19aac49c6a2
Created June 29, 2024 08:46
SwiftBar plugin to show Apple "Magic" peripheral battery levels
#!/bin/zsh
tmp=$(ioreg -c 'AppleDeviceManagementHIDEventService' | grep 'BatteryPercent\|\"Magic' | sed -e 's/[ |]*//' -e 's/\"//g' -e 's/\Product = //' -e 's/BatteryPercent //' | paste -d\ - - | sed 's/Magic \(.\).* = /\1/')
arr=(${(f)tmp})
count=1
for d in "${arr[@]}"; do
# Extract type and percentage
type="${d[1,1]}"
pct="${d[2,3]}"
# Define base symbol based on type
@zkarj735
zkarj735 / disks.5s.sh
Last active June 29, 2024 04:51
A SwiftBar plugin to show how many external disks are connected
#!/bin/zsh
# Get a count of the number of external disks
count=$(df -Hl | grep '^/dev.*\s/Volumes' | wc -l | tr -d '[:space:]')
# Assuming less than 10, turn the number into an SF symbol, e.g. "5.square"
strip=$(echo -n "$count.square")
# Generate a list of the volume names to include in the menu for info
vols=$(df -Hl | grep '^/dev.*\s/Volumes' | sed -e 's|^/dev/.*/Volumes/||' | sort)
# Output
echo ":$strip: | sfcolor=#ffffff sfsize=18\n---\n$vols"
@zkarj735
zkarj735 / upsert.sql
Created May 29, 2024 08:28
A technique to "update or insert" a single row in a DB2 table.
-- DB2 has a MERGE statement which is designed to merge the rows from one table into another.
-- This technique adapts that to the case of a single row of literal values (such as might be provided in an application program).
-- This is informally known as an 'upsert' - a portmanteau of "update" and "insert".
-- The key parts of this statement are:
-- 1. The USING clause provides the values for the entire row, and also provides column names for these.
-- 2. The ON clause is the means for detecting an existing record (like a join's ON clause).
-- 3. The WHEN MATCHED clause specifies which columns to update in an existing record. Obviously this won't contain the keys, but needn't contain all the other columns either.
-- 4. The INSERT VALUES clause uses the column names provided in the USING clause.
@zkarj735
zkarj735 / split_tiff.sh
Created December 1, 2022 23:00
Split a multi-page TIFF file into individual files
#!/bin/zsh
# REQUIRES: ImageMagick
# Splits out the individual images using the original filename plus a suffix
convert multi-page.tiff -set filename:f "%[t]_%[fx:t+1]" +adjoin "%[filename:f].tif"
@zkarj735
zkarj735 / label_pixel_shift.sh
Last active December 13, 2022 05:47
Find Pentax Pixel Shift images and label them
#!/bin/zsh
# REQUIRES: ExifTool
# Finds Pentax "Pixel Shift" RAW files in the current directory (and below) and adds an XMP label.
# This enables them to be spotted in RAW processing software that can't handle them.
# The -r flag recurses down through directories, and the . starts at the current directory. Change as desired.
# Change the label 'Purple' as desired.
exiftool -if 'Pentax:$quality =~ /shift/i' -XMP-xmp:Label=Purple -r .
@zkarj735
zkarj735 / eject_drives.sh
Last active December 11, 2022 02:12
macOS shell scripts to list "ejectable" drives and to eject them all
#!/bin/zsh
# Ejects "ejectable" drives. Use list_drives.sh to validate this will correctly identify and eject the drives you have.
df | egrep df | egrep '^(/dev/disk|exfat).+\s/Volumes' | sed -E 's|.+/(.+)|\1|' | sort | tr '\n' ' ' | sed -E 's|(.+) |💿 \1\n|' | sed -E 's|.+/(.+)|\1|' | xargs -n 1 diskutil eject