Count selected objects in Finder

Unless the last line in a handler is something like a log request, which has a side-effect, but no value.

( The value of the mostly recently evaluated expression is bound to the built-in name result, and in the absence of an explicit return, the function/handler returns the current value of result, if any … )

on test()
    2 + 2
    log result
end test

on run
    {test(), 2 + 2}
end run

As a footnote to this thread:

If you give someone a program, you will frustrate them for a day; if you teach them how to program, you will frustrate them for a lifetime.

David Leinweber

and related:

… features so clever that you stop thinking about your problem and start thinking about the clever features

in this reddit thread

1 Like

duplicate deleted

Hey @trych,

As @ComplexPoint says, there are instances where nothing will be returned.

But — as a rule-of-the-thumb you should always be aware of the possibility of a returned value and test for it.

set x to "some text"
return result

The “result” variable will tell you what that is.

Another rule-of-the-thumb is to always test AppleScripts using the Script Editor.app or preferably Script Debugger if you have it.

If you use the Script Editor then it’s a good idea to open the Event Log window before you run your script. You get a better sense of what the code is doing when you do this.

(Script Debugger has even better tools for data visualization.)

Otherwise AppleScript is just a magical black-box run from Keyboard Maestro.

-Chris