Using LaunchAgents to Move Files

mykool
New Contributor III

Basically, what we are wanting to do is automatically move files from directories to OneDrive directories when a new file has been downloaded. I have created a script using mv -v ~/Desktop/* ~/OneDriveLocation/Desktop/ which works when running it. Created a LaunchAgent under /Library/LaunchAgents using watch parameter and Globbing. I don't get an errors loading it but the LA doesn't seem to do anything. Any advice?

8 REPLIES 8

mm2270
Legendary Contributor III

Have you run a launchctl list | grep "name_of_launchd_here" on the machine it's loaded on to see if it's reported a 0 exit status? if you see a non 0 exit status show up in the results it may be having an issue. Best thing to do is add the StandardErrorPath key to your LaunchAgent. It looks like something like this:

<key>StandardErrorPath</key>
<string>/tmp/test.stderr</string>

When the job runs, if it encounters any problems it should write them out to that tmp file so you can examine what it's complaining about. You'll have to unload/load to get the key to take effect.

mykool
New Contributor III

So, using the error path, it does try to run the script but it errors. When I run the script normally, it works, but the launchagent seems to throw an error of the directory can't be found. So, help me out here. This is what I have. Here is my launch agent and the script

<?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.company.DesktopMove</string>
<key>EnableGlobbing</key>
<true/>
<key>ProgramArguments</key>
<array>
<string>~/Scripts/MoveDesktop.sh</string>
</array>
<key>WatchPaths</key>
<array>
<string>~/Desktop/</string>
</array>
<key>OnDemand</key>
<true/>
</dict>
</plist>
* So when I drop something on the desktop, it does actually try to run the script. here is the script.

Move desktop items to OneDrive directory

mv ~/Desktop/* ~/Documents/Test/
Very simple. But says the directory doesn't exist. I'm running it on Big Sur.

Can anyone reproduce this to actually work?

mykool
New Contributor III

The error is shows is that it's trying to move it into ~/Documents/Test/ I think it's the "" at the end of the destination. But I can't figure out how to resolve that

sdagley
Esteemed Contributor II

@mismith223 Looks like you forgot the / between ~ and Documents for your target directory operand. Since you're running as a LaunchAgent I think the ~ will resolve to the current user's home directory, but having the script determine the current user would be better. See this article for a method to do that: Getting the current user in macOS – Update

mykool
New Contributor III

JAMF took out the asterisk I was adding. Not sure how to display that here. So it's looking for ~/Documents/Test/asterisk which isn't a directory. But running the script straight from the terminal works.

sdagley
Esteemed Contributor II

@mismith223 Running the script from Terminal is not the same as running it from launchd (which is what your LaunchAgent is doing). Have you tried the article linked in my previous replay and incorporated its method for determining the loggedInUser and changing your script to reference /Users/$loggedInUser/Documents/... instead of ~/Documents/...?

mykool
New Contributor III

It can find the username just fine. The logs show it pipes that information in correctly.

noahblair1
New Contributor

You may have already figure this out, but if you give /bin/bash Full Disk Access in Security & Privacy, that will resolve this issue. Not sure if that's the most secure way to handle it. But it works.