Skip to main content
added 197 characters in body
Source Link
Lri
  • 106.4k
  • 19
  • 230
  • 283

Using launchd, you could saveSave a property list like this inas ~/Library/LaunchAgents/test.plist.:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC -//Apple Computer//DTD PLIST 1.0//EN
http://www.apple.com/DTDs/PropertyList-1.0.dtd>
<plist version="1.0">
<dict>
    <key>Label</key>
    <string>com.stackexchange.apple.40705<<string>test</string>
    <key>ProgramArguments</key>
    <array>
        <string>say</string>
        <string>yy</string>
    </array>
    <key>WatchPaths</key>
    <array>
        <string>/Users/username<string>~/Desktop/</string>
    </array>
</dict>
</plist>

The property list is loaded automatically after logging out and back in, but youagent can load it immediatelybe loaded with launchctl load ~/Library/LaunchAgents/com.stackexchange.apple.40705test.plist or by logging out and back in. Applying changes requires unloading and loading the property listplist.

Programs can only be run at most every ten seconds. You can increaseTilde expansion (but not decrease~/) the time limitworks in WatchPaths by addingdefault. EnableGlobbing adds wildcard and tilde expansion for ProgramArguments, but it doesn't affect Program or WatchPaths.

If a watched file is modified within ten seconds from the last invocation, a message like this is added to system.log:

com.apple.launchd.peruser.501[146]: (test) Throttling respawn: Will start in 7 seconds

One way to get rid of the messages is to add something like ThrottleIntervalsleep 10 keyto the end of scripts. Setting ThrottleInterval to 10 doesn't help.

Changes in subfolders of watched folders aren't detected.

See man launchd and man launchd.plist for more information.

Using launchd, you could save a property list like this in ~/Library/LaunchAgents/.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC -//Apple Computer//DTD PLIST 1.0//EN
http://www.apple.com/DTDs/PropertyList-1.0.dtd>
<plist version="1.0">
<dict>
    <key>Label</key>
    <string>com.stackexchange.apple.40705</string>
    <key>ProgramArguments</key>
    <array>
        <string>say</string>
        <string>yy</string>
    </array>
    <key>WatchPaths</key>
    <array>
        <string>/Users/username/Desktop/</string>
    </array>
</dict>
</plist>

The property list is loaded automatically after logging out and back in, but you can load it immediately with launchctl load ~/Library/LaunchAgents/com.stackexchange.apple.40705.plist. Applying changes requires unloading and loading the property list.

Programs can only be run at most every ten seconds. You can increase (but not decrease) the time limit by adding a ThrottleInterval key.

See man launchd and man launchd.plist for more information.

Save a property list like this as ~/Library/LaunchAgents/test.plist:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC -//Apple Computer//DTD PLIST 1.0//EN
http://www.apple.com/DTDs/PropertyList-1.0.dtd>
<plist version="1.0">
<dict>
    <key>Label</key>
    <string>test</string>
    <key>ProgramArguments</key>
    <array>
        <string>say</string>
        <string>yy</string>
    </array>
    <key>WatchPaths</key>
    <array>
        <string>~/Desktop/</string>
    </array>
</dict>
</plist>

The agent can be loaded with launchctl load ~/Library/LaunchAgents/test.plist or by logging out and back in. Applying changes requires unloading and loading the plist.

Tilde expansion (~/) works in WatchPaths by default. EnableGlobbing adds wildcard and tilde expansion for ProgramArguments, but it doesn't affect Program or WatchPaths.

If a watched file is modified within ten seconds from the last invocation, a message like this is added to system.log:

com.apple.launchd.peruser.501[146]: (test) Throttling respawn: Will start in 7 seconds

One way to get rid of the messages is to add something like sleep 10 to the end of scripts. Setting ThrottleInterval to 10 doesn't help.

Changes in subfolders of watched folders aren't detected.

See man launchd and man launchd.plist for more information.

deleted 39 characters in body
Source Link
Lri
  • 106.4k
  • 19
  • 230
  • 283

Using launchd, you could save a property list like this in ~/Library/LaunchAgents/.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC -//Apple Computer//DTD PLIST 1.0//EN
http://www.apple.com/DTDs/PropertyList-1.0.dtd>
<plist version="1.0">
<dict>
    <key>Label</key>
    <string>com.stackexchange.apple.40705</string>
    <key>ProgramArguments</key>
    <array>
        <string>say</string>
        <string>yy</string>
    </array>
    <key>WatchPaths</key>
    <array>
        <string>/Users/username/Desktop/</string> <!-- ~/ doesn't work -->
    </array>
    <key>ThrotteInterval</key>
    <integer>10</integer> <!-- run at most every 10 seconds (default) -->
</dict>
</plist>

It can beThe property list is loaded automatically after logging out and back in, but you can load it immediately with launchctl load ~/Library/LaunchAgents/com.stackexchange.apple.40705.plist or by logging out and back in. Applying changes requires unloading and loading the property list.

launchdPrograms can only registers modifications to files if they are saved atomically, or deleted and recreated every time they are savedbe run at most every ten seconds. Most OS X apps do thatYou can increase (but not decrease) the time limit by default, but for example TextMate and vim don'tadding a ThrottleInterval key.

The keys available on the property lists are documented inSee man launchd and man launchd.plist for more information.

