AppleScript path relative to script location

元气小坏坏 提交于 2019-12-08 13:02:44

问题


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?


回答1:


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



回答2:


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


来源:https://stackoverflow.com/questions/30988539/applescript-path-relative-to-script-location

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!