Detect Mac Application moved to trash folder using launch Agents - macos

I want to run a script when my application moved into trash folder and I come to know that using launch agents its possible, I googled and made the plist but its not working or not calling the script..
<?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>Label</key>
<string>com.meet.anywhere</string>
<key>WatchPaths</key>
<array>
<string>~/.Trash</string>
</array>
<key>ProgramArguments</key>
<array>
<string>/Users/eclit/Desktop/DeleteScript.scpt</string>
</array>
<key>KeepAlive</key>
<false/>
</dict>
</plist>

launchd cannot run the applescript directly. You need to use the command line tool osascript to run the applescript. As such your ProgramArguments portion should look like the following. Also, there's no need for the KeepAlive section. It does nothing so I'd remove it.
<key>ProgramArguments</key>
<array>
<string>osascript</string>
<string>/Users/eclit/Desktop/DeleteScript.scpt</string>
</array>

<?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>Label</key>
<string>com.meet.anywhere</string>
<key>WatchPaths</key>
<array>
<string>~/.Trash/AppName.app</string>
</array>
<key>ProgramArguments</key>
<array>
<string>osascript</st
<string>/Users/eclit/Desktop/DeleteScript.scpt</string>
</array>
<key>KeepAlive</key>
<false/>
</dict>
</plist>

Related

Launchd is not working

