2

I've been messing around with this cool AppleScript scripting language and I have yet another question. How to make an AppleScript app locate itself?

Like something like this (that obviously doesn't work):
get info of myself set variable to "location"

So the output, "variable," would be for instance /home/Desktop/MyEpicApp.app.

Is there any script or a sequence of them to get the location of the app and store it in a variable?

Many thanks in advance, even a tiny bit of help is appreciated.

3
  • without testing - & based an only a vague Applescript ken... 'path to myself' or 'POSIX path to myself'. Applescript definitely understands 'me' to mean this script or this compiled app. Never tested how far that stretches.
    – Tetsujin
    May 3, 2016 at 18:10
  • Try: set thePath to POSIX path of (path to me as text) May 3, 2016 at 18:17
  • @user3439894 Works perfectly, it's exactly what I want. You can post it as an answer and I'll accept it. Thanks! May 3, 2016 at 18:21

2 Answers 2

3

To set the path of an AppleScript script or app to a variable use the following code:

set thePath to POSIX path of (path to me as text)

Example:

set thePath to POSIX path of (path to me as text)
display dialog "The path to me is: " & thePath

enter image description here

If you want the path up to the script or app set to a variable use the following code:

set thePath to POSIX path of ((path to me as text) & ":")

Example:

set thePath to POSIX path of ((path to me as text) & ":")
display dialog "The path to me is: " & thePath

enter image description here

2
0

To set a variable to the Posix-style path of the current script that you gave as an example:

set thePath to POSIX path of (path to me) as text

Scripts are stored as files, but apps are stored as bundles (better known in the Unix/POSIX world as directories), so the exact result depends on whether what's running was saved as a script (.scpt file) or app (.app bundle).

Let's say I'm user "gh" and I keep my scripts in a subdirectory of my home directory named Scripts.

Running in a script saved as foo.scpt, the above statement would set thePath to

/Users/gh/Scripts/foo.scpt

Running as foo.app, it would set thePath to

/User/gh/Scripts/foo.app/

Note the trailing slash.

If I want to load a library from the same place as the current script, there are a few ways to get just the needed part of the path, but the easiest is:

set my_root to POSIX path of (((path to me) as text) & "::")
set my_lib to load script POSIX file (my_root & "lib.scpt")

Whether the script is saved as an .scpt or a .app, my_root always receives

/Users/gh/Scripts/

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .