如何获取文件容器文件夹的路径?
How do I get the path of the container folder of a file?
我想将文件所在文件夹的路径传递给命令make new file at path
。我知道如何获取当前打开文件的路径(tell application "TexShop" to set thePath to get path of document 1
),但我不知道如何获取包含文件夹的路径。
我试过使用 container of
,像这样:
tell application "TeXShop"
set thePath to get container of path of document 1
end tell
.
但是我得到这个错误:
TeXShop got an error: Can’t make container of path of document 1 into type reference.
这是因为虽然容器命令在应用程序 Finder 中是允许的,但它不适用于应用程序 TeXShop?
以下 AppleScript 代码将获取最前面应用程序中最前面文档的路径及其包含文件夹的路径。这也将在包含文件夹中创建新文件。
(* The Delay Command Gives You Time To Bring The Desired App To The Front
Mainly For Use While Testing This Code In Script Editor.app *)
delay 5 -- Can Be Removed If Not Needed
tell application (path to frontmost application as text) to ¬
set documentPath to (get path of document 1) as POSIX file as alias
tell application "Finder" to set containingFolder to container ¬
of documentPath as alias
tell application "Finder" to make new file at containingFolder
我想将文件所在文件夹的路径传递给命令make new file at path
。我知道如何获取当前打开文件的路径(tell application "TexShop" to set thePath to get path of document 1
),但我不知道如何获取包含文件夹的路径。
我试过使用 container of
,像这样:
tell application "TeXShop"
set thePath to get container of path of document 1
end tell
.
但是我得到这个错误:
TeXShop got an error: Can’t make container of path of document 1 into type reference.
这是因为虽然容器命令在应用程序 Finder 中是允许的,但它不适用于应用程序 TeXShop?
以下 AppleScript 代码将获取最前面应用程序中最前面文档的路径及其包含文件夹的路径。这也将在包含文件夹中创建新文件。
(* The Delay Command Gives You Time To Bring The Desired App To The Front
Mainly For Use While Testing This Code In Script Editor.app *)
delay 5 -- Can Be Removed If Not Needed
tell application (path to frontmost application as text) to ¬
set documentPath to (get path of document 1) as POSIX file as alias
tell application "Finder" to set containingFolder to container ¬
of documentPath as alias
tell application "Finder" to make new file at containingFolder