微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

为什么在 AppleScript 中设置文件夹和文件的路径不起作用?

如何解决为什么在 AppleScript 中设置文件夹和文件的路径不起作用?

该脚本正在从驻留在文件夹中的文件获取属性,该文件夹大部分时间位于最前面的窗口,但并不总是位于最前面的窗口,例如桌面。但是当脚本检查文件是否存在时,这总是会引发错误,尽管文件在那里。实际上,仅当文件从某个位置放入 FileMaker 容器时才会触发脚本。所以它就在那里!

归结为“将 fileExists 设置为 (containerName & theFile) as alias exists”给出了“:Desktoptext2.txt”,这似乎是构建路径的问题。

1) 下面的代码有什么问题? 2) 是否有其他方法可以获取在评估时位置未知的文件的某些属性

property theFile : "text2.txt"

tell application "Finder"
    try
        -- Gets The Name Of The Front Finder Window If There Is One Opened
        set containerName to name of front Finder window as POSIX file as text
        -- Checks If The File "text2.txt" Exists In That Folder
        -- fileExists Will Be Set To True Or False Depending On The Results
        set fileExists to (containerName & theFile) as alias exists
    on error errMsg number errNum
        -- If Either Of The PrevIoUs Commands Throws An Error
        -- This Will Give You An Option To Choose A Folder Where You Think
        -- The File "text2.txt" Is Located
        activate
        set containerName to my (choose folder with prompt "Choose A Folder") as text
    end try
    try
        -- Checks If The File "text2.txt" Exists In The New Chosen Folder
        set fileExists to (containerName & theFile) as alias exists
    on error errMsg number errNum
        -- If "text2.txt" Does Not Exist In This Folder Either,Script Stops Here
        return
    end try
    delay 0.1
    -- If fileExists Is Set To True From PrevIoUs Commands 
    if fileExists then
        set fullFilePath to (containerName & theFile) as alias
        set {creation date:creaDate,modification date:modDate,name:fName,displayed name:dName,name extension:nExt,description:descript,URL:fPath} to properties of fullFilePath
        set theText to creaDate & "#" & modDate & "#" & fName & "#" & ¬
            dName & "#" & nExt & "#" & descript & "#" & fPath
        tell current application to (do shell script "echo " & theText & ¬
            ">> $HOME/Desktop/FileProperties.txt")
    end if
end tell

解决方法

虽然可以设置首选项以在 Finder 窗口中显示 POSIX 路径,但通常 Finder 窗口的名称就是 - 打开它的文件夹的名称。 Finder 窗口的 target 属性是它的文件引用,所以最好使用它并强制转换到所需的路径。

在与 Finder 打交道时,它可能会因 POSIX 内容而变得愚蠢,因此最好避免这种情况,并在需要时强制执行。别名也可以用作 exists 测试的快捷方式,因为如果项目不存在,强制转换将失败:

property fileName : "text2.txt"

try
    tell application "Finder" to set containerName to target of front Finder window as text
    set filePath to (containerName & fileName) as alias
on error errMsg -- no file or Finder window
    log errMsg
    activate
    try
        set containerName to (choose folder with prompt "Choose A Folder") as text
        set filePath to (containerName & fileName) as alias
    on error errMsg -- no file or cancel
        log errMsg
        return
    end try
end try
log filePath
tell application "Finder" to set {creation date:creaDate,modification date:modDate,name:fName,displayed name:dName,name extension:nExt,description:descript,URL:fPath} to properties of filePath
set theText to creaDate & "#" & modDate & "#" & fName & "#" & ¬
    dName & "#" & nExt & "#" & descript & "#" & fPath
do shell script "echo " & theText & " >> $HOME/Desktop/FileProperties.txt"

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。