我刚刚偶然发现了编程语言中最尴尬的行为之一。
猜猜这个AppleScript在做什么:
set workspace to "tmp"
set folder1 to "08_0000012_11"
set folder2 to "8_12_11"
tell application "Finder"
if (not (exists folder folder1 of (workspace as alias))) then
make new folder at workspace as alias with properties {name:folder1}
end if
if (not (exists folder folder2 of (workspace as alias))) then
make new folder at workspace as alias with properties {name:folder2}
else
log "Folder already exists " & folder2
end if
end tell
应该在/tmp目录下创建两个文件夹08_0000012_11和8_12_11,对吗?…错了!它创建第一个,并声明另一个已经存在。但事实并非如此!
它似乎试图对这些名字应用一些逻辑。把它们分成3个数字,忽略零。请告诉我这背后有什么合理的解释。
这是Finder中的一个bug。它应该只在对文件名排序时忽略前导零,在比较文件名时忽略而不是。提交一个bug报告
相同的逻辑在System Events中正确工作,例如:
set fname to "001"
tell application "System Events"
if not exists folder fname of desktop folder then
make new folder at desktop folder with properties {name:fname}
end if
end tell