Using a Variable as a Constraint for an Action

Moutya

New member
I'm new to MacroDroid and have no coding experience.

I am trying to do something that I thought would be incredibly easy, but which has tuned out to be a multi-day nightmare.

I have a list of various folders that I want to delete periodically using an Action. I created a delete folder action for each individual folder (about 8 of them) and then assigned the desired Trigger to fire off these actions. This worked fine, except for the fact that once the folders have been deleted, the next time the Action is run I get bombarded with errors saying "Please reconfigure the file path to an accessible location on your device". These errors not only show as notifications, but also on the Actions themselves in the action list.

I thought an easy way to fix this would be to add an "If folder exists/does not exist" condition (which I assumed would be an option or checkbox somewhere) but I could find no such option anywhere. After trawling various sites and web searches I decided to try gibing this AI thing a go. The AI said that MacroDroid has a "File/Folder Exists" action and trigger, and then it gave step-by-step instructions on how to construct the macro. The only problem is that there is no such action in MacroDroid (at least as far as I can see).

The lack of this action seems to be borne out by my search on this forum, which turned up only one result in which a Bash script work-around had to be used simply to check if a folder existed or not. The instructions in that post were vague (it just said 'use a variable') which was not much help to me as a noob.

I ended up going back to AI after hours of further searching on how to do this.

AI suggested that I create a shell script with the following command:

Code:
[ -d /path/to/directory ] && echo "Directory exists"

Then add a Set Variable.

It got confusing here because at first it said just assign the variable as 'True' (so presumably Boolean) but then it later said use a String for the variable, but I didn't know whether that variable should contain the text to match (i.e. "Directory exists") or whether the string "True" would be returned if the previous shell command had echoed some output.

Here's what AI said:

  • In the "Variable Set" action, set the variable name to "Folder Exists".
  • In the "Value" field of the "Variable Set" action, enter the following expression: if([rd] = "Directory exists", "true", "false").
  • In this expression, [rd] is a special MacroDroid variable that contains the output of the last "Run Shell Command" action. The expression checks if [rd] is equal to the string "Directory exists". If it is, the expression returns the string "true". If it is not, the expression returns the string "false".
  • Finally, use the "Constraint" feature to check the value of the "Folder Exists" variable before running the following actions to ensure that they are only taken if the directory exists.


This all made sense to me "logically" (bearing in mind that I have no coding or MacroDroid experience) but needless to say it didn't work. I tested by assigning a notification action instead of the folder delete action, but the notification was always triggered regardless of whether the folder in question existed or not.

I even tried using a script to do the folder deletion and just call the whole deletion process from a script action, but that didn't work either, and I don't like that clunky work-around because I can't easily toggle individual folder delete actions on an off as needed (which is easy to do if the delete actions are in the MacroDroid GUI, because you can just tap to disable individual actions, and then re-enable them just as easily). However, if using a script to do the deletion then you'd have to keep opening the script to comment out or delete specific paths (which could easily lead to breaking the script if you even slightly mess up, which is especially likely when trying to edit blocks of script text on a small screen like a phone).

In this particular instance I don't actually care whether the folders exist or not (if there's no folder to delete then that is not an issue). I only resorted to this method of folder-checking in the absence of any native means to stop MacroDoid throwing a total fit just because the Action path does not currently exist at certain points in time.

Any help with the above issue (that preferably stays within the MacroDroud GUI wherever feasible / possible) would be very much appreciated. Thanks.
 
Last edited:

RSF

Well-known member
See simpler example, attached; might help. I think you'll need to use a shell script -- I don't see a way to check for a folder's existence solely within MacroDroid's native actions. But this example may help illustrate the variables you'll need to (a) capture the output of the shell command and (b) turn that into a true/false scheme.

Note that Android has been getting more and more picky about letting apps look at folders and files on a device. Sometimes it seems like MacroDroid needs to ask for folder-access permissions after they've already been set previously. Seems to work OK, though, as long as the folders are in shared-by-definition folders like "Downloads" or "Documents".
 

Attachments

  • Test_for_folder.macro
    6.6 KB · Views: 11

Endercraft

Moderator (& bug finder :D)
Sometimes it seems like MacroDroid needs to ask for folder-access permissions after they've already been set previously
It seems like a bug from MacroDroid because even when it tells me to reconfigure the file access, it still works fine.
 

Moutya

New member
See simpler example, attached; might help. I think you'll need to use a shell script -- I don't see a way to check for a folder's existence solely within MacroDroid's native actions. But this example may help illustrate the variables you'll need to (a) capture the output of the shell command and (b) turn that into a true/false scheme.

Note that Android has been getting more and more picky about letting apps look at folders and files on a device. Sometimes it seems like MacroDroid needs to ask for folder-access permissions after they've already been set previously. Seems to work OK, though, as long as the folders are in shared-by-definition folders like "Downloads" or "Documents".

Thank you for your reply and example macro.

I'll take a look at it and report back how I get on.
 

Moutya

New member
@RSF

I've loaded up your example code and it works perfectly! It does exactly what I want.

The way that the macro is laid out makes it easy to understand too (so I can now easily adapt this for multiple other use cases).

Thank you very much for taking the time to put this relevant example together.

Cheers (y)
 
Top