Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

J. J.

macrumors regular
Original poster
Oct 15, 2012
122
9
Hi. This is part of the code of an AppleScript:

Code:
set filetypeword to choose from list {"1", "2", "3", "4", "5", "6", "7"} with prompt "XXXXX" with title "XXX" default items 1 without empty selection allowed
if filetypeword is equal to "1" then
	set filetype to "l"
else if filetypeword is equal to "2" then
	set filetype to "f"
else if filetypeword is equal to "3" then
	set filetype to "d"
else if filetypeword is equal to "4" then
	set filetype to "l -type f"
else if filetypeword is equal to "5" then
	set filetype to "l -type d"
else if filetypeword is equal to "6" then
	set filetype to "d -type f"
else if filetypeword is equal to "7" then
	set filetype to "l -type f -type d"
end if
do shell script "XXXXX" & filetype & "XXXXX"

Once I run it I get error "Variable 'filetype' is not defined". Can you help me please? Is there another way to do this? Thank you.
 
The result from choose from list is either a list (whether or not multiple selections are made or allowed), or the boolean false (if canceled).
 
Scope of variables in Applescript?

Hi. This is part of the code of an AppleScript:

Code:
set filetypeword to choose from list {"1", "2", "3", "4", "5", "6", "7"} with prompt "XXXXX" with title "XXX" default items 1 without empty selection allowed
if filetypeword is equal to "1" then
	set filetype to "l"
else if filetypeword is equal to "2" then
	set filetype to "f"
else if filetypeword is equal to "3" then
	set filetype to "d"
else if filetypeword is equal to "4" then
	set filetype to "l -type f"
else if filetypeword is equal to "5" then
	set filetype to "l -type d"
else if filetypeword is equal to "6" then
	set filetype to "d -type f"
else if filetypeword is equal to "7" then
	set filetype to "l -type f -type d"
end if
do shell script "XXXXX" & filetype & "XXXXX"

Once I run it I get error "Variable 'filetype' is not defined".

I have barely done anything with Applescript, but coming from a C++ background, I would have immediately said that by defining filetype within the if/elseif clauses, it is not scoped outside.

Can you help me please? Is there another way to do this? Thank you.

Copy and pasting your code, I get the same error, defining filetype before-hand to some value on the first line it works for me.

The Apple Developer page, though, seems to suggest that there is not a C++-like variable scope in Applescript.

Code:
set filetype to "-1"

Maybe that helps.

-drsoong
 
...coming from a C++ background, I would have immediately said that by defining filetype within the if/elseif clauses, it is not scoped outside.
AppleScript doesn't have the same scoping inside statement blocks. The main problem is the comparisons of a string to a list, which will fail, although if you define the variable beforehand it will act as a fall-through or default.

The posted script is a bit redundant though, and can be shortened to something like:
Code:
set commandList to {"l", "f", "d", "l -type f", "l -type d", "d -type f", "l -type f -type d"}
set filetypeword to (choose from list commandList with prompt "XXXXX" with title "XXX" default items {"l"} without empty selection allowed) as text
if filetypeword is not "false" then do shell script "echo " & filetypeword -- or whatever
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.