AppleScript current script’s path

Posted in AppleScript on October 13, 2016 · 1 min read
How can I get the current AppleScript's path?

In AppleScript I prefer to script repetitive tasks so that I can be more efficient in my work load and I use two approaches to running my scripts. The first approach is create a directory and leave the created AppleScript in said directory and when I drop files in that directory I can open the AppleScript and run it.The second approach is to create a script and add it to the Scripts Folder and run it through the menu.

To run a script in a directory we need to create the directory. On the Desktop create a folder and call it test. When you have the new folder test opened, open the Script Editor and save your script inside of the directory test called pathTest.scpt. In the script create a tell block for Finder:

tell application "Finder"

end tell

once the tell block is created we will create a variable setting the current path of the script to it:

tell application "Finder"
    set scriptPath to path to me
end tell

Result
alias "Macintosh HD:Users:username:Desktop:test:pathTest.scpt"

if you wanted the POSIX path of the script try:

tell application "Finder"
    set scriptPath to POSIX path of (path to me)
end tell

Result
"/Users/username/Desktop/test/pathTest.scpt"

Since we can get the full script path we can also just get the directory path of the script:

tell application "Finder"
    set currentPath to container of (path to me) as alias
end tell

Result
alias "Macintosh HD:Users:username:Desktop:test:"

if you forget the code as alias you will get a result of:

tell application "Finder"
    set currentPath to container of (path to me)
end tell

Result
folder "test" of folder "Desktop" of folder "username" of folder "Users" of startup disk of application "Finder"

well we have the Alias of the directory path but what if we wanted the Posix path:

tell application "Finder"
    set currentDir to POSIX path of (container of (path to me) as text)
end tell

Result
"/Users/username/Desktop/test/"

Another approach to getting the directory script path is:

tell application "Finder"
    set currentPath to POSIX path of ((path to me as text) & "::")
end tell

Result:
"/Users/username/Desktop/test/"

To execute our script from the AppleScript menu bar icon you should make sure the icon is enabled, reference Show AppleScript icon in menu bar. If the icon is enabled I prefer to tweak my code to prompt me for the directory location to run my script on:

The above script was placed in the AppleScript's main scripts directory located in the Library, selected from the AppleScript drop down and the test folder we created on our Desktop was chosen.

Help the site go ad free:
Storm Trooper