loop back to a certain section of code, depending on user input

Alrighty guys, I’ve got all my kinks for this script worked out, and figured out how I’m going to do what I want to do. There are just 2 more things I want to do which I cannot figure out. One is major, and I don’t want to release this without doing it, and the second is only minor, more cosmetic then anything.

first of all, my code

tell application "Finder"
	display dialog "This script will first rename your homebrew for 1.5 so that the 'corrupted data' icons do not show up, and then will move your game files to the correct folder, if you choose to do so." buttons {"OK", "Cancel"}
	if button returned of the result is "Cancel" then
		quit
	else
		if button returned of the result is "OK" then
			display dialog "First you will enter the name of your homebrew. This will not affect how it shows up on your PSP, it just allows you to find it on your Memory Stick. 
		Second, you will choose the folder with the game. 
		Third, you will choose the exploit folder, which is the folder with the % sign in its name"
			set brewname to text returned of (display dialog "What is this homebrew called?" default answer "")
			set partone to (choose folder with prompt "Choose the main folder" without invisibles) --get the name of the main folder
			set parttwo to (choose folder with prompt "Choose the second folder, with the % in front" without invisibles) --get the name of the secondary folder
			if partone is parttwo then
				display dialog "You cannot chose the same folder, please make sure to choose two different folders" buttons {"OK"}
				quit
			end if
			--**lets quickly set up the path to the games folder
			set gamefold to ((path to me as string) & ":Contents:Resources:Games:")
			set name of folder partone to ("__SCE__" & brewname)
			set name of folder parttwo to ("%__SCE__" & brewname)
			duplicate folder partone to gamefold
			duplicate folder parttwo to gamefold
		end if
	end if
	--a list in here somewhere to show what games have been moved to the games folder already, hopefully with a way that does not allow the user to select anything, just shows what is in the folder
	display dialog "Do you want to add more homebrew?" buttons {"Yes", "No"}
	--somehow, I need to have the script loop back to defining 'brewname', and continue this cycle until the user says they do not have any more homebrew to add
	display dialog "We are now going to get the disk with your memory stick. Please be sure your PSP is on, plugged into the computer, and is in USB mode before clicking OK" buttons {"OK"}
	if button returned of the result is "OK" then
		set disklist to list disks
		set pspstick to (choose from list disklist with title "Select your Memory Card")
		set gameloc to ((pspstick as string) & ":PSP:GAME:")
	end if
	--I'll add in the move commands once we have this repeat section working
end tell

Ok, my major point, which I’m hoping to find a way to solve, is this. When the script first runs, it goes through the steps, until it gets to the “Do you want to add more Homebrew” dialog. If the user selects yes, I want the script to loop back to defining the brewname variable. If no, it continues on to the end of the script. I, unfortunatly, don’t have much experence with loops and repeats, so I’m not too sure how to go with this.

A few cosmetic things I wanted to do, which I have tried several methods and not worked.

  1. Make the user only define one folder, since all games that are released have 2 folders, with the same name, in the same directory. (I.E- mygame, and %mygame). So instead of having to select folders twice, have the user select mygame, and then the script automatically finds %mygame in the same directory, and renames it correctly. (I.E- Mygame can be renamed through the varaible, but the script sees that there is another folder with ‘mygame’ in its name in the same directory, so it also gets that one, sees it has a % in its name, so it renames it differently). I don’t know if it can be done, but I figured I’d throw it out there.

The second, is how would I define a list not to allow user selections, so it just shows the contents of the list. (Or in this case, folder contents coorced into a list) When the user clicks OK, it just continues on, and doesn’t do anything with the list. (I’m also hoping to find a way to have the result of brewname be put into a list as well as being used in renaming the folder, so its easier to understand instead of folder names, but I don’t know if you can do two different things with one variable)

Once again, thanks for all your help :slight_smile: Hopefully, this thing will be done before long :stuck_out_tongue:

In answer to your first question (I haven’t altered your script; this is just an example of how it’s done):

repeat -- we have an exit clause later
	set FirstPath to (choose folder with prompt "Please select the  source directory")
	set SecondPath to (choose folder with prompt "Please select the  destination directory")
	if FirstPath is SecondPath then -- test that they aren't the same
		display dialog "You cannot choose the same directory for both the source and the destination.  Please try again."
		set FirstPath to "" -- set them back to blank for the next try
		set SecondPath to ""
	else
		set Q to button returned of (display dialog "Start over again?" buttons {"Yes", "No"} default button "No")
		if Q = "No" then exit repeat -- and if Yes, we go around again
	end if
