AppleScript path relative to script location

2.1k views Asked by At

I'm trying to launch a Finder window of a folder that's in the same directory as my script. When I run the code below, it launches a blank Finder window set to the path of the script not to the folder.

tell application "Finder"
    tell application "Finder" to make new Finder window
    set file_path to (path to me) as text
    set target of Finder window 1 to file_path
end tell

How can I get the path to the folder of the script, not the script?

2

There are 2 answers

0
Craig Smith On

You were close. You do not need the text version of the file, you only need the file itself, then you can ask Finder for that file's container:

tell application "Finder"
    tell application "Finder" to make new Finder window
    set file_path to (path to me)
    set target of Finder window 1 to file_path's container
end tell
0
smizzlov On

The shortest way I know to do this is:

tell application "Finder" to open ((path to me as text) & "::")

Editing your script renders the following:

tell application "Finder"
    make new Finder window -- There is no need for an your second tell statement
    set file_path to (path to me as text) & "::" -- Goes up one directory
    set target of Finder window 1 to file_path
end tell