So I’m writing an Applescript to roll virtual polyhedral dice.
This is where I am …
rollAgain()
on rollAgain()
-- List of dice counts (1 to 10, adjust as needed)
set diceOptions to {"1", "2", "3", "4", "5", "6", "7"}
set sidesOptions to {"3", "4", "6", "8", "10", "12", "20", "26"}
set diceChoice to choose from list diceOptions with prompt "Role playing games normally use dice in their mechanics. So first choose the number of dice you'd like to roll ..." default items {"2"} without multiple selections allowed
if diceChoice is false then return
set sidesChoice to choose from list sidesOptions with prompt "Now choose the number of sides that the dice should have ..." default items {"26"} with multiple selections allowed
if sidesChoice is false then return
set diceCount to (item 1 of diceChoice) as integer
set sidedness to (item 1 of sidesChoice) as integer
rolldice(diceCount, sidedness)
end rollAgain
on rolldice(diceCount, sidedness)
set r to return
set eventList to {}
repeat diceCount times
set end of eventList to (random number from 1 to sidedness)
end repeat
-- Convert the dice count to words
set diceCountInWords to numberToWord(diceCount)
set longestNumber to 0
repeat with i from 1 to count of eventList
set currentNumber to (item i of eventList) as string
if (length of currentNumber) > longestNumber then
set longestNumber to length of currentNumber
end if
end repeat
set formattedResults to ""
repeat with i from 1 to count of eventList
set currentResult to (item i of eventList) as string
set padding to (longestNumber - (length of currentResult))
set paddedResult to spacePadding(padding) & currentResult
set formattedResults to formattedResults & "Dice " & i & ": " & paddedResult & r & r
end repeat
set dialogResult to display dialog "You rolled " & diceCountInWords & " " & sidedness & "-sided dice" & r & r & formattedResults buttons {"Thanks", "Roll Again"} default button "Thanks"
if button returned of dialogResult is "Roll Again" then
rollAgain() -- Call the main function again
else
return -- Exit the script
end if
end rolldice
on spacePadding(padding)
set spaceString to ""
repeat padding times
set spaceString to spaceString & " "
end repeat
return spaceString
end spacePadding
on numberToWord(n)
if n = 1 then
return "one"
else if n = 2 then
return "two"
else if n = 3 then
return "three"
else if n = 4 then
return "four"
else if n = 5 then
return "five"
else if n = 6 then
return "six"
else if n = 7 then
return "seven"
else if n = 8 then
return "eight"
else if n = 9 then
return "nine"
else if n = 10 then
return "ten"
else
return n as text
end if
end numberToWord
Broadly speaking, I’m very happy with the script. It does what it needs to do. Having said this there are a few improvements that I’d like to make, and if anyone could help, I’d be very grateful.
-
I want to change the buttons that appear in the number of dice selector dialog box to read ‘cancel’ which will end the script, and ‘Next Step’ which will take the script to the next stage.
-
I also want to change the buttons that appear in the number of sides to the dice selector dialog box to read ‘cancel’ which will end the script, and ‘Roll’ which will activate the script.
Those changes are pretty simple I think.
I wondered if it’s possible if it’s possible at stage two of the script to have graphics of the various polyhedral dice rather than a drop down box? I’m not sure it is but I thought I’d ask anyway.
Lastly is there a way to embed a graphic at the start of the script - like a PNG that I create? Within the final app this would be embedded although for the script I could link to a file on my Mac? Are there specs for how big/small, resolution and so on for this graphic?