Get AppleScript Path of Parent Folder
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 "/".
Solution 1:
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
Solution 2:
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
.