Dialog with 2 buttons, each triggering a different script
Hi!
I was wondering if it would be possible to have a dialog box show up and have only 2 buttons, none of which is a Cancel button, and that each button triggers a different script. Like this:
Button 1: script 1
Button 2 script 2
Or would it be possible to do some kind of if else (or if if? Is it a thing?). Putting both scripts within an if statement?
Thank you!
- Chris Shaw @Chris_Shaw2022-04-30 23:03:37.402Z2022-04-30 23:12:12.841Z
If your looking to run a another external script / macro with a dialog you could do this:
//Get choice from user const scriptChoice = sf.interaction.displayDialog({ prompt: "Which Script?", buttons: ["Script 1", "Script 2"], }).button // Run command based on user answer if (scriptChoice == "Script 1") { sf.soundflow.runCommand({ commandId: '/*commandId of script you wish to run*/', }); } else { sf.soundflow.runCommand({ commandId: '/*commandId of script you wish to run*/', }); }
If you want to pick between two functions within the script you can do this:
// Define functions function scriptOne() { log("Running function 1") }; function scriptTwo() { log("Running function 2") }; // Get choice from User const scriptChoice = sf.interaction.displayDialog({ prompt: "Which Script?", buttons: ["Script 1", "Script 2"], }).button; //Ternary operator to pick which script to run (scriptChoice == "Script 1") ? scriptOne() : scriptTwo();
Chris Shaw @Chris_Shaw2022-04-30 23:05:20.624Z
I would recommend having a cancel button otherwise there's no way to stop either script from running if you triggered this by mistake. You don't need to hit "Cancel' you can just press escape.
- In reply toChris_Shaw⬆:TThomas Gloor @Thomas_Gloor
Hey @Chris_Shaw
Thank you very much! If I were to add a cancel button, should I do it like so?
//Get choice from user const scriptChoice = sf.interaction.displayDialog({ prompt: "Which Script?", buttons: ["Script 1", "Script 2"], cancelButton: ["Cancel"], }).button // Run command based on user answer if (scriptChoice == "Script 1") { sf.soundflow.runCommand({ commandId: '/*commandId of script you wish to run*/', }); } if (scriptChoice == "Script 2") { sf.soundflow.runCommand({ commandId: '/*commandId of script you wish to run*/', }); } else { throw 0 }); }
Also, I have a subsidiary question. I plan to release a package, so I'm trying to be careful about the way it is structured. Is there a disadvantage in calling other commands within script? By opposition as using the second solution and defining each full script as a function?
Best!
Chris Shaw @Chris_Shaw2022-05-01 15:44:32.862Z
You define a cancel button by adding "Cancel" to the
buttons
list:const scriptChoice = sf.interaction.displayDialog({ prompt: "Which Script?", buttons: ["Cancel","Script 1", "Script 2"], cancelButton: "Cancel" }).button;
As for your second question, there is no disadvantage in calling other commands from within script. It's good practice to do so if the scripts you are calling are scripts that can run on their own - it prevents redundant coding and if you update the script you only have to do so in one place.
What is important for a published package is that the script that you call must reside in the package itself. When you set the command ID you must use "Copy Command ID Local to Package":Otherwise when a user uses your script it will try to access that command from your account which isn't allowed and will throw an error. The local package ID will call the script that resides in the package itself.
The command ID makes a reference to your user ID:
user:cki0lfpum0002hx10oh37c1m2:cl22hdmga0003bq10bppkrpex
Whereas the Command ID Local to Package doesn't:
package:cl22hdmga0003bq10bppkrpex
- TThomas Gloor @Thomas_Gloor
Thank you for clarifying @Chris_Shaw
I usually keep commands that call each other within the same package, as I noticed that it could be an issue.- TThomas Gloor @Thomas_Gloor
It works like a charm! Thank you for your help. There is just one thing I don't really understand.
//Ternary operator to pick which script to run (scriptChoice == "Script 1") ? scriptOne() : scriptTwo();
Could you tell me in english (lol) exactly what it means? So I can try to replicate and understand?
Chris Shaw @Chris_Shaw2022-05-01 18:38:21.393Z
it's a more compact way of doing a simple if / or statement:
(is this true) ? thenDoOrReturnThis : elseReturnOrDoThisInstead
So in this script if
scriptChoice == "scriptOne"
then return (or in our case execute) the left side of the colon else return / execute the right side of the colon.It can also be used to simplify an if / else statement like this:
if (firstName == "Chris") { var lastName = "Shaw" } else { var lastName = "Gloor" }
can be written as:
var lastName = (firstName == "Chris") ? "Shaw" : "Gloor"
- TThomas Gloor @Thomas_Gloor
Thank you @Chris_Shaw for this very clear explanation. I’m really trying to absorb as many theory and practice at the same time, and I’m fairly new so, it helps tons!
One last thing. The “.button” at the end of the dialog constant. Does it mean that the constant actually stores which button was pressed? As in it stores the result of the button property of the dialog object?
Chris Shaw @Chris_Shaw2022-05-02 02:44:16.859Z2022-05-02 02:57:59.851Z
if you were to use the button dialog without
.button
const scriptChoice = sf.interaction.displayDialog({ prompt: "Which Script?", buttons: ["Script 1", "Script 2"], cancelButton: ["Cancel"], })
then the dialog would return this object when we press Script 1:
{ "Button": "Script 1", "Text": null, "GaveUp": false, "Success": true, "Error": null, "UserCancelled": false }
We only need the button name so we use:
const scriptChoice = sf.interaction.displayDialog({ prompt: "Which Script?", buttons: ["Script 1", "Script 2"], cancelButton: ["Cancel"], }).button
Which returns
"Script 1"
which is stored inscriptChoice
- TThomas Gloor @Thomas_Gloor
OK!
Thank you! So basically, it is what I understood. It's a way to choose which data I wanna get from the dialog.
Thank you for allowing me in JavaScript 101.
Best,
T.