何时在 AppleScript 中使用字符串、别名、POSIX 路径

问题描述 投票:0回答:1

我是 AppleScript 新手,但仍然对何时使用字符串、别名或 POSIX 路径来寻址文件或文件夹感到困惑。

例如,在下面的脚本中,我理解

readmePath
只是一个连接字符串,可能看起来像
path/to/folder/README.md
。对于
do shell script "touch " & quoted form of POSIX path of readmePath
行似乎也是如此,但如果我删除
POSIX path of 
它不起作用。

为什么我需要在这里转换为 POSIX,即使该操作看起来只是两个字符串的串联?

tell application "Finder"
    set currentFolder to target of front window as string
    set readmePath to currentFolder & "README.md"
    
    if not (exists file readmePath) then
        do shell script "touch " & quoted form of POSIX path of readmePath
        reveal file readmePath
    else
        display dialog "README.md already exists in this folder."
    end if
end tell
applescript
1个回答
0
投票

如果您的字符串已经看起来像

path/to/folder
,那么您不需要的POSIX路径。

但是,传统的 Apple 路径是

:
分隔的,而不是
/
分隔的。如果您有
path:to:folder
,则
POSIX path of
会将其转换为
/
分隔形式(同时更改以
:My Volume Name:
开头的内容以正确以
/Volumes/My Volume Name
或其他适当的安装点开头)。


请注意,在任何情况下,引用的形式对于避免注入攻击都是必要的,并且在处理来自比您自己的手写和手工审查的代码更不可信的任何地方的路径时不应被忽略。

© www.soinside.com 2019 - 2024. All rights reserved.