为什么设置文件夹和文件的路径在 AppleScript 中不起作用?
Why does setting a path of folder and file does not work in AppleScript?
脚本正在从位于文件夹中的文件中获取属性,该文件大部分时间位于最前面,但并不总是最前面的 window,例如桌面。但是当脚本检查文件是否存在时,尽管文件存在,但总是会抛出错误。实际上,仅当文件从某个位置放入 FileMaker 容器时才会触发脚本。所以它就在那里!
它归结为“将 fileExists 设置为 (containerName & theFile),因为别名存在”给出了“: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 window 中显示 POSIX 路径,但通常 Finder window 的名称就是 - 打开的文件夹的名称。 Finder window 的 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"
脚本正在从位于文件夹中的文件中获取属性,该文件大部分时间位于最前面,但并不总是最前面的 window,例如桌面。但是当脚本检查文件是否存在时,尽管文件存在,但总是会抛出错误。实际上,仅当文件从某个位置放入 FileMaker 容器时才会触发脚本。所以它就在那里!
它归结为“将 fileExists 设置为 (containerName & theFile),因为别名存在”给出了“: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 window 中显示 POSIX 路径,但通常 Finder window 的名称就是 - 打开的文件夹的名称。 Finder window 的 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"