Handlers and Properties in Applescript

Hi Everyone,
I am making a script that basically starts by asking a question with buttons:


set the_button to button returned of (display dialog "Welcome to P2Pixie, would you like to use the FTP and Mailing Function?" buttons {"Yes", "No"} default button "Yes")
if the_button is "Yes" then 

property new_foldername : "PROXY"
set this_folder to (choose folder with prompt "Select the main shoot folder")
set foldername to name of (info for this_folder)
set folderPOSIXpath to POSIX path of this_folder
set tries to 1
set validextension to false

---coerce illegal characters to legal
--Store the current TIDs. To be polite to other scripts.
set previousDelimiter to AppleScript's text item delimiters
set potentialName to foldername as text
set legalName to {}
set illegalCharacters to {"/", ":", ","} --Whatever you want to eliminate.
set legalcharacter to {"-"}
--Now iterate through the characters checking them.
repeat with thisCharacter in the characters of potentialName
	set thisCharacter to thisCharacter as text
	if thisCharacter is not in illegalCharacters then
		set the end of legalName to thisCharacter
	else if thisCharacter is in illegalCharacters then
		set the end of legalName to legalcharacter
	end if
end repeat

---There is more action

if the_button is "No" then
--Insert the other code
property new_foldername : "PROXY"
set this_folder to (choose folder with prompt "Select the main shoot folder")
set foldername to name of (info for this_folder)
set folderPOSIXpath to POSIX path of this_folder
set tries to 1
set validextension to false

---coerce illegal characters to legal
--Store the current TIDs. To be polite to other scripts.
set previousDelimiter to AppleScript's text item delimiters
set potentialName to foldername as text
set legalName to {}
set illegalCharacters to {"/", ":", ","} --Whatever you want to eliminate.
set legalcharacter to {"-"}
--Now iterate through the characters checking them.
repeat with thisCharacter in the characters of potentialName
	set thisCharacter to thisCharacter as text
	if thisCharacter is not in illegalCharacters then
		set the end of legalName to thisCharacter
	else if thisCharacter is in illegalCharacters then
		set the end of legalName to legalcharacter
	end if
end repeat

end if

--This is where the "yes" button would active the FTP upload

So, here is the problem. I basically want to use the “yes” button to activate an action to create a file and then upload the file to an FTP site.

I have the FTP part figured out, but the code I want to insert has some property statements in the beginning. However, when I do this, it seems to bypass the beginning code and go straight to the property action. Any thoughts or solutions of how to fix this?

Thank you so much

Property at the top?

Hi Richard,
I did put all the properties at the top and got an error. I will have to do some more research on this. I was thinking of making a handler to put the action including property calls in, but didn’t really work. I will keep experimenting. Thank you so much.

Boyd

Definitely ‘property’ at the top, not inside an ‘if’ statement. ‘Property’ declarations are handled when the script’s compiled. ‘If’ statements are executed at run time. You can change an already-declared property’s value at run time if you like:

set new_foldername to "SOMETHING ELSE"

. but there’s probably no point in using a property if you do that in your case.

Thank you for your help, I will try that.

This is sort of the followup to this, and I will try to explain it as best I can.

Lets say I have an action I want to run and lets call it:
Action1

I also start with a question.

If the question answered is “yes”,
then I want to run Action 1 and
I want to run Action 2

if the answer is “no”
I just want to run Action 1

Is there a way to write this so I won’t have to code the action twice? Is this just using a handler, I guess?

Thank you.

You can do this without a handler


set {button returned:buttonReturned} to display dialog "answer Yes or No" buttons {"Cancel", "No", "Yes"}
-- start action 1

say "action 1"

-- end action 1

if buttonReturned is "No" then return

-- start action 2

say "action 2"

-- end action 2

Or:


set {button returned:buttonReturned} to display dialog "answer Yes or No" buttons {"Cancel", "No", "Yes"}
-- start action 1

say "action 1"

-- end action 1

if buttonReturned is "Yes" then
	
	-- start action 2
	
	say "action 2"
	
	-- end action 2
	
end if

-- Any further business.

If you really wanted to set up sub-routines, this would be the form:

property new_foldername : "PROXY"

set the_button to button returned of (display dialog "Welcome to P2Pixie, would you like to use the FTP and Mailing Function?" buttons {"Yes", "No"} default button "Yes")

if the_button is "Yes" then
	my Action_1()
	my Action_2()
end if

if the_button is "No" then
	my Action_1()
end if

--Add anything here that should happen at the end, regardless of whether the button is yes or no



--Sub-Routines

on Action_1()
	--do stuff part 1
end Action_1

on Action_2()
	--do stuff part 2
end Action_2

Model: iMacintel
AppleScript: xCode 3.0
Browser: Safari 525.20.1
Operating System: Mac OS X (10.5)