AppleScript handler problem (doubtless a beginner's idiocy)

I suspect I am doing that which I should not be doing: I have dived into scripting with AppleScript. While far too shy to show the entire script (which is effectively assembling a markdown document) I have run aground on one aspect and, after many days’ battling, have finally plucked up the courage to reveal more of my ignorance here.

In a nutshell, I need DT to create one, two or three tabs at various places in the document header. With the overconfidence of a beginner I thought I might write a handler. So we have this:

on aTab(num)
	set aTab to ""
	repeat num times
		set aTab to aTab & "    "
	end repeat
	return aTab
end aTab

together with this (somewhat emasculated for brevity):

tell application id "DNtp"
	try
		activate
		[miss lots of bits which work well]
		set theContent to return & my aTab(1) & "Date:" & my aTab(3) & recordName & " " & "at " & theTime & nextLine & my aTab(1) & "The weather: " & my aTab(2) & theWeather & nextLine & my aTab(1) & "Location:" & my aTab(2) & myLocation & nextLine
		[miss more working bits and some error trapping]
	end try
end tell

When I run the script from DT I obtain the error :<<script>>> doesn’t understand the “aTab” message. I am entirely sure I have made some completely idiotic beginner’s error and if someone would be kind enough to point it out very gently I’d be most grateful.

Stephen

Use either local at the beginning …

on aTab(num)
	local aTab
	set aTab to ""
	repeat num times
		set aTab to aTab & "    "
	end repeat
	return aTab
end aTab

… or use different names for the handler and the variable

on aTab(num)
	set theTabs to ""
	repeat num times
		set theTabs to theTabs & "    "
	end repeat
	return theTabs
end aTab

Great to see you’re learning AppleScript! :slight_smile:

Pete, that so kind and really appreciated. The whole script is actually not too bad (very immodest of me to say so) and I’ve learned so much in the process. It’s really good to have people like you here helping: thanks again!

Stephen

1 Like

I happily second @pete31’s praise. AppleScript is (literally :smiley: ) not rocket science, but it’s amazing how powerful it can be. And even if that powerful thing is just assembling a Markdown document for yourself, it always feels good to run the script and behold what you have wrought :wink: :heart:

PS: While local is indeed useful, I’d opt for different names unless using a local is something that makes sense to you.

1 Like

Thanks for all the encouragement. I much enjoy the discipline of quasi-programming (e.g., Alfred workflows and Hazel) and very many years ago wrote quite a lot in BBC Basic <blush> (so became accustomed to using functions and procedures). AppleScript by no means appears a completely foreign language. But, as demonstrated by this thread, there are some fundamental principles I need to understand rather better. :wink:

I’ll try to avoid running back here too often with script problems!

Stephen

1 Like