Press button to copy to clipboard?

I’m making a script to generate a 25 digit string.
This is what i came to:

on randomString(aLength)
	set randomLetters to {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0"}
	set aString to ""
	
	repeat aLength times
		set aString to aString & some item of randomLetters
	end repeat
	
	return aString
end randomString

display dialog randomString(25) buttons {"Copy to Clipboard", "Cancel"}

What i want, is that when you press the “Copy to Clipboard” button, you get the string generated copied to clipboard.

Any thoughts?

Hi,

almost, it’s missing just one line :slight_smile:


on randomString(aLength)
	set randomLetters to {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0"}
	set aString to ""
	
	repeat aLength times
		set aString to aString & some item of randomLetters
	end repeat
	
	return aString
end randomString

set R to randomString(25)
display dialog R buttons {"Copy to Clipboard", "Cancel"} default button 1
set the clipboard to R

Thanks a lot! Now, let me see if i know what just happened…
you assigned the variable “R” to the randomString. Then the dialog displayed the variable “R” (the string). Finally the variable was copied to clipboard. Is that right? (by learning is how I won’t make the same mistakes again :D)

Exactly.

A note about display dialog:
Actually display dialog returns a record with the items text returned and button returned,
but we don’t need the result. If the user presses “Cancel”, the script will aborted with error -128