ali-rantakari / trash

Small command-line program for OS X that moves files or folders to the trash.
570 stars 16 forks source link

trash command doesn't work in LaunchAgent #30

Closed dcastil closed 5 years ago

dcastil commented 6 years ago

Hi @ali-rantakari!

I try to set up a script which moves old files in a specific directory into the trash every day and found out that the trash command isn't working when started from the LaunchAgent.

So when creating a file in ~/Library/Launchagents with following content

<!-- 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>sh</string>
        <string>-c</string>
        <string>~/script.sh</string> <!-- this script is being executed -->
    </array>
    <key>RunAtLoad</key>
    <true/>
    <key>StartCalendarInterval</key>
    <dict>
        <key>Hour</key>
        <integer>18</integer>
        <key>Minute</key>
        <integer>0</integer>
    </dict>
</dict>
</plist>

and a file ~/script.sh with following content

# script.sh

touch test1
touch test2
rm test1
trash test2

and then load the LaunchAgent with

$ launchctl load ~/Library/LaunchAgents/test.plist

only test1 gets deleted and not test2. Do you know what's the reason for that? I couldn't find out why it's not working.

Thanks!

raybaxter commented 5 years ago

@dcastil Where is your copy trash located?

rm is in /bin/rm which would be in the path of a shell script run by LaunchAgent, but trash probably isn't. You'll need to include the path to trash in ~/script.sh or put trash somewhere on the default path.

dcastil commented 5 years ago

That makes sense, thanks for the explanation! I just didn't think that the context of execution would be different from the terminal.