Launchd wildcard characters

Oct 06, '10 07:30:00AM

Contributed by: tedw

One of the occasional irritations of working with launchd is that it doesn't naturally expand shell wildcard characters (~,*,?,...). This means that full paths have to be spelled out for all files: an annoyance at best, and an obstacle when commands need to select specific groups of files or do work across different home folders. For example, a command to clean certain files from a user folder when any user logs in, which is simple enough to write in the shell -- rm -f ~/Folder/*.xxx -- fails when written into a launchd plist.

However, launchd can expand shell characters, but expansion is disabled by default (assumedly for performance reasons). To enable expansion, add an EnableGlobbing key to the plist and set its value to true. The plist for the above shell command, then, would be:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
 <key>RunAtLoad</key>
 <true/>
 <key>EnableGlobbing</key>
 <true/>
 <key>Label</key>
 <string>user.files.rm.globbed</string>
 <key>ProgramArguments</key>
 <array>
  <string>rm</string>
  <string>-f</string>
  <string>~/Folder/*.xxx</string>
 </array>
</dict>
</plist>
Which should work as expected.

Unfortunately, expansion is only enabled for strings in the Program or ProgramArguments keys, not for other keys (such as the WatchPaths, QueueDirectory, or WorkingDirectory keys), so this can't be used to set up wildcard triggers.

[crarko adds: I haven't tested this one.]

Comments (4)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20101005132236801