I am having troubles making a Launchd script. It is fairly simple, all it needs to do is launch an application - the location is: /Library/Desktop Pictures/wallpaper
I've loaded the file using terminal, and terminal gives no errors but the script does not execute. I need the script to execute every 1 hour.
The plist file:
<?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>KeepAlive</key>
<true/>
<key>Label</key>
<string>wallpaper.restart</string>
<key>ProgramArguments</key>
<array>
<string>/Library/Desktop Pictures/wallpaper.wallpaperapp.app</string>
</array>
<key>StartCalendarInterval</key>
<dict>
<key>Hour</key>
<integer>1</integer>
</dict>
</dict>
</plist>
Please Help.
Thanks,
Devansh
EDIT:
Below is the next better version of the above code, but still not much luck :(
<?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>KeepAlive</key>
<dict>
<key>AfterInitialDemand</key>
<true/>
</dict>
<key>Label</key>
<string>com.plisterine.backgroundchanger</string>
<key>LowPriorityIO</key>
<false/>
<key>Nice</key>
<integer>0</integer>
<key>ProgramArguments</key>
<array>
<string>/usr/bin/osascript</string>
<string>/Library/Desktop Pictures/wallpaper/wallpaper.scpt</string>
</array>
<key>StartInterval</key>
<integer>3600</integer>
</dict>
</plist>
StartCalendarInterval runs the script at a specific time every day, in your case at 01:00 (1 am)
To run it every hour you have to write
<key>StartInterval</key>
<integer>3600</integer>

How can I avoid using a bash script in my launchd plist?

This is my launchd plist
<?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>KeepAlive</key>
<true/>
<key>Label</key>
<string>com.localhost.hexo</string>
<key>ProgramArguments</key>
<array>
<string>/Users/frankg/dev/bin/start-hexo.sh</string>
</array>
<key>UserName</key>
<string>frankg</string>
<key>RunAtLoad</key>
<true/>
<key>StandardErrorPath</key>
<string>/Users/frankg/hexo</string>
<key>StandardOutPath</key>
<string>/Users/frankg/hexo</string>
</dict>
</plist>
This is my bash script
hexo server --cwd /Users/frankg/dev/code/apps/blog >> /tmp/MyLaunchdTest.out
This is working. How can I avoid the use of a bash script.
Specify the full path to the hexo executable, followed by each argument to it as a separate string in the ProgramArguments array. I'm a bit confused by the various output redirects -- in the current .plist you specify output to /Users/frankg/hexo, but in the script you redirect it to /tmp/MyLaunchdTest.out instead; I assume the latter is what you actually want? AIUI you could also use the WorkingDirectory key to replace the --cwd option. Something like this:
<?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>KeepAlive</key>
<true/>
<key>Label</key>
<string>com.localhost.hexo</string>
<key>ProgramArguments</key>
<array>
<string>/path/to/hexo</string>
<string>server</string>
</array>
<key>UserName</key>
<string>frankg</string>
<key>RunAtLoad</key>
<true/>
<key>StandardErrorPath</key>
<string>/Users/frankg/hexo</string>
<key>StandardOutPath</key>
<string>/tmp/MyLaunchdTest.out</string>
<key>WorkingDirectory</key>
<string>/Users/frankg/dev/code/apps/blog</string>
</dict>
</plist>

Why doesn't this .plist work?

Here she comes...
<?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>Label</key>
<string>Titan Server</string>
<key>ProgramArguments</key>
<array>
<string>/Users/Ian/Titan/bin/titan.sh</string>
<string>start</string>
</array>
<key>RunAtLoad</key>
<true/>
<key>WorkingDirectory</key>
<string>/Users/Ian/Titan</string>
</dict>
</plist>
If I go into terminal and do...
launchctl load "Titan Server.plist"
... it says ...
/Library/LaunchAgents/Titan Server.plist: Operation already in
progress
Huh?
If I navigate to the working directory manually and type the command it works ok.

Creating a LaunchDaemon for Gitblit

I want to create a LaunchDaemon on OSX Mavericks for my Gitblit, this is my file (org.gitblit.plist):
<?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>KeepAlive</key>
<true/>
<key>Label</key>
<string>org.gitblit.plist</string>
<key>ProgramArguments</key>
<array>
<string>/usr/bin/java</string>
<string>--baseFolder</string>
<string>data</string>
<string>-jar</string>
<string>/Applications/gitblit/gitblit.jar</string>
</array>
<key>RunAtLoad</key>
<true/>
</dict>
</plist>
It's not working, but when I try java -jar gitblit.jar --baseFolder data on /Applications/gitblit everything works fine. Am I missing something?
The Gitblit GO distribution comes with a shell script gitblit.sh that will work for this purpose. This is the version of the LaunchAgent plist ~/Library/LaunchAgents/org.gitblit.plist I used.
Note the addition of the WorkingDirectory key, as well as log files in case of errors.
<?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>KeepAlive</key>
<true/>
<key>Label</key>
<string>org.gitblit.plist</string>
<key>ProgramArguments</key>
<array>
<string>/bin/bash</string>
<string>/Applications/gitblit/gitblit.sh</string>
</array>
<key>WorkingDirectory</key>
<string>/Applications/gitblit/</string>
<key>RunAtLoad</key>
<true/>
<key>StandardOutPath</key>
<string>/Applications/gitblit/stdout.txt</string>
<key>StandardErrorPath</key>
<string>/Applications/gitblit/sterr.txt</string>
</dict>
</plist>
So I finally ended up cheating like this:
I saved this script on /opt/scripts/start_gitblit.sh:
#!/bin/bash
cd /Applications/gitblit/
java -jar gitblit.jar --baseFolder data
Then I stored this property list on ~/Library/LaunchAgents/org.gitblit.plist:
<?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>KeepAlive</key>
<true/>
<key>Label</key>
<string>org.gitblit.plist</string>
<key>ProgramArguments</key>
<array>
<string>/bin/bash</string>
<string>/opt/scripts/start_gitblit.sh</string>
</array>
<key>RunAtLoad</key>
<true/>
</dict>
</plist>
Everything is working now.

How to register(install) as service java application with JSVC wrapper on MacOS X

I have an application which i want to run as service on MacOS X. I used JSVC as wrapper and currently it starts in console just fine, shutdown process is correct, etc. So now i have to register it as service. Found some manuals, wrote .plist file. Next i executed
sudo launchctl load /Library/LaunchDaemons/my.service.plist
sudo launchctl start my.service
And nothing happened. Service didn't started.
plist contents:
<?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>Label</key>
<string>my.service</string>
<key>ProgramArguments</key>
<array>
<string>/servertest/MYService</string>
<string>-jvm</string>
<string>server</string>
<string>-outfile</string>
<string>out.txt</string>
<string>-errfile</string>
<string>err.txt</string>
<string>-verbose</string>
<string>-debug</string>
<string>-home</string>
<string>/System/Library/Frameworks/JavaVM.framework/Home</string>
<string>-cp</string>
<string>./lib/hsqldb.jar:./lib/my-wrapper.jar:./lib/commons-daemon-1.0.8.jar</string>
<string>my.service.DaemonMac</string>
</array>
<key>WorkingDirectory</key>
<string>/servertest</string>
<key>StandardOutPath</key>
<string>/servertest/stdout.log</string>
<key>StandardErrorPath</key>
<string>/servertest/stderr.log</string>
<key>KeepAlive</key>
<false/>
<key>RunAtLoad</key>
<true/>
</dict>
</plist>
What am i doing wrong?
Okay, nobody knows, i guess. Anyway, i handled it myself. Here's example which worked for me:
<?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>KeepAlive</key>
<false/>
<key>Label</key>
<string>myserver</string>
<key>OnDemand</key>
<true/>
<key>ProgramArguments</key>
<array>
<string>/opt/MYServer/MYServer</string>
<string>-server</string>
<string>-outfile</string>
<string>/opt/MYServer/out.txt</string>
<string>-errfile</string>
<string>/opt/MYServer/err.txt</string>
<string>-verbose</string>
<string>-debug</string>
<string>-nodetach</string>
<string>-home</string>
<string>/System/Library/Frameworks/JavaVM.framework/Home</string>
<string>-cp</string>
<string>/opt/MYServer/lib/hsqldb.jar:/opt/MYServer/lib/my-wrapper.jar:/opt/MYServer/lib/commons-daemon-1.0.8.jar</string>
<string>my.service.DaemonMac</string>
</array>
<key>RunAtLoad</key>
<true/>
<key>StandardErrorPath</key>
<string>/opt/MYServer/stderr.log</string>
<key>StandardOutPath</key>
<string>/opt/MYServer/stdout.log</string>
<key>WorkingDirectory</key>
<string>/opt/MYServer</string>
</dict>
</plist>

Resources