Page 1 of 2

Random wallpaper

Posted: 30 Oct 2018 20:15
by Vicdilou
Hello,

I create this flow to change wallpaper randomly, I think there is a simpler and more optimized way to do it but I don't know how.

What do you think ?
Thanks

Re: Random wallpaper

Posted: 30 Oct 2018 22:32
by Helios
Hi,

maybe you could do the following.

Start with your trigger.
Connect it to a Script action:

Code: Select all

wallpapers = newList("Name of wallpaper 1.", "Name of wallpaper 2.", "Name of wallpaper 3.", ...);
random_wallpaper = getRandomElement(wallpapers);
Then use your action (Set Live Wallpaper), but use {random_wallpaper} as value for the Live Wallpaper.

Re: Random wallpaper

Posted: 31 Oct 2018 09:21
by Vicdilou
I try with one wallpaper but it doesn't work
I don't know if it's the right name of the wallpaper or if I did something wrong with the script.

31.10.2018 10:20:22.333 [Fond d'écran jour copy] Error:
ch.gridvision.ppam.androidautomagic.simplelang.a.d: Could not evaluate expression 'wallpapers = newList("Dans la lune",)
random_wallpaper = getRandomElement(wallpapers)' due to errors: [expression expected[36], ';' expected[38]]

Re: Random wallpaper

Posted: 31 Oct 2018 11:00
by Desmanto
The error is at the missing semicolon (;) at the end of the first line. And also additional comma for one element only. You should put semicolon and also remove the comma

Code: Select all

wallpapers = newList("Dans la lune",)
random_wallpaper = getRandomElement(wallpapers)
Helios just give you example, so semicolon is missing from the script. The proper one should be

Code: Select all

wallpapers = newList("Dans la lune");
random_wallpaper = getRandomElement(wallpapers);
Make it as a habit to always end the line with semicolon (unless for certain if() else situation). And because you have 15, it will better to separate each wallpaper into new line for better view/reading. For example

Code: Select all

wallpapers = newList(
"Wallpaper1",
"Wallpaper2",
"Wallpaper3",
"Wallpaper4",
"Wallpaper5");
random_wallpaper = getRandomElement(wallpapers);
The random method you get here is truly random. In 15 execution, it is possible that some wallpaper haven't been appear once and there are some appear twice. If you need random, but each wallpaper must appear once in every 15 execution; then you should create the list in glovar, example global_wallpapers. Then store the execution count in another glovar. If executed 15 times, shuffle it and reset the count to 0.

To create the glovar, you can add it manually and use list type. Or you can just type in script for one time creation.

Code: Select all

global_wallpapers = newList(
"Wallpaper1",
"Wallpaper2",
"Wallpaper3",
"Wallpaper4",
"Wallpaper5");
global_wallpapers_count = 0;
Execute this once, it will create your glovar and count. Then delete the script and use this script for the loop.

Code: Select all

random_wallpaper = global_wallpapers[global_wallpapers_count];
global_wallpapers_count = global_wallpapers_count + 1;
if(global_wallpapers_count == 15)
{
  shuffleList(global_wallpapers);
  global_wallpapers_count = 0;
}
First we get the random wallpaper from the shuffled glovar (first 15 execution is not shuffled yet) based on the current count. Increment the count, so next time it gets the next wallpaper. Check if the count is 15 already (already execute 15 times). If yes, shuffle it again and set the count to 0, so it restart to retrieve from the first random element.

In total, you need only one trigger, one script and one set live wallpaper (and 2 glovar). Much more efficient flow

About the trigger, it seems you use some other plugin to do it. If your trigger is notification on statusbar, Automagic already have that trigger, try to find it in the trigger list.

Re: Random wallpaper

Posted: 31 Oct 2018 12:53
by Vicdilou
Hi,

The script work but I don't understand how to create the glovar.
The trigger is good, I use it with other flow and IFTTT.

Thanks

Re: Random wallpaper

Posted: 31 Oct 2018 18:30
by Desmanto
You can swipe from left to open the hamburger menu (where you can access Automagic Settings) There is Global Variables, tap that. Click plus sign and add global_wallpapers, change the type from string to list. Add value and add each of your wallpaper name until finish. Then add another glovar global_wallpapers_count with value 0.

Or you can just simply add it in script just like here

Code: Select all

global_wallpapers = newList(
"Wallpaper1",
"Wallpaper2",
"Wallpaper3",
"Wallpaper4",
"Wallpaper5");
global_wallpapers_count = 0;
After changing wallpaper1 to your wallpaper name, tap 3 dot menu at top right > execute. This will execute the script and create the glovar directly. After that, you can delete the script, as it is one-time-job only.

Re: Random wallpaper

Posted: 31 Oct 2018 19:10
by Vicdilou
The value doesn't change after an execution. I don't know where I'm wrong.

Re: Random wallpaper

Posted: 01 Nov 2018 15:06
by Desmanto
Which value? The global_wallpapers? That one only change after 15 executions, not every execution. So you have random list, but every wallpaper should appear at least once in 15 executions.

Re: Random wallpaper

Posted: 03 Nov 2018 06:45
by Vicdilou
It's doesn't work, sometimes I have the same wallpaper twice.

Re: Random wallpaper

Posted: 03 Nov 2018 18:30
by Desmanto
For each 15 execution it will be different. However, after 15, the list got randomized. It is possible at 15th, you got wallpaper A as the last. Then at the next randomize, A become the first element, hence it seems you got the same wallpaper again. if it is necessary, you can check for the previous wallpaper. If it is the same with the next one, you can shuffle it again, or just use the next element.