Executing code on a error

This is a pretty simple one… I think, anyways :stuck_out_tongue:

ell application "Finder" to set parttwo to (first folder of partone's container whose name is "%" & partone's name)

Currently, that only gets folders that BEGIN with %. Unfortunatly, not all of them do. Some end with the % instaed. I tried messing around with things like “Contains “%” and partone’s name” or “…whose name is “%” and partone’s name or partone’s name & “%””

Of course, I wouldn’t be here if that worked :stuck_out_tongue:

The question I have is what would command would I use to tell the script “if you can’t find a folder by this name, try looking for a folder by this name instead”?

Thank ya :slight_smile:
-Parrot

Maybe something like this:

tell application "Finder"
	try
		set parttwo to (first folder of partone's container whose name is "%" & partone's name)
	on error -- none of the first kind
		try
			set parttwo to (first folder of partone's container whose name is partone's name & "%")
		on error -- none of the second kind either
			display dialog "Folder Missing"
		end try
	end try
end tell

EDIT:I see that Adam answered sooner and better.

How about this?


tell application "Finder"
		
	if exists (first folder of partone's container whose name is "%" & partone's name) then
		
		set parttwo to (first folder of partone's container whose name is "%" & partone's name)
	else if exists (first folder of partone's container whose name is partone's name & "%") then
		
		set parttwo to (first folder of partone's container whose name is partone's name & "%")
		
	else
		
		display alert "No such folder exists."
		
	end if
	
	return parttwo
	
end tell
end

Not sure nested tries are any better than sequential ifs, CapJ, just a different way to do it.

Try something like this, Parrot:

tell application "Finder" to set parttwo to first folder of partone's container whose name contains "%" and name contains (get partone's name)

(Of course, you may still need some way of trapping the possibility that no such permutations exist. :))

Amazing that it’s the “get” before “partone’s name” that does it. Did you know it, or discover it, Kai, and why did you think to try it? (just so the rest of us won’t think we’re complete AHs)

What’s involved here amounts to rather more than just throwing in an explicit ‘get’, Adam (although that can sometimes help).

Compound, single-line statements demand even greater care and understanding than more conventional syntax forms. One common cause of confusion is to do with different AppleScript classes and whether or not they’re evaluated implicitly (through, for example, attributing them to a variable - or performing a supplementary operation, such as concatenation).

In this case, the expression partone’s name actually refers to a property (of the alias represented by the variable partone) - rather than to a text value. It needs to be evaluated before a meaningful text comparison can be made. How that evaluation is best invoked can depend on the context of the comparison[s] involved.

Had my example been written more conventionally, it might have looked something like this:

tell application "Finder"
	set nameone to partone's name
	set parttwo to first folder of partone's container whose name contains "%" and name contains nameone
end tell

This works, because the act of setting the variable nameone results in the evaluation of partone’s name property. So the one-line version, which omits the ‘set’ (and therefore the implicit ‘get’), requires an explicit ‘get’.

Some initial confusion may also have arisen about the phrasing of multiple comparisons. I suppose the English-like nature of the language can sometimes lead us to assume that AppleScript might actually understand statements like [x is y or z] or [x contains y or z] - when we really need to supply two values for each comparison: [x is y or x is z] or [x contains y or x contains z].

That aside, you were all very close to coming up with a cleaner solution.

My suggestion would, of course, return the first item of folders with names like “%somename”, “somename%”, “% a variation of somename”, “another variation of somename%” - or even “These versions of somename don’t work 100% yet…”. However, if Parrot wanted to find only a folder named either “%somename” or “somename%”, then your suggestions were on the right track - except that they could work (as a one-liner) like this:

tell application "Finder" to set parttwo to first folder of partone's container whose name is "%" & partone's name or name is partone's name & "%"

So how does that get around the need for an explicit ‘get’?

Simply because both comparisons involve the concatenation of a text value (“%”) with the property partone’s name - which must therefore be evaluated to complete the operation.

Note, though, that this form makes more than one external Finder call, just to get the same value (partone’s name). A more efficient method would be to make just one call - and then store the result in another variable:

tell application "Finder"
	set nameone to partone's name
	set parttwo to first folder of partone's container whose name is "%" & nameone or name is nameone & "%"
