How to: Mac & AppleScript
Mac automation scripts to automate the Mac UI and settings

`bin/` directory:

- `mac_diff_settings.sh` - takes before and after snapshots of UI setting changes and diffs them to make it easy to find `defaults` keys to add to `setup/mac_settings.sh` to save settings
- `mac_restore_file.sh` - checks all the backup mount points for the latest backup that has a given file and then restores it
- `mac_backup_du_in_progress.sh` - find large files in the currently in-progress Time Machine backup to find out what is taking so long and racking up so many more GB of changes than you expect. This helps discover large but unnecessary files that you might want to exclude using the adjacent script `mac_backup_exclude_paths.sh`
- `mac_backup_exclude_paths.sh` - excludes many common large caches, docker and VM paths from macOS Time Machine backups
- `mac_backup_find_excluded_paths.sh` - does a deep search for macOS Time Machine excluded backup paths on file/folder attributes. See [HariSekhon/Knowledge-Base Mac page](https://github.com/HariSekhon/Knowledge-Base/blob/main/mac.md#time-machine) for why
- `mac_rmdir.sh` - safely delete a directory on Mac only if it is empty of actual data, by first removing macOS hidden metadata files and dirs such as `.fseventsd/`, `.Spotlight-V100/` and `.DS_Store` - straight `rmdir` fails otherwise
- `mac_iso_to_usb.sh` - converts a given ISO file to a USB bootable image and burns it onto a given or detected inserted USB drive
- `mac_ramdisk.sh` - creates a mac ramdisk of given MB size. Useful for performance, or even testing disk write scripts such as `disk_speed_write_*.sh` without wearing out your SSD
- `mac_delete_local_snapshots.sh` - deletes local macOS snapshots to free up disk space. When there is a substantial discrepancy between what the `df -h` command and the Finder UI shows, this is often the cause
- `copy_to_clipboard.sh` - copies stdin or string arg to system clipboard on Linux or Mac
- `paste_from_clipboard.sh` - pastes from system clipboard to stdout on Linux or Mac
- `paste_from_clipboard_upon_changes.sh` - pastes from system clipboard to stdout on Linux or Mac whenever the clipboard changes
- `paste_diff_settings.sh` - Takes snapshots of before and after clipboard changes and diffs them to show config changes

`applescript/` directory:

- `keystrokes.sh` - send N keystroke combinations
- `mouse_clicks.sh` - send N mouse click combinations to sequence of screen coordinates
  - `get_mouse_coordinates.sh` - print the current mouse coordinates - to know what to pass to above script
- `mouse_clicks_remote_desktop.sh` - switches to Microsoft Remote Desktop, waits 10 seconds and then clicks the mouse
  once a minute to prevent the screensaver from coming on. Workaround to Active Directory Group Policies that don't let
  you disable the screensaver. Point your mouse to an area that will have no mouse click effect, the Cmd-Tab to Terminal
  and run this
- `get_frontmost_process_title.scpt` - detect the frontmost window
  - to detect if you should send keystrokes / mouse clicks)
- `set_frontmost_process.scpt` - switch to bring the given app to the foreground to send keystrokes / mouse clicks to it
  - `browser_get_default.scpt` - get the default configured browser in format passable to Applescript (for  above script)
- `is_screen_locked.py` - detect if the screen is locked to stop sending keystrokes or mouse clicks
- `is_screensaver_running.scpt` - detect if the screensaver is running to stop sending keystrokes or mouse clicks
- `reopen_app.sh` - relaunch a given app
  (used to reload Shazam to detect DB changes after removing tracks programmatically from its DB)
- `spotify_app_search.sh` - runs a search in the Spotify App on Mac using Applescript
- `shazam_app_dump_tracks.sh` - dumps `artist - track` one per line from the Shazam local sqlite DB
- `shazam_app_delete_track.sh` - deletes a given `"artist" "track"` from the Shazam local sqlite DB
- `shazam_search_spotify_then_delete_track.sh` - searches for each Shazam'd track in the local Spotify desktop app,
  then prompts to delete each track from the local Shazam DB once you've saved it in Spotify.
  Useful to migrate Shazam'd tracks to Spotify after Apple removed the integration
- `screensaver_activate.scpt` - activate screensaver
- `shorten_text_selection.scpt` - shortens the selected text in the prior window. Replaces `and` with `&` and crushes
  out multiple blank lines. I use this for LinkedIn comments due to the short 1250 character limit
- `start_app_at_login.sh` - adds an app to the Login items to auto-start

Hammerspoon code has been moved to its own repo:

[![Readme Card](https://github-readme-stats.vercel.app/api/pin/?username=HariSekhon&repo=Hammerspoon&theme=ambient_gradient&description_lines_count=3)](https://github.com/HariSekhon/Hammerspoon)

See also [Mac](https://github.com/HariSekhon/Knowledge-Base/blob/main/mac.md) page
in [HariSekhon/Knowledge-Base](https://github.com/HariSekhon/Knowledge-Base).