Monday, July 16, 2018

#1 2014-11-17 10:09:05 am

stsanford
Member
Registered: 2014-11-17
Posts: 2

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.

Applescript:

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!

Offline

 

#2 2014-11-17 11:17:35 am

StefanK
Member
From:: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 11499
Website

Re: Input dialog to filename

Hi,

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

Applescript:


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


regards

Stefan

Offline

 

#3 2014-11-17 11:34:22 am

stsanford
Member
Registered: 2014-11-17
Posts: 2

Re: Input dialog to filename

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
smile

Offline

 

#4 2014-11-17 04:43:44 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 5405

Re: Input dialog to filename

stsanford wrote:

I'm pretty much ensured I'm not generating predictable and repeating keys...


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

Applescript:

use framework "Foundation"

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


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)