#!/bin/bash
FILE_TO_WATCH="/tmp/trigger_file.txt"
FLASH_IMAGE="$HOME/alfred-e-neuman.jpeg"
flash_screen() {
# Convert the image path to absolute path
local abs_flash_image
abs_flash_image=$(realpath "$FLASH_IMAGE" 2>/dev/null)
# Create a temporary Swift script for fullscreen flash
local temp_script="/tmp/flash_screen.swift"
if [[ -f "$abs_flash_image" ]]; then
# Flash with image
cat > "$temp_script" << 'SWIFT_EOF'
import Cocoa
let app = NSApplication.shared
let window = NSWindow(
contentRect: NSScreen.main!.frame,
styleMask: [.borderless],
backing: .buffered,
defer: false
)
window.backgroundColor = NSColor.white
window.level = NSWindow.Level(rawValue: Int(CGWindowLevelForKey(.maximumWindow)))
window.isOpaque = true
window.ignoresMouseEvents = true
if let imagePath = CommandLine.arguments.count > 1 ? CommandLine.arguments[1] : nil,
let image = NSImage(contentsOfFile: imagePath) {
let imageView = NSImageView(frame: window.contentView!.bounds)
imageView.image = image
imageView.imageScaling = .scaleProportionallyUpOrDown
window.contentView?.addSubview(imageView)
}
window.makeKeyAndOrderFront(nil)
DispatchQueue.main.asyncAfter(deadline: .now() + 0.2) {
window.close()
app.terminate(nil)
}
app.run()
SWIFT_EOF
swift "$temp_script" "$abs_flash_image" > /dev/null 2>&1 &
else
# Flash with white color
cat > "$temp_script" << 'SWIFT_EOF'
import Cocoa
let app = NSApplication.shared
let window = NSWindow(
contentRect: NSScreen.main!.frame,
styleMask: [.borderless],
backing: .buffered,
defer: false
)
window.backgroundColor = NSColor.white
window.level = NSWindow.Level(rawValue: Int(CGWindowLevelForKey(.maximumWindow)))
window.isOpaque = true
window.ignoresMouseEvents = true
window.makeKeyAndOrderFront(nil)
DispatchQueue.main.asyncAfter(deadline: .now() + 0.2) {
window.close()
app.terminate(nil)
}
app.run()
SWIFT_EOF
swift "$temp_script" > /dev/null 2>&1 &
fi
# Clean up after a moment
sleep 0.5
rm -f "$temp_script"
}
# Create the directory for the file if it doesn't exist
mkdir -p "$(dirname "$FILE_TO_WATCH")"
# Create the file if it doesn't exist (fswatch needs it to exist)
touch "$FILE_TO_WATCH"
echo "Watching for changes to: $FILE_TO_WATCH"
echo "Flash image: $FLASH_IMAGE"
echo "Press Ctrl+C to stop..."
# Use fswatch with event flags to get detailed event information
fswatch -l 0.01 --event-flags "$(dirname "$FILE_TO_WATCH")" | while read event_line
do
# Only process events for our specific file
if [[ "$event_line" == *"$(basename "$FILE_TO_WATCH")"* ]]; then
echo "$(date '+%Y-%m-%d %H:%M:%S') - Raw event flags: $event_line"
# Check for creation events (Created, ItemCreated)
if [[ "$event_line" == *"Created"* ]]; then
echo "FILE CREATED - Flashing screen..."
flash_screen
# Check for deletion events (Removed, ItemRemoved)
elif [[ "$event_line" == *"Removed"* ]]; then
echo "FILE DELETED - Flashing screen..."
flash_screen
# Check for modification events (Updated, Modified, Changed)
elif [[ "$event_line" == *"Updated"* || "$event_line" == *"Modified"* || "$event_line" == *"Changed"* ]]; then
echo "FILE MODIFIED - Not flashing (uncomment to enable)"
# flash_screen
else
echo "UNKNOWN EVENT TYPE - Event flags: '$event_line'"
echo "Debug: Looking for file '$(basename "$FILE_TO_WATCH")' in event"
# Flash anyway for testing
flash_screen
fi
else
# Debug: show all events to understand what we're getting
echo "$(date '+%Y-%m-%d %H:%M:%S') - Other file event: $event_line"
fi
done