Compare Two Dates – Delete Files Older Than XX Days From Today

Compare 2 dates

The goal is to have KM go thru designated folders (day of the week folders) in the morning and if the files within are older that xx days from today, then delete them.

It doesn't seem that I am doing the right thing to compare the 2 dates.
The 'today' date is greater than 'creation2' but the statement says it's false.
The last 'if any condition' is behaving the opposite of what I would expect.
Should I be using a calculation to compare the dates?
I tried and could not get that to work either.
A shout to @JMichaelTX I use his original 'Trash files before xxx' macro a lot.
Need this one to be more automatic.
Thank you for any help.

Keyboard Maestro Actions.kmactions (3.2 KB)

These is my workflow

Please replace the last Action (set Tag) with move to trash (These was for testing purposes only)

1 Like

...sooo

This thread is the closest thing to my question,... but I still don't understand why this doesn't work;


Screen Shot 2021-06-15 at 12.47.09 PM

Screen Shot 2021-06-15 at 1.12.56 PM

I think it is because you are comparing the value of the Variable DATE to the string "A", not the value of the Variable A, but I am not able to test in KBM at my computer now.

1 Like

Valid point - I neglected to add that when I change the LESS THAN to MORE THAN the answer changes to a wrong "No". Also I get the wrong "no" when I change the PLUS to a MINUS in the %time...% formula. This leads me to believe it is seeing the A value as a number.

Take a look at the discussion at Math Comparison Fail - #5 by JMichaelTX and in particular ComplexPoint's post at #6 in the thread - I think you may find your answer there.

1 Like

Nope - I started with CALCULATION CONDITION of the IF action, then switched to "The variable:" comparison (of the IF) to see if that worked - notice the comparison options of "The variable:" option of the IF include; <, >, ≀, β‰₯, =, etc which implies a numeric comparison. This is why I'm so stumped.

Also, by changing the PLUS to a MINUS or the LESS THAN to a GREATER THAN, the result actually change but again to the wrong answer - "YES" to "NO" - he was always getting "NO"

SOLVED
I was using GREATER THAN and LESS THAN instead of "IS BEFORE" or "IS AFTER"

Thank you for your time Rolian

Glad you solved it - I learned something now too.

Hey Folks,

Just for contrast – here's one way to do the job with AppleScript.

-Chris


Delete Files in the Downloads Folder Older than 2 Days (at Midnight) v1.00.kmmacros (6.6 KB)

2 Likes

I was trying to test this to eventually use on one of my macros, but due to my low knowledge of AppleScript, there are things I still don't understand.

So I wanted to test it changing it to 2 minutes instead of 2 days:

set theDate to (current date) - 2 * minutes
Didn't work. Tried:
set theDate to (current date) - 2 * mins
Also didn't work

Now, is that the issue? Or is it something related to the path?

