Thursday, March 23, 2017

#1 2017-03-16 06:41:35 am

Mazinguer04
Member
Registered: 2017-03-16
Posts: 4

delete old files with folders action?

When i put a file into a folder, i would like to check all files an delete the older than X days. (its for download folder). Its posible? ¿there is a way to check all files in a folder or only can check the new files in the folder?

THANK YOU

Model: Mac mini
Browser: Safari 602.4.8
Operating System: Mac OS X (10.10)


Filed under: "automator"

Offline

 

#2 2017-03-17 08:54:42 am

Nigel Garvey
Moderator
From: Warwickshire, England
Registered: 2002-11-19
Posts: 4218

Re: delete old files with folders action?

Hi. Welcome to MacScripter.

It looks as if Automator folder actions only receive the items which triggered them. But it's possible to do what you want.

1. Start a new "Folder Action" workflow.
2. Use the pop-up menu at the top of the window to choose the folder to which you want to attach it.
3. Drag a "Run AppleScript" action into the workflow window.
4. Replace the code in the action window with this:

Applescript:

on run {input, parameters}
   
   set X to 7 -- Change this to the number of days you need it to be.
   
   set XDaysAgo to (current date) - X * days -- Exactly X * 24 hours before the time the script runs.
   set time of XDaysAgo to 0 -- The start of the day X days before the time the script runs.
   
   tell application "Finder"
       -- Get one of the items which has been added to the folder.
       set item1 to item 1 of input
       -- Use it to identify the folder.
       set containerFolder to container of item1
       -- Delete every item in the folder that was last modified before XDaysAgo.
       delete (every item of containerFolder whose modification date comes before XDaysAgo)
   end tell
   
   return input
end run

5. Save the workflow.

The workflow will be set up and ready to run, but can take a few seconds to react to items being added to the folder.

It's also possible to write an AppleScript-only folder action, which doesn't involve Automator. But you'd have to attach it and set it up yourself — and I've forgotten how to do that now.  smile  The code would look like this:

Applescript:

on adding folder items to this_folder after receiving these_items
   
   set X to 7 -- Change this to the number of days you need it to be.
   
   set XDaysAgo to (current date) - X * days -- Exactly X * 24 hours before the time the script runs.
   set time of XDaysAgo to 0 -- The start of the day X days before the time the script runs.
   
   tell application "Finder"
       -- Ignore the added items and just delete every item in the folder that was last modified before XDaysAgo.
       delete (every item of this_folder whose modification date comes before XDaysAgo)
   end tell
   
end adding folder items to


NG

Offline

 

#3 2017-03-18 05:19:28 am

Mazinguer04
Member
Registered: 2017-03-16
Posts: 4

Re: delete old files with folders action?

Thank you !

Butt that creates a new problem for me. First i need to make a filter as it appears in the example (not all files and folders need to be deleted) and on the other hand, only move the files, don't delete them (As I say at first). I do not see how to solve these aspects of the problem using applescript. ¿Any more help :-) ? Thank you.
(can you see the sample at https://www.dropbox.com/s/r4l94uor2a3dc … o.png?dl=0 )

https://www.dropbox.com/s/r4l94uor2a3dcya/ejemplo.png?dl=0

Last edited by Mazinguer04 (2017-03-18 05:20:36 am)

Offline

 

#4 2017-03-18 08:06:53 am

Nigel Garvey
Moderator
From: Warwickshire, England
Registered: 2002-11-19
Posts: 4218

Re: delete old files with folders action?

Hi.

The Filter Finder Items (Filtrar ítems del Finder) action in my version of Automator (2.6) never returns any results, even when passed items which should get through the filter. I don't know whether this is just a problem on my computer or the action actually has a bug. But an AppleScript can easily perform the filtering shown in your screenshot. It could also move the qualifying items if you liked, although the destination folder would have to be coded into it. Alternatively, it can just pass the items on to the Trasladar ítems del Finder action and you can set the destination in that.

The filter in your screenshot specifies items created within the past two days, whereas your first post says files older than a certain number of days. Could you please clarify what you want here? As I understand it at the moment:

You want to move certain items and not delete any others.
Items to be moved must have been created within the past two days and must not be named "Anteriormente…" and must not be named "NO GUARDAR". (Are "Anteriormente…" and "NO GUARDAR" two items with those exact names?)


NG

Offline

 

#5 2017-03-19 08:19:19 am

Mazinguer04
Member
Registered: 2017-03-16
Posts: 4

Re: delete old files with folders action?

This is what folder action should do:

https://www.dropbox.com/s/5unmzt22ahinz … 2.png?dl=0
https://www.dropbox.com/s/5unmzt22ahinz7o/ejemplo2.png?dl=0

Offline

 

#6 2017-03-19 10:53:45 am

Nigel Garvey
Moderator
From: Warwickshire, England
Registered: 2002-11-19
Posts: 4218

Re: delete old files with folders action?

OK. That's no problem. Just use an AppleScript action with this code:

Applescript:

on run {input, parameters}
   
   set X to 2 -- Number of days ago.
   set XDaysAgo to (current date) - X * days -- Exactly 48 hours before the time the script runs.
   set time of XDaysAgo to 0 -- 00:00:00 on the day two days before the script runs.
   
   tell application "Finder"
       -- Get one of the items which has been added to the folder.
       set item1 to item 1 of input
       -- Use it to identify the folder.
       set containerFolder to container of item1
       -- Move qualifying items to the folder "Anteriormente…" in the same folder.
       move (every item of containerFolder whose name is not "Anteriormente…" and name is not "NO GUARDAR" and modification date comes before XDaysAgo) to folder "Anteriormente…" of containerFolder with replacing
       
       return result
   end tell
end run

NB. In the script, I've used the horizontal ellipsis character ("…") at the end of "Anteriormente…". If the name of that folder actually ends with three period (".") characters, you'll need to change that in the script.

Last edited by Nigel Garvey (2017-03-19 11:05:24 am)


NG

Offline

 

#7 2017-03-19 02:21:59 pm

Mazinguer04
Member
Registered: 2017-03-16
Posts: 4

Re: delete old files with folders action?

Thank you so much for everything. Now it works properly just as I wanted it to. I will try to do other folder actions using Appelscript. Thank you.

big_smilebig_smilebig_smile

Offline

 

Board footer

Powered by FluxBB

[ Generated in 0.104 seconds, 10 queries executed ]

RSS (new topics) RSS (active topics)