我有一些Applescript代码通过KEYSTROKE转换到"转到"的路径切换到指定目录。对话框。
遗憾的是,如果请求的目录不存在,此对话框会不引发错误?!它只是尽可能沿着路径走,然后将不存在的路径碎片映射到其下任何窗口的默认文本字段!
示例:给出"〜/音乐/不存在"它将切换到用户主文件夹中的音乐文件夹,然后键入' "不存在"进入底层窗口的默认文本字段(如果有)。
因此,我需要知道如何从Applescript中找出给定路径是否已经存在。
答案 0 :(得分:1)
在AppleScript中,您只需通过强制转换alias
说明符的路径来检查路径是否存在。如果路径不存在,则抛出错误
此外,您必须以编程方式扩展代字号。处理程序返回一个布尔值。
on checkPathExists(thePath)
if thePath starts with "~" then set thePath to POSIX path of (path to home folder) & text 3 thru -1 of (get thePath)
try
POSIX file thePath as alias
return true
on error
return false
end try
end checkPathExists
set pathisValid to checkPathExists("~/Music/nonexistent")
或者使用System Events
,但发送Apple事件要贵一些:
set thePath to "~/Music/nonexistent"
tell application "System Events" to set pathisValid to exists disk item thePath
答案 1 :(得分:0)
这样的事情怎么办?
set theFolder to "path:to:the:Folder:" --your folder/path
if FolderExists(theFolder)=true then -- HERE YOU DON'T have to put exists again, it's already check in the handler FolderExists(theFolder)
display dialog "Exists " & theFolder & " !"
else
display dialog "Missing " & theFolder & " !"
end if
--handler for checking if exists theFolder/thePath
on FolderExists(theFolder) -- (String) as Boolean
tell application "System Events"
if exists folder theFolder then
return true
else
return false
end if
end tell
end FolderExists