end repeat
-- Done! press on with the rest

For your second “cosmetic” feature - displaying a list in a dialog, something like this:

set F to choose folder -- or just the folder name as alias from some previous action
tell application "Finder" to set N to name of every file in F
set NameList to "The files are" & return & return
repeat with aN in N
	set NameList to NameList & aN & return
end repeat
display dialog NameList buttons {"OK"} default button 1

I’m not sure I understand what you want for the first feature - is it guaranteed that there will only be one folder with % in it’s name present?

Thanks adam. As soon as I get a chance, I’ll implement those into my script. :smiley:

As for your question…

For the firmware I am writing for, 2 folders are REQUIRED to make a game run. For this example we’ll use a game called ‘mygame’

When I download it, there would be 2 folders inside the download folder. This is ALWAYS the case. 1 would be “mygame” and the second would be “%mygame”. These conventions are set in stone. (They will always be the same name, whatever the name of the game is, and one would have a % in the beginning of its name)

They are both required to run a game. The reason I rename them would be because normally, without renaming them, one of the two folders would show up as ‘corrupted data’ in the menu, and it cannot be deleted, otherwise the game would not work. When you have 10 or so games, that can get annoying.

What i’m trying to do is when I choose my first folder (mygame), I want the script to look in the same directory that mygame was found in, and find anything else containing the same name (in this case, %mygame). Then, assign that second result to a different variable, so it can be renamed. After that, its no problem. I just wanted to make the selection process as simple as possible, so that anyone can use it. (And, with things like PSP’s, <10 year olds might be using this :slight_smile:

Try something like this:

set Gfldr to choose folder -- this is the game folder, we want its % equivalent
tell application "Finder"
	set Gname to name of Gfldr -- easier to compare
	set outerFolder to container of Gfldr -- the download folder, need it to get the others
	set tPaths to every folder in outerFolder as alias list -- paths to every folder in the download folder
	set tNames to name of every folder in outerFolder -- names of the folders
end tell
-- Now look for the match:
repeat with k from 1 to count tNames -- can't use the "aFolder in...." form, need the count.
	tell item k of tNames -- so we can use "it" instead of repeating this
		if characters 2 thru -1 of it as string = Gname and character 1 of it = "%" then -- the test
			set PathToPctFile to item k of tPaths -- path to it
			exit repeat -- no need to look further
		end if
	end tell
end repeat
PathToPctFile

Another way to achieve a similar result might be:

set f to choose folder
tell application "Finder" to set p to (first folder of f's container whose name is "%" & f's name) as alias

As John Keats said: “A thing of beauty is a joy forever;” Inspired, even. /AB

While I admit that I left my version spread out so the OP would see where it was going, I would never have imagined reducing it to two lines.

It could just as easily have been a one-liner, Adam. :wink:

tell application "Finder" to tell folder (choose folder) to set p to (first folder of its container whose name is "%" & name) as alias

(However, since I figured the OP might want to use an existing variable, I resisted the impulse - and left the choose folder statement separate.)

Incidentally, while on the subject of alternative methods, perhaps I could throw in another couple of suggestions for listing a folder’s items (files and folders):

set f to choose folder (* or use an existing variable *)
set {d, text item delimiters} to {text item delimiters, return}
set {l, text item delimiters} to {"The folder contains:" & {"", "", list folder f without invisibles}, d}
display dialog l

Or even (succumbing to the irresistible temptation of another one-liner):

display dialog "The folder contains:" & return & return & (do shell script "ls " & quoted form of (choose folder)'s POSIX path)

I, of course couldn’t resist the attempt, but failed to recognize this bit: … (first folder of its container whose …; instead, I had it without recognizing that I was looking “inside it” not “next to” it.

This is the one I like. Using ASTIDs to add in the returns rather than cycling through the whole list in a repeat is indeed neat. Thanks for that, I’ll use it in several other contexts.

I try to avoid shell scripts unless they seem absolutely necessary, although, I must admit, in cases involving the Finder, perhaps even the one above, they often pay in speed as well as crispness of expression.

Generally, the list folder/TIDs technique would be my preferred approach, too. However, if there’s any serious lifting to be done, the shell method shouldn’t be ruled out.

While other permutations are possible, let’s compare the examples discussed:

The following is a brief summary of some typical results here, when tested on folders containing 10, 100, and 1000 items. (The figures represent execution times, expressed as an index: 100 = fastest in each test run.)

As ever, such things usually boil down to choosing the most appropriate tool for the job in hand… :slight_smile:

Thanks as always. ACB