Functions within a "tell finder" ?

I have a script that is getting way too long… I tried breaking it up with functions (I’m not even sure if that’s what they’re called in applescript) – but I have problems now…

Is there a way to make a function at the end of a script “global” ? (again, probably wrong lingo)

Here’s an example of what I want to do, that doenst’ work :slight_smile:


tell application "Finder"
if exists startup disk then
say("This")
else
say("That")
end if
end tell
on say(value)
display dialog value
end say

I hope that’s readable… The problem (and I didnt’ actually try this script) is that I get “finder” doesnt’ understand “say()” or something like that… And I need to have these functions nested inside if statements that need to be inside a “tell finder” – I certainly hope some of that made sense to someone. :slight_smile:

Thanks for any help you can offer,
-Shawn

Shawn,

You were close. The term you are looking for ‘function’ is Handler, or Sub-routine and your syntax is close in the one you supplied. I only suggest changing a couple things.

–>1
Using say(this) as a subroutine name is kind of dicey as the finder wants to see that as the command “say”. Change your subroutine name to something that isn’t already a command listed somewhere. If you are using Script Debugger, or some other script editor other than Apple’s stock editor, you can change the look of your code so various components, like variables, application keywords, and operators can all have different looks, like bold, italic, diff colors. This is what made me notice the say command was being mistaken as a “say command” and not a handler name.

–>2
When calling a handler in your script you need to include some code so your script will know there is a chunk of code elsewhere in the script to run. You can do this 1 of 2 ways (maybe more, this is all I know of)

a) handlername(somevaluetopass) of me
b) my handlername(somevaluetopass

“of me” and “my” tells the script to run that handler

–>3
If you are going to pass a variable into your handler you have to set one first. I did that with "set myValue to “This” Now we can pass it to the handler.

tell application "Finder"
	if exists startup disk then
		set myValue to "This"
		my display(myValue)
	else
		set myValue to "That"
		my display(myValue)
	end if
end tell


on display(myValue)
	display dialog (myValue)
end display

Hope this helps - handlers were tough for me to figure out at first as I am more of a designer and not so much a programmer.

Best,

I think that you have a couple of issues at work here. First, ‘say’ is a command/verb that results in speech from the computer.

say "Hi trunkboy"

As far as nesting within tell blocks, this can be overcome by declaring possesion of the sub-routine (also referred to as a handler). For instance, using your example, with a variable to replace the ‘say’ command:

tell application "Finder"
	if exists startup disk then
		my sayPhrase("This")
	else
		my sayPhrase("That")
	end if
end tell

on sayPhrase(value)
	display dialog value
end sayPhrase

my = the script

Confused yet? :wink:

(edited to add multiple thank yous. :slight_smile: Thank you both!)

You’re really helping a beginner along! I thank you sincerely. I think I can make my many-page applescript quite a bit nicer now, and not have to copy/paste so much redundant code.

Oh – and the say(this) was a quicky example, really the functions updates a progress bar – and it’s like 4 lines of crazy code ever time I wanted to nudge the progress bar a bit.

You mentioned different editors, perhaps I’ll have to look for others. I didnt’ realize there were other options. Are they all commercial (ie cost)? I wonder if any work in OS 8.1…

Thanks again, you’re my current hero. :slight_smile:
-Shawn

If you check out the main page for this BBS, you’ll see a forum for script editors, with relevant links. Smile is free and very powerful - the rest are commercial (other than Apple’s Script Editor). I think all are available for OS 8 but I can’t say for sure. When recommending a commercial script editor, I always choose Script Debugger. It has a great set of features and it is constantly updated and improved. :slight_smile: