You can make a difference in the Apple Support Community!

When you sign up with your Apple Account, you can provide valuable feedback to other community members by upvoting helpful replies and User Tips.

script application set up question

Hi,

When I add script as below, after run , it will show message:some files could not be moved to the trash,

How to ignore it and keep the script to run complete? And how to add to assigned folder?Thanks.

Posted on Oct 9, 2024 7:26 AM

Reply
8 replies

Oct 9, 2024 7:45 PM in response to show0116

While the modern approach is to use a launch daemon with a schedule, the old way of using cron is often simpler, especially with the changes made to launchd over the past few OS releases.


If you make this an app, you can modify /etc/crontab (requires admin rights) and create an entry to trigger the tool on a regular schedule. For example, if you want a script.sh to execute under the user context of username (meaning your user account), at 20:45 (8:45 p.m.), every day, every day of the week, and every day of the month, you would use a line like this. First column is minutes, second is hours in 24 hour time.


45      20      *       *       *       username      /Library/username/path/to/script.sh


Search for cron or crontab examples and you will find lots of examples.


If you want to do it the "modern" way, start here with Apple's dev docs.


Either path can get you to scheduling a process to run at repeated intervals.



Oct 9, 2024 5:30 PM in response to show0116

Try removing the -o as I believe that is an or conditional. Thus, the command is looking for files (-type f) with a -ctime of +1d OR anything with an -mtime of +1d. If the folder has an -mtime of +1d than it is included because of the -o.


I likely should ask some questions at this point.


• Describe the workflow you are trying to achieve. I know you are trying to clear files from a folder based on matching criteria. That I get. But when do you want this to run? Is it running all the time (watched folder)? Is it running once a day (cron or launch daemon)? Every hour? And how do you plan to trigger this? Are you double clicking something? Is it running automatically in the background?


Oct 9, 2024 8:41 AM in response to show0116

I think the issue is the -iname '*.*' as I am going to speculate it is trying to delete the hidden . or .. special files inside all directories. For example, if I break this down and run the following command on a folder:


find . -iname '*.*'


The result is:


.
./.DS_Store
./bff02d99-1309-4215-a32a-829ccff47b84_Part003.zip
./bff02d99-1309-4215-a32a-829ccff47b84_Part002.zip
./bff02d99-1309-4215-a32a-829ccff47b84_Part001.zip
./afile.txt


Note that . is included at the top of the results. You cannot move that item to the trash. This is the cause of your error.


You should modify or remove the -iname directive. You are already searching for creative time more than 1 day and modify time of more than 1 day. Why include a name wildcard when all files that match the time criteria will be included regardless of name?


You might consider focusing just on files as this will ignore directories and specials. If you are only trying to remove files, then perhaps a:


find /your/path -type f -ctime +1d -o -mtime +1d


If you are already using find, why not just append a -delete the end? Then you can remove the "Move Finder Items to Trash." Note, if you use -delete, then the files are gone. If you are looking for the safety net of the Trash, then continue to use the second tile in your automation to move to trash for later deletion.


Make sure you test these recommendation on sample data. I do not know the entirety of your workflow or your intended goals. I am providing a suggestion based on what is posted. The command and recommendation I provide above can result in file deletion. Run incorrectly, this could result in data loss. Test before implementing. Trust, but verify.



script application set up question

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.