# Automation on macOS
#typ/Liste #tool/macos
running shell scripts on my laptop without oversight
- [launchd](https://support.apple.com/en-gb/guide/terminal/apdc6c1077b-5d5d-4d35-9c19-60f2397b2369/mac) is native and feature rich, but jobs are annoying to setup
- If the interval was missed, it triggers the job immediately after waking up (if within a reasonable window)
- cron. not a good fit for a laptop that’s not running all the time
- would work with some manual checks, but im not implementing new features just to run a script
- notifications: [terminal-notifier](https://github.com/julienXX/terminal-notifier)
- `terminal-notifier -message "Hello, this is my message" -title "Message Title"`
- more solutions and context: [How do I make a Mac Terminal pop-up/alert? Applescript? – Stackoverflow](https://stackoverflow.com/questions/5588064/how-do-i-make-a-mac-terminal-pop-up-alert-applescript)
- more on terminal-notifier on osxdaily: [Send an Alert to Notification Center from the Command Line in OS X](https://osxdaily.com/2012/08/03/send-an-alert-to-notification-center-from-the-command-line-in-os-x/)
## Configuring launchd
- writting configs manually (pain)
- [official developer docs](https://developer.apple.com/library/archive/documentation/MacOSX/Conceptual/BPSystemStartup/Chapters/CreatingLaunchdJobs.html)
- plist generator: [Launched](https://launched.zerowidth.com) (free, webbased)
- GUI by peter borg: [Lingon](https://www.peterborgapps.com/lingon/) (12\$/year or 24\$ one-time, native)
## Launchd resources
- [JP’s Projects - Automation with launchd](https://jpquast.github.io/posts/2023-04-16-using-launchd/)
- [MacOS ‘launchd’ examples (launchd plist example files)](https://alvinalexander.com/mac-os-x/launchd-examples-launchd-plist-file-examples-mac/)
- [How to Use launchd to Run Services in macOS](https://medium.com/swlh/how-to-use-launchd-to-run-services-in-macos-b972ed1e352)
- fuck medium for their authwall, thank freedium: https://freedium.cfd/https://medium.com/swlh/how-to-use-launchd-to-run-services-in-macos-b972ed1e352
- [GitHub - psobolik/plist-gen: Generate a macOS launchd plist from a yaml file](https://github.com/psobolik/plist-gen?tab=readme-ov-file)
### launchd tips according to Deepseek
beaware, AI slop. some good tips tho.
1. **Path Issues**
- ✅ **Always use full absolute paths** (e.g., `/Users/username/...`).
- ❌ Never use `~`, `$HOME`, or relative paths (e.g., `../scripts/`).
2. **Permissions**
- Ensure the script is executable:
```bash
chmod +x /Users/username/Developer/scripts/backup_zotero.sh
```
- The job runs with **your user privileges** (since it’s in `~/Library/LaunchAgents/`).
3. **Environment Variables**
- `launchd` runs with a **bare-bones environment**.
- If your script needs `$PATH` or other vars, define them explicitly:
```xml
<key>EnvironmentVariables</key>
<dict>
<key>PATH</key>
<string>/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin</string>
</dict>
```
4. **Logging**
- Without `StandardOutPath`/`StandardErrorPath`, output disappears silently.
- Debug with:
```bash
tail -f ~/Logs/zotero_backup.{log,err}
```
5. **Testing**
- Verify the job is loaded:
```bash
launchctl list | grep backup_zotero
```
- Force a manual run:
```bash
launchctl start com.user.backup_zotero
```