OSX 10.6.8 Xcode 4.2更改创建日期和修改日期

时间:2016-02-19 03:00:52

标签: xcode shell applescript osx-snow-leopard

我一直在研究一个Applescript来改变特定文件的创建和修改日期(通过shell脚本)。具有手动输入日期的第一个脚本工作正常,但使用带有附加时间的(当前日期命令)的第二个脚本会抛出错误代码,即使两个脚本的输出看起来相同。任何帮助都将非常感激。

脚本#1

resetFile()
on resetFile()
    do shell script "SetFile -d '02/18/2016 00:01:00' ~/Desktop/Test.txt"
    do shell script "SetFile -m '02/18/2016 00:01:00' ~/Desktop/Test.txt"
end resetFile

脚本#2

resetFile()
on resetFile()
    set {year:y, month:m, day:d} to (current date)
    set {year:y, month:m, day:d} to result
    if (d * 1) < 10 then
        if (m * 1) < 10 then
            set dateTime to quoted form of ("0" & m * 1 & "/" & "0" & d * 1 & "/" & y * 1 & " " & "00:01:00") as string
    else
        set dateTime to quoted form of (m * 1 & "/" & "0" & d * 1 & "/" & y * 1 & " " & "00:01:00") as string
    end if
else
    if (m * 1) < 10 then
        set dateTime to quoted form of ("0" & m * 1 & "/" & d * 1 & "/" & y * 1 & " " & "00:01:00") as string
    else
        set dateTime to quoted form of (m * 1 & "/" & d * 1 & "/" & y * 1 & " " & "00:01:00") as string
    end if
end if
display dialog dateTime
do shell script "SetFile -d dateTime ~/Desktop/Test.txt"
do shell script "SetFile -m dateTime ~/Desktop/Test.txt"
end resetFile

Dialog dateTime

Error Code

1 个答案:

答案 0 :(得分:1)

这是一个更简单的版本来创建日期字符串。如果需要,它使用处理程序添加前导零:

resetFile()
on resetFile()
    set {year:y, month:m, day:d} to (current date)
    set dateTime to quoted form of (pad(m as integer) & "/" & pad(d) & "/" & y & space & "00:01:00")

    display dialog dateTime
    do shell script "SetFile -d " & dateTime & " ~/Desktop/Test.txt"
    do shell script "SetFile -m " & dateTime & " ~/Desktop/Test.txt"
end resetFile

on pad(v)
    return text -2 thru -1 of ((v + 100) as text)
end pad