passing variable to handler

I am trying to pass a variable to an event handler to be processed. I am new to applescript studio and I am sure that this should be easy, but I can’t get it for the life of me. What I am trying to do is pass the result of a select folder command to later pass it to a do shell script command.

Here’s the code


on clicked theObject
if the name of theObject is equal to “sourceButton” then
set source_file to choose folder with prompt “Which directory would you like to archive?” (This is the variable that I would like to later pass)
set source_file to POSIX path of source_file
else

if the name of theObject is equal to "destButton" then
    set dest_file to choose file name with prompt "Where would you like to save your file?" (*I alos want to pass the value of this variable as well.*)
    set dest_file to POSIX path of dest_file
else

if the name of theObject is "archiveButton" then
set pass_word1 to text returned of text field "passField"   
do shell script "hdiutil create -encryption -passphrase " & pass_word1 & " -srcdir " & source_file & " " & dest_file 

Where pass_word1 is the variable I need to get the info which is typed into a text field. I don’t think that any of this is correct. In fact, I know that my passing the variables from the other buttons is not working at all and I am not sure at all about getting the text from text field is correct.

Any help would be great.

Thanks,
Chris

This should be close to what you’re looking for…

property source_file : ""
property dest_file : ""

on clicked theObject
	if the name of theObject is equal to "sourceButton" then
		set source_file to choose folder with prompt "Which directory would you like to archive?"
		set source_file to POSIX path of source_file
		
	else if the name of theObject is equal to "destButton" then
		set dest_file to choose file name with prompt "Where would you like to save your file?"
		set dest_file to POSIX path of dest_file
		
	else if the name of theObject is "archiveButton" then
		set pass_word1 to content of text field "passField" of window "YOUR_WINDOW_NAME_HERE"
		do shell script ("hdiutil create -encryption -passphrase " & pass_word1 & " -srcdir " & source_file & " " & dest_file)
	end if
end clicked

To get the values you collect to remain persistent between clicks, store them in a property variable declared at the top of your script. Any time you set the variable in your script, it will store that variable in the script level, rather than in the handler level… where it will remain until your change it or quit the app. Make sure you enter the name of the window containing the password field where it says “YOUR_WINDOW_NAME_HERE”. You may also need to use the ‘quoted form of’ switch when sending your shell script. Anywhere spaces or awkward characters may be in the string sent to the shell, try using something like…

(quoted form of source_file)

Good luck,
j

That worked great, Thanks a million. I had been racking my brain for days. I knew it had to be something simple.

BTW, I did not have to alter the do shell script command at all.

Thanks,
Chris