-1

Im new to creating scripts for Apple (MacOsx Ventura)… I would like to create an AppleScript with serveral buttons 3 to 9 depending on what Shell script I want to create… What I have now works in Mac terminal with selecting by pressing 1, 2, 3, etc… I like the look of Buttons… this method looks good so… but how to select the option and running a shell script or applescript after clicking ok

set optionList to {“Shell Script-1”, " Shell Script-2", " Applescript-1", " Applescript-2"}
set chosenScript to choose from list optionList with prompt “Choose a Script to run:”
if chosenScript is false then
    error number -128 (* User cancelled *)
else
    set chosenScript to chosenScript’s item 1 (* extract choice from list *)
end if
display dialog "You chose a " & chosenScript & “!”
vadian
  • 274,689
  • 30
  • 353
  • 361
  • Shane Stanley wrote the script library Dialog Toolkit Plus available on [this site](https://latenightsw.com/freeware/). You can build custom dialog windows. – vadian Feb 10 '23 at 17:05

2 Answers2

0

With choose from list it's pretty easy to get the selected item by adding index prefices.

set optionList to {"1 Shell Script-1", "2 Shell Script-2", "3 Applescript-1", "4 Applescript-2"}
set chosenScript to choose from list optionList with prompt "Choose a Script to run:"
if chosenScript is false then
    error number -128 (* User cancelled *)
else
    set chosenIndex to word 1 of (chosenScript's item 1) as integer
end if
display dialog "You chose a " & text 3 thru -1 of item chosenIndex of optionList & "!"

You can even press the index number and then return.

vadian
  • 274,689
  • 30
  • 353
  • 361
  • so if user select 1 or highlights Shell Script-1 and clicks ok, how would I get it to run /Volume/USB/Folder/Shell-Script-1.sh – David Bowyer Feb 10 '23 at 17:29
  • also how can I get an applescript to run when the user select applescript-1 Tell Application "/Volume/USB/Folder/Applescript-1.app" I'm able to convert *.scpt to *.app... – David Bowyer Feb 10 '23 at 17:40
  • Create a second list with the paths and call `do shell script item chosenIndex of pathList` or `run script ...` – vadian Feb 10 '23 at 17:59
  • add this under set optionLst to set pathList to {“/volume/USB/Folder/Shell Script-1.sh”, " /volume/USB/Folder/Shell Script-2.sh ", " /volume/USB/Folder/Applescript-1.app", " /volume/USB/Folder/Applescript-2.app"} ???? – David Bowyer Feb 10 '23 at 18:18
  • then add this under display dialog line?? do shell script item chosenScripts of pathList – David Bowyer Feb 10 '23 at 18:19
0

I found what I needed, it maybe not be buttons, but it has selectable options.. I got it with help from ChatGPT

    set scriptsList to {"Enable SMB", "Disable SMB", "Create a Local User", "Delete a Local User", "Application Install", "Student Password Fix"}

choose from list scriptsList with title "Script Selection" with prompt "Select a script:"

if the result is not false then
    set userChoice to item 1 of result
    
    if userChoice is "Enable SMB" then
        set scriptPath to "/Volumes/Ventoy/Scripts/MAC_Scripts/Enable-SMB.zsh"
        do shell script "open -a Terminal -sh " & quoted form of scriptPath
    else if userChoice is "Disable SMB" then
        set scriptPath to "/Volumes/Ventoy/Scripts/MAC_Scripts/Disable-SMB.zsh"
        do shell script "open -a Terminal -sh " & quoted form of scriptPath
    else if userChoice is "Create a Local User" then
        set scriptPath to "/Volumes/Ventoy/Scripts/MAC_Scripts/Create-Local-Account.scpt"
        do shell script "osascript " & quoted form of scriptPath
    else if userChoice is "Delete a Local User" then
        set scriptPath to "/Volumes/Ventoy/Scripts/MAC_Scripts/User-Delete.zsh"
        do shell script "open -a Terminal -sh " & quoted form of scriptPath
    else if userChoice is "Application Install" then
        set scriptPath to "/Volumes/Ventoy/Scripts/MAC_Scripts/Applications-Policies.zsh"
        do shell script "open -a Terminal -sh " & quoted form of scriptPath
    else if userChoice is "Student Password Fix" then
        set scriptPath to "/Volumes/Ventoy/Scripts/MAC_Scripts/Student_Password_Fix.zsh"
        do shell script "open -a Terminal -sh " & quoted form of scriptPath
        
    end if
end if