I remember someone here mentioning something about using colon instead of slash as a separator (can't remember who and in which context... maybe @Nige_S?).
Is this the case? I also tried with colon and it didn't work...

set theDate to (current date) - 2 * minutes
#set time of theDate to 0

set targetFolder to path to "/Users/dannywyatt/My Files/Tests/Keyboard Maestro Tests/Dropbox"

tell application "Finder"
	
	set itemsToDelete to (files of targetFolder whose creation date < theDate) as alias list
	
	# Trash items (not direct delete).
	delete itemsToDelete
	
end tell

That bit's fine.

But path to doesn't convert a string like you've got to an alias -- you need to present it as a POSIX file then make that into an alias so the Finder can use it. Try changing line 4 to:

set targetFolder to (POSIX file "/Users/dannywyatt/My Files/Tests/Keyboard Maestro Tests/Dropbox" as alias)
1 Like

Thank you. That worked!

So I have a few questions:

1. I guess path to would only be used to those predefined locations such as documents, desktop, home folder, etc? I saw a list of them earlier on some website, but can't find them anymore. But path to would only be valid in those situations?
2. If I have the path separated by colons, how would that be written? If that's even one way of doing it?
3. In here they used the POSIX differently from the way you used it. Is there a difference? They used ((POSIX......) as alias)
4. I wanted to include both files and folders. I could do it like this, but I would guess there's a way to include both in a single variable to then delete both?

tell application "Finder"
	
	set itemsToDelete1 to (files of targetFolder whose creation date < theDate) as alias list
	set itemsToDelete2 to (folders of targetFolder whose creation date < theDate) as alias list
	
	# Trash items (not direct delete).
	delete itemsToDelete1
	delete itemsToDelete2

end tell

path to is a verb in the "StandardAdditions" OSAX -- if you open the "StandardAdditions" dictionary in Script Editor/Script Debugger you'll find path to in the "File Commands" section, along with the list of locations you use it with. By default it returns an alias, a reference to the item that the Finder can use directly:

set thePath to path to downloads folder
tell application "Finder" to open thePath

Those would be the old-style, pre-OS X, paths. You can still use them, still get them:

path to downloads folder as text
--> "Macintosh HD:Users:nige_s:Downloads:"

and

set thePath to "Macintosh HD:Users:nigel:Downloads:"
tell application "Finder" to open alias thePath

Yes, but notice they've had to also use folder before the variable:

set deleted to POSIX file "/Users/username/Library/Cookies/"
    tell application "Finder"
        delete (files of folder deleted)

...which you don't have to do if you've already created the reference as an alias:

set deleted to (POSIX file "/Users/username/Library/Cookies/" as alias)
    tell application "Finder"
        delete (files of deleted)

Just different ways of converting a string into something the Finder can use as an object reference -- I'm sure @ccstone could give us a dozen more!

Back to the Finder's AppleScript dictionary...

If you look at file you'll see it inherits from item, if you look at folder it also inherits from item. So you could get items of targetFolder... and include both.

But it doesn't hurt to be specific and target files and folders separately -- the cost of the extra operations is nothing in the grand scheme of things.

1 Like

Thank you. Great info here. I save this to my AS Tips document so I can read it and take notes.

I don't see that... maybe my macOS is older than yours? You mean this?

Is this what you mean?

image

That's the Finder dictionary -- path to is in "StandardAdditions":

Yep, exactly that. You can see that folder inherits ("inh.") from container which inherits from item, while file inherits directly from item.

1 Like

I don't see that "Open Dictionary" window though.
I can open the Library and I see this:

image

Which gives me access to this:

If I search for path to then I see this:

And then clicking one of the options:

So I guess it's probably the same thing, it just looks different on my computer?

But yes, I see that the item, container, etc. I have to check some stuff online to learn a bit more about it, at least the basics. Thanks for your help!

I can't see what's different? It's the path to verb, and it's in "File Commands" of "StandardAdditions". There are two variants, one which you use with a "specified application or script" and the one we're talking about which returns "the full path to the specified folder".

Unless you mean the "Library" window -- which I guess means you're using Script Debugger rather than Script Editor. What you get will be the same -- how you get to it and how it's displayed may be different, though.

Hey Danny,

If you're going to monkey with AppleScript strongly I recommend that you download and use Script Debugger Lite (freeware).

Its Dictionary Viewer and Search alone are game-changers, and it has a multitude of usability features, unlike Apple's anemic Script Editor app.

Script Debugger Info

When writing and testing AppleScript on macOS I advise folks to use Script Debugger – even if they don't write much AppleScript.

I've used it for 26 years plus and cannot begin to express how much it changed the AppleScript experience for me.

The commercial version is not inexpensive ($99.99), BUT it reverts to its FREE β€œLite” version after a 30 day demo period – and the free version still beats the utter pants off of Apple's Script Editor.app.

-Chris

1 Like

Thanks for sharing. Downloaded it.
For the basic knowledge I still have, the Script Editor does the job, I believe.
But I installed the Debugger as well anyway.
The next goal is to start learning the basics of AS just so I can get around some macros without feeling too "dumb"...