Input dialog to filename

Hello,
I’m not that frequent an AppleScript scripture, so it seems every time I try something, I have to relearn!

I need to take user input to create a text file on the desktop.

set client_name to ""
set client_name to display dialog "Client Name?:" with title "Key Generator" default answer "client name"
set x to ""
repeat 48 times
	set x to x & some item of "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%^&*()_-+={}?"
end repeat
set the clipboard to x

set thePath to (path to desktop as Unicode text) & string {client_name + ".txt"}

(* If there's an error, recover and continue from where the access is closed. *)
set fRef to (open for access file thePath)
try
	(write) & {x}
end try
close access fRef
-- Rest of script.

(* Ditto. Here we think there may even be a problem opening the access. *)
try
	set fRef to missing value
	set fRef to (open for access file thePath)
	-- Read/write stuff here.
end try
if (fRef is not missing value) then close access fRef -- If fRef _is_ missing value, the access wasn't opened.
-- Rest of script.

(* If there's an error, delay it just long enough to close the access. Otherwise continue. *)
set fRef to (open for access file thePath)
try
	-- Read/write stuff here.
on error errMsg number errNum
	close access fRef
	error errMsg number errNum
end try
close access fRef
-- Rest of script.

Thanks for any advice or help you can provide!

Hi,

there are a lot of syntax error.
This should do it


set client_name to text returned of (display dialog "Client Name?:" with title "Key Generator" default answer "client name")
set x to ""
repeat 48 times
	set x to x & some item of "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%^&*()_-+={}?"
end repeat

set thePath to (path to desktop folder as text) & client_name & ".txt"
try
	set fRef to open for access file thePath with write permission
	write x to fRef
	close access fRef
on error
	try
		close access file thePath
	end try
end try


Stephan,
Awesome… Yeah, I was WAY!!! OFF!!

Thank you so much. One Happy camper now. It’s such a silly thing, but it makes it so much smoother day-to-day and I’m pretty much ensured I’m not generating predictable and repeating keys…

All the best,
Scott
:slight_smile:

If you’re running Mavericks or Yosemite, it’s easy enough to generate a UUID:

use framework "Foundation"

set theString to current application's NSUUID's UUID()'s UUIDString() as text