How can I extract my script's file name and location?

By using “path to me” and AppleScript’s text item delimiters, you can obtain both the name and path of the script.

It’s important to note that the sample code provided below may not produce the desired result when run with a script editor. Under some circumstances, the path result will be the path to the script editor and the name will be the name of the script editor, since path to me returns the full path to the folk who is running the script. Saving and running the script as an application should result in the desired behavior.

Note: The “display dialog” is used in this example strictly to show the results of the script, and is for educational purposes only. It can be commented out or removed without affecting the rest of the script.

set myPath to (path to me) as string
display dialog myPath -- for test purposes only

try
	set oldDelims to AppleScript's text item delimiters
	set AppleScript's text item delimiters to {":"}
	set myName to last text item of myPath
	set AppleScript's text item delimiters to oldDelims
on error
	set AppleScript's text item delimiters to oldDelims
end try
display dialog myName -- for test purposes only