Why Doesn't the Path Conditional Seem to Work?

The path conditional [File or Something] in the If/Then Action fails for me when there is a file there. Does anyone know what I'm doing wrong?

(Run the attached macro and choose a folder with a file in it, somewhere in the user's Documents folder.)

Thank you.

Test:PathTest.kmmacros (7.2 KB)

I'm 80% sure about this. The reason it says nothing is found is because you are misusing the condition. You are passing a folder name to a condition that's asking you for a filename. That's supposed to be a specific filename in there.

I will now ponder the best way to achieve what you want to do. There's more than one way to test what you want to do, and I want to provide a solution that you will like.

1 Like

Hi, thank you for your thoughts. This is specifically a Path condition, though, isn't it?

No, if you read the wording it says "a file exists at this path." The noun in that sentence is a file.

There is also a different clause that says "a folder exists." But I know that's not what you want either, even though it will return true in your case.

Here's one way to do what you want:


That checks if the number of lines in the output of the "ls ~/data/*" command is >=1.

I'm still working on a more elegant solution.

I just found this very issue came up before, and the solution provided is similar to mine, and the author of KM provided a slightly more elegant solution (which I too have come up with) but they don't have anything better.

I may be able to come up with something better yet. Or I may not.

1 Like

Thank, kindly!

Here's a slightly more elegant answer, because it avoids using the pipe symbol:


However there's a strange thing here! The KM Editor says "false" in this statement but when I run it, it says "something" (which means it was evaluated true). This could be a bug in the editor's code. NEVERMIND, it works correctly. Maybe the KM Editor wasn't updating its screen for some reason, but now it is.

1 Like

Thank you again, and agreedā€¦strange. :slight_smile:

If you are using the Path condition, then it has options for what you are looking for:


So if you ask it if a file exists at a path and you give it the path to a folder, then the answer will be no, a file does not exist at that path, a folder exists. It is not a check to see if a file exists with a folder at that path, that is not what the condition does.

You can count the content of the folder using the Folder Contents collection.

This seems quite odd to me, since the editor displays conditions by asking the Engine to run the code, so the result of the condition should be the same in the editor as when you run the macro, unless the condition changes because you are running the macro instead of watching the condition in the Editor (eg the Front Window condition will likely give a different result if the Keyboard Maestro editor is not the front window).

In this case, assuming you are not changing the contents of the ~/data/date directory, then the results would be the same for the evaluation and running the macro.

If I am understanding what your find command is doing, it should return either ~/data/data if it is a folder and is empty, and nothing otherwise.

Keep in mind that any ā€œinvisibleā€ file in the directory (eg Finder files like .DS_Store) will count as not empty so this test is quite fragile.

1 Like

Aha, that could be the explanation.

1 Like

Thank you, Peter.