Using launchd, you could save a property list like this in ~/Library/LaunchAgents/.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC -//Apple Computer//DTD PLIST 1.0//EN
http://www.apple.com/DTDs/PropertyList-1.0.dtd>
<plist version="1.0">
<dict>
    <key>Label</key>
    <string>com.stackexchange.apple.40705</string>
    <key>ProgramArguments</key>
    <array>
        <string>say</string>
        <string>yy</string>
    </array>
    <key>WatchPaths</key>
    <array>
        <string>/Users/username/Desktop/</string> <!-- ~/ doesn't work -->
    </array>
    <key>ThrotteInterval</key>
    <integer>10</integer> <!-- run at most every 10 seconds (default) -->
</dict>
</plist>

It can be loaded with launchctl load ~/Library/LaunchAgents/com.stackexchange.apple.40705.plist or by logging out and back in. Applying changes requires unloading and loading the property list.

launchd only registers modifications to files if they are saved atomically, or deleted and recreated every time they are saved. Most OS X apps do that by default, but for example TextMate and vim don't.

The keys available on the property lists are documented in man launchd.plist.

Using launchd, you could save a property list like this in ~/Library/LaunchAgents/.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC -//Apple Computer//DTD PLIST 1.0//EN
http://www.apple.com/DTDs/PropertyList-1.0.dtd>
<plist version="1.0">
<dict>
    <key>Label</key>
    <string>com.stackexchange.apple.40705</string>
    <key>ProgramArguments</key>
    <array>
        <string>say</string>
        <string>yy</string>
    </array>
    <key>WatchPaths</key>
    <array>
        <string>/Users/username/Desktop/</string>
    </array>
</dict>
</plist>

The property list is loaded automatically after logging out and back in, but you can load it immediately with launchctl load ~/Library/LaunchAgents/com.stackexchange.apple.40705.plist. Applying changes requires unloading and loading the property list.

Programs can only be run at most every ten seconds. You can increase (but not decrease) the time limit by adding a ThrottleInterval key.

See man launchd and man launchd.plist for more information.

added 84 characters in body
Source Link
Lri
  • 106.4k
  • 19
  • 230
  • 283

Using launchd, you could save a property list like this asin ~/Library/LaunchAgents/com.stackexchange.apple.40705.plist.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC -//Apple Computer//DTD PLIST 1.0//EN
http://www.apple.com/DTDs/PropertyList-1.0.dtd>
<plist version="1.0">
<dict>
    <key>Label</key>
    <string>com.stackexchange.apple.40705</string>
    <key>ProgramArguments</key>
    <array>
        <string>say</string>
        <string>yy</string>
    </array>
    <key>WatchPaths</key>
    <array>
        <string>/Users/username/Desktop/</string> <!-- cannot use ~/ doesn't work -->
    </array>
    <key>ThrotteInterval</key>
    <integer>10</integer> <!-- run at most every 10 seconds (default) -->
</dict>
</plist>

It can be loaded by logging out and back in or with launchctl load ~/Library/LaunchAgents/com.stackexchange.apple.40705.plist or by logging out and back in. Applying changes requires unloading and loading the property list.

WatchPathslaunchd only registers modifications to files if they are saved atomically. (Or, or deleted and recreated every time they are saved, which is what most. Most OS X apps do.) But that by default, but for example TextMate and vim don't perform atomic saves by default.

The keys available on the property lists are documented in man launchd.plist.

Using launchd, you could save a property list like this as ~/Library/LaunchAgents/com.stackexchange.apple.40705.plist.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC -//Apple Computer//DTD PLIST 1.0//EN
http://www.apple.com/DTDs/PropertyList-1.0.dtd>
<plist version="1.0">
<dict>
    <key>Label</key>
    <string>com.stackexchange.apple.40705</string>
    <key>ProgramArguments</key>
    <array>
        <string>say</string>
        <string>yy</string>
    </array>
    <key>WatchPaths</key>
    <array>
        <string>/Users/username/Desktop/</string> <!-- cannot use ~/ -->
    </array>
    <key>ThrotteInterval</key>
    <integer>10</integer> <!-- run at most every 10 seconds (default) -->
</dict>
</plist>

It can be loaded by logging out and back in or with launchctl load ~/Library/LaunchAgents/com.stackexchange.apple.40705.plist.

WatchPaths only registers modifications to files if they are saved atomically. (Or deleted and recreated every time they are saved, which is what most OS X apps do.) But for example TextMate and vim don't perform atomic saves by default.

Using launchd, you could save a property list like this in ~/Library/LaunchAgents/.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC -//Apple Computer//DTD PLIST 1.0//EN
http://www.apple.com/DTDs/PropertyList-1.0.dtd>
<plist version="1.0">
<dict>
    <key>Label</key>
    <string>com.stackexchange.apple.40705</string>
    <key>ProgramArguments</key>
    <array>
        <string>say</string>
        <string>yy</string>
    </array>
    <key>WatchPaths</key>
    <array>
        <string>/Users/username/Desktop/</string> <!-- ~/ doesn't work -->
    </array>
    <key>ThrotteInterval</key>
    <integer>10</integer> <!-- run at most every 10 seconds (default) -->
</dict>
</plist>

It can be loaded with launchctl load ~/Library/LaunchAgents/com.stackexchange.apple.40705.plist or by logging out and back in. Applying changes requires unloading and loading the property list.

launchd only registers modifications to files if they are saved atomically, or deleted and recreated every time they are saved. Most OS X apps do that by default, but for example TextMate and vim don't.

The keys available on the property lists are documented in man launchd.plist.

Source Link
Lri
  • 106.4k
  • 19
  • 230
  • 283
Loading