applescript - 在 AppleScript 中使用 POSIX 表示法检查项目是否作为文件夹存在
问题描述
我有一个字符串,它以 POSIX 表示法表示文件夹中的文件/文件夹:
/Users/surfacedetail/Desktop/Temp/Test1
我想检查 Test1 是否存在并且是一个文件夹。以下说明它存在:
if exists my POSIX file myFileOrFolder then
beep
end if
这工作正常,但如何判断它是文件夹还是文件?
问题是这是一个庞大脚本的一部分,这是一个更好的测试,它使用系统事件,但它不起作用。我认为这与我如何构建路径字符串有关。
tell application "Finder"
set myScriptFilePath to (path to me)
set mySourceFolder to folder of myScriptFilePath
set myFileAliasList to the entire contents of mySourceFolder
set myPOSIXSourceFolder to URL of mySourceFolder
set myPOSIXSourceFolder to characters 8 thru -1 of myPOSIXSourceFolder as string
repeat with myFile in myFileAliasList
set myFileName to name of myFile
log "myFileName: " & myFileName
if (myFileName ends with ".txt") then
set AppleScript's text item delimiters to "."
set myFolderName to first text item of myFileName
set myFolderPath to myPOSIXSourceFolder & myFolderName as string
log "myFolderPath: " & myFolderPath
tell application "System Events"
if not (exists folder myFolderPath) then
beep
display dialog "Could not find Folder for File: " & myFileName
return {}
end if
end tell
end if
end repeat
end tell
解决方案
你可以在System Events
没有任何类型转换的情况下做到这一点
set thePath to "/Users/surfacedetail/Desktop/Temp/Test1"
tell application "System Events"
if exists folder thePath then
beep
end if
end tell
推荐阅读
- python - Pandas:从具有混合字符串浮点值的列中去除空格
- c# - 从两个不同的视图绑定到数据源。MVVM、WPF
- python - 从python的子进程连续调用shell命令并解析输出
- vba - VBA - 基于单元格数据通过 Outlook 发送电子邮件
- sql - 在 oracle 中取一个大集合的第一个唯一值
- python - numpy.polyfit 数据导出到 Python pandas.core.frame
- python-3.x - 如何让请求继续尝试连接到 url,而不管它在列表中从哪里中断?
- python - Django 2 个不同的模型字段项
- python - Python 3.6转换NTP服务器查询函数
- python - 为一行代码编写一个函数?