end tell

As anyone who knows me can attest, I love a good one-liner. But sometimes, they can be more trouble than they’re really worth…

If in doubt, break it down. :slight_smile:

Thanks for taking the time to provide the thorough explanation.

j

So, two peanuts walk into a bar. One was a-salted.

Eh, eh?

Ok, i’m done…

Anywho, I ended up using Adam’s example, as it worked best for what I wanted to do. Thanks for everyones help, though :slight_smile:

thank you kai for that excellent explanation =)

I would actually like to throw this up there, since it’s the same script, and this has shown up just recently.

	if button returned of the result is "OK" then
				set disklist to list disks
				set pspstick to (choose from list disklist with title "Choose your PSP Memory Stick")
				set gameloc to ((pspstick as string) & ":PSP:GAME:")
				duplicate every item of folder gamefold to folder gameloc
			end if

It does this just fine. But for whatever reason, it errors, saying that “There is already a file by the same name”. Now, Overwriting is a possibility, but I don’t know how to do that, so I can use that if someone tells me (I used the search function on the forums, but to no avail). But what miffs me is that the test image I’m using (I used disk utility to create a small image to test moving stuff onto) is completely blank, except for the folders I’ve created, none of which share any name with any folders in my script…

The weird thing is that this just started happening, it worked fine earlier today, I just came across it as I was doing my final run-throughs. I thought something may have gone corrupt, so I deleted that image and created a new one. Same problem.

Any ideas?

Each to his own, I suppose, Parrot. However, for the benefit of anyone else following this, the only real difference (result-wise) between that and the final version suggested above is (as I mentioned earlier) an existence test. That’s easily added - and is significantly more efficient than using multiple try statements:

set f to choose folder
tell application "Finder"
    set n to f's name
    tell (first folder of (get f's container) whose name is "%" & n or name is n & "%")
        if not (exists) then tell me to return display dialog "Folder missing."
        set r to it
    end tell
end tell
(* if it exists, do something with r *)

Try using the duplicate command’s replacing parameter:

duplicate every item of folder gamefold to folder gameloc with replacing

PH3;

Pay attention here; Kai’s version is substantially more efficient involving, as it does, only simple tests rather than setting up a double try block.

Unlike him, I simply didn’t think of it.

PS: Thanks for the explanation too, Kai. It fits lots of situations where evaluating something as a variable and using it in another statement works, but putting the same evaluation without the “set” in a one-liner won’t. Happens to me all the time, so now there’s another arrow in the quiver.

I actually went with your version since it made the most sense as to how to customize it.

That said, for grins, I gave Kai’s version a try. The difference in ‘thinking’ time was noticable. (~ .5 second) so I think I might stick with that :stuck_out_tongue:

Now then, I’m almost done, just having an issue with my selector script, and apple events timing out. It for whatever reason, doesn’t pay attention to quit after the run command…

tell application "Finder"
	set thefold to ((path to me as string) & "Contents:Resources:PSPapps:") as alias
	set theapp to (name of every file of folder thefold)
	set runapp to (choose from list theapp as list with prompt "What do you want to do?")
	set openapp to ("" & thefold & runapp) as alias
	run script openapp
tell application "OSX_PSPkit"
quit
end tell
end tell

Once I sort that out, I’ll be good to go! (of course, I’m gonna try to actually sort this one out myself :P)

Thanks for all your help guys :smiley:

Getting more dangerous by the day, Adam… :wink: (Good to know some of you found it helpful.)

Just one or two observations, Parrot. If the OSX_PSPkit application is actually your selector script (and you haven’t saved it as a stay-open app), you shouldn’t need an explicit quit at all. Once the script has been executed, the app should quit of its own accord. It would also be worth adding a statement to trap cases where the user might hit the Cancel button in the choose from list dialog.

Here’s the kind of thing I mean, along with a couple of other suggested tweaks (one of which should allow the Finder tell statement to be dispensed with):

set thefold to (path to me as Unicode text) & "Contents:Resources:PSPapps:"
set theapp to list folder thefold without invisibles
set runapp to (choose from list theapp with prompt "What do you want to do?")
if runapp is false then return
run script file (thefold & runapp)