简体   繁体   中英

AppleScript path relative to script location

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?

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM