0

Hence, in a certain folder, say ~/Desktop/foo,

any time a file/subfile/folder is touched (added, saved),

I want to just run a certain command in the shell.

Is there a way to do this, either with a zsh command, or perhaps some other automation on the Mac, or an app?? TY

Fattie
  • 178
  • 1
  • 5
  • 25

2 Answers2

1

Have a look into macOS folder actions. This enables AppleScript and Automator actions to be setup to monitor a specific folder and act on changes.

The simplest way to run a shell script, is to use Automator with a Run Shell Script action. On startup, Automator will ask the type, Folder Action is the one to choose.

Some pointers: https://developer.apple.com/library/archive/documentation/LanguagesUtilities/Conceptual/MacAutomationScriptingGuide/WatchFolders.html

http://www.macosxautomation.com/automator/folder-action/index.html

Be sure to checkout many other posts here on the subject also.

1

You can do this using Folder Actions:

Ctrl-click any folder in the Finder and choose Services>Folder Actions Setup in Monterey. In early versions of macOS, the option appears in the main lists of contextual items.

To avoid the malicious and accidental installation of Folder Actions, macOS requires a confirmation step that can’t be automated: click Run Service in the Confirm Service dialog.

This adds the current folder to the Folders with Actions list without adding actions. The Folder Actions Setup window appears with a dialog already open that reads Choose a Script to Attach.

This list includes ones that Apple seeded, locating them inside the /Library/Scripts/Folder Actions Scripts folder. Any scripts installed by apps or that you’ve created and placed inside your Home directory’s ~/Library/Scripts/Folder Actions Scripts folder appear as well.

You can create Folder Actions in a numbers of ways:

  • Use an item Apple provides in the dialog that appears when you invoke Folder Actions Setup. These mostly relate to image processing.

  • Create a Folder Action in Automator using its broader set of tools. This can include passing information to an AppleScript, JavaScript, or shell script.

  • Write your own AppleScript to perform a task.