I am writing an AppleScript that will be run from a USB drive, but the dive name may change often so I need to figure out how to get the directory path of the parent folder that contains the script.

This gets me the entire directory with directory to the file, I need the directory of its parent folder:

set pPath to path to me as text
display dialog pPath

I came across this (which is what I need), but I need an AppleScript path, not a POSIX path!

set pPath to POSIX path of ((path to me as text) & "::")
display dialog pPath

When the script is run it should detect the directory path to the script, the first example of code does this, but it then needs to go up one folder to the parent folder which contains the script (the second example does this) but I need it converted from the POSIX path to Applescript path that uses ":" instead of "/".

  • I could be wrong however I think you're mixed up a bit in that it appears your using the term directory when in reality your mean path so I suggest you actually add some real world examples of each element you require because a presently written it is confusing and needs to be edited. – user3439894 Jan 24 '16 at 1:53

AppleScript is written in English. Read what it says! Remove the posix path of from the code, and you'll get the HFS path.

Full code:

tell application "Finder" to get (container of (path to me)) as text
| improve this answer | |
  • Yes, I understand that, however, I get the full path, not the parent path that holds the script. – ProGrammer Jan 24 '16 at 2:59
  • I updated it with what you need. – William T Froggard Jan 24 '16 at 3:01
  • The code you provided me with seems to work, however, when run from a USB stick, I get the error: Can't make <<class ctnr>> of alias "[path]" into type text. It works when the script is copied to the desktop, but I need to run it from a USB drive. – ProGrammer Jan 25 '16 at 7:25
  • @ProGrammer Well, what does your code look like now? – William T Froggard Jan 25 '16 at 11:54
  • It uses the code, from your answer, within a directory path. The USB stick has a folder that contains the project's main (exported application) script, along with a resource folder that holds the Installer files I wish to copy. It only works when the folder (main folder) is on the desktop, not when it is on a USB stick, but it should still detect its path (with your code), right? Here is my entire script for you to look at: justpaste.it/qwrf – ProGrammer Jan 27 '16 at 4:59

Try this:

Unix style:

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

If you need to pass it to e.g. shell script, you might want to enclose the path with quotes:

set my_POSIX_path to quoted form of POSIX path of ((path to me as text) & "::")

macOS style:

set my_path to (((path to me as text) & "::") as alias) as string

Then depending on your needs, replace set my_path to / set my_POSIX_path to with display dialog or just return.

| improve this answer | |

You must log in to answer this question.

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