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.
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.
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
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.
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
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.