Use AppleScript instead of System Events to get name of script?

I’ve been rewriting my scripts for Catalina and trying to prevent various permission requests to appear - especially in scripts that (for various reasons) can’t be code signed or notarized.

Is there a pure AppleScript (including AsObjC) way of getting the name of the running script without using System Events?

I’ll probably have more questions like this, and I apologize in advance if they’re wasting bandwidth.

property name : "Fred"

return my name

:slight_smile:

Ah - I should have explained that my scripts test their name in order to decide which actions to perform.

I let the user change the name of the script in order to change its behavior. For example, if a script has “51” in its name, then it does different things from what it does if has “62” in its name.

Sorry to have left this unclear, and very grateful for any advice on testing the current name of the script file - not the name I originally saved it under.

Well there’s this in vanilla AS:

set pathToMe to path to me as text

set astid to AppleScript's text item delimiters
set AppleScript's text item delimiters to ":"
if (pathToMe ends with ":") then
	set myName to text item -2 of pathToMe
else
	set myName to text item -1 of pathToMe
end if
set AppleScript's text item delimiters to astid

myName

Or in ASObjC:

use AppleScript version "2.4"
use framework "Foundation"
use scripting additions

set pathToMe to POSIX path of (path to me)
set pathToMe to current application's class "NSString"'s stringWithString:(pathToMe)
set myName to pathToMe's lastPathComponent() as text

Perfect! Thank you! And the ASObjC version gives me another excuse to stop supporting older OS versions…

And also:

use framework "Foundation"
use framework "AppKit"
use scripting additions

current application's NSRunningApplication's currentApplication()'s localizedName() as text -- edited as below

That skips the extension, I think, which might make subsequent parsing a tad simpler in some cases.

Even better! That removes the need to

set myName to text 1 thru -5 of myName

I think currentApplication's should ideally be currentApplication()'s:

Shane’s code returns the name of the application running it, not necessarily the name of the script file. They’ll only be the same if the script’s running under its own steam as an applet.

My own ASObjC suggestion above can be made to lose the extension with an additional NSString method:

use AppleScript version "2.4"
use framework "Foundation"
use scripting additions

set pathToMe to POSIX path of (path to me)
set pathToMe to current application's class "NSString"'s stringWithString:(pathToMe)
set myName to pathToMe's lastPathComponent()'s stringByDeletingPathExtension() as text

Quite so.

Good point. I assumed from the context that @emendelson was talking about applets, but perhaps not.

Well he seems happy with your answer. :slight_smile:

I was happy with Shane’s answer, because it works with applets, and I could easily work around the fact that it didn’t produce the desired answer in Script Debugger. But Nigel’s method lets me get around that by working in SD as well as in applet.

Warmest thanks to everyone!