【问题标题】:What logic and why is AppleScript applying to folder names? Guess what this script is doingAppleScript 应用于文件夹名称的逻辑和原因是什么?猜猜这个脚本在做什么
【发布时间】:2016-09-14 21:47:40
【问题描述】:

我只是偶然发现了我生活中编程语言最尴尬的行为之一。

猜猜这个 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 个数字并忽略零。请告诉我对此有一些合理的解释。或者这只会发生在我身上......

【问题讨论】:

  • 对我来说同样的行为。不是逻辑。要创建文件夹,我使用 shell 命令 'mkdir' :快得多,如果已经存在则不创建,并且工作正常!

标签: applescript directory finder


【解决方案1】:

这是 Finder 中的一个错误。它应该只在对文件名排序时忽略前导零,在比较它们时File a bug report.

相同的逻辑在系统事件中正常工作,例如:

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

【讨论】:

    猜你喜欢
    • 2011-03-18
    • 1970-01-01
    • 2011-08-26
    • 1970-01-01
    • 2016-01-24
    • 2019-12-08
    • 2016-08-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多