【问题标题】:Applescript get last opened date of fileApplescript获取文件的最后打开日期
【发布时间】:2011-08-27 12:03:16
【问题描述】:

我可以使用
set modDate to the modification date of theFile as string 获取文件的最后修改日期,并使用
set modDate to the creation date of theFile as string 获取文件的创建日期。

有没有类似last opened date 的东西来获取文件上次打开的日期?

【问题讨论】:

    标签: macos file applescript


    【解决方案1】:

    是的。有一个名为 kMDItemLastUsedDate 的 UNIX 命令返回目标项目的最后使用日期。

    set the Last_opened_date to (do shell script "mdls -name kMDItemLastUsedDate " & quoted form of the POSIX path of theFile)
    

    但是,此命令不返回文字日期对象。相反,它会返回一个 ISO 8601:2004 格式 (YYYY-MM-DD HH:MM:SS) 的日期对象,如果您尝试将 date 放在它前面,则会出现语法错误。

    这是修改后的脚本:

    property months : {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"}
    
    set the Last_opened_date to date convert_date(do shell script "mdls -name kMDItemLastUsedDate " & quoted form of the POSIX path of theFile)
    
    on convert_date(passed_data)
        set prevTIDs to AppleScript's text item delimiters
        set AppleScript's text item delimiters to space
        set the ISO_date to the first text item of (passed_data as string)
        set AppleScript's text item delimiters to "-"
        set the date_parts to every text item of the ISO_date
        set the_year to the first text item of the date_parts
        set the_month to the second text item of the date_parts
        set the_day to the third text item of the date_parts
        set AppleScript's text item delimiters to space
        set the time_string to the second text item of (passed_data as string)
        set AppleScript's text item delimiters to prevTIDs
        return item (the_month as integer) of months & the_day & ", " & the_year & space & the time_string
    end convert_date
    

    【讨论】:

    • 修改后的脚本不起作用。我认为有几个错误,例如遗漏空格等。
    【解决方案2】:

    我想获得一个仅在聚光灯元数据中可用的日期(图像文件的内容创建日期“kMDItemContentCreationDate”-我认为来自相机的原始日期)的问题。所以我想出了这个;注意:为了我自己的清晰度/ ocd,我使用了“复制”和“告诉”。 “do shell script”有一个“as date”强制,但它只是给了我不同的错误。还有更简单更好的“awk”来做更多/更好的事情,但“-name”只给出了你要求的一个 mdls 值。

    (* 获取“metaDate”的 mdls 值,即许多可用的元数据日期之一 “qpImg”是某个文件的“引用形式的 posix 路径” awk 将其剥离为实际的日期/时间字符串 *)

    tell current application to copy (do shell script ("mdls " & " -name " & metaDate & " " & qpImg & " | awk -F ' ' '/Date/ {print $3,$4};'")) to targDate
    

    (* 以“2012-01-19 14:37:38 -500”的形式获取 mdls 信息日期,并使其具有适用性。 “inText”是一个 posix 路径,可以即时转换为它的“引用形式”。 "%x %r" 是 "标准数字" 日期和 12 小时时间,可以通过 "as date" *)

    tell current application to set formtdDate to do shell script "date -j -f '%Y-%m-%d %H:%M:%S' " & quoted form of inText & " +'%x %r'"
    

    -- 两者可以使用 xargs 组合

    tell current application to copy (do shell script ("mdls -name " & metaDate & " " & qpImg & " | awk -F ' ' '{print $3,$4};' | xargs -0 -I indate date -j -f '%Y-%m-%d %H:%M:%S' indate +'%x %r'")) to targDate
    

    【讨论】:

      【解决方案3】:

      没有纯粹的 AppleScript 解决方案来获取文件的最后访问日期。使用 shell 工具 stat 和 date 的组合,您可以构造一个提供上次打开日期的 AppleScript 辅助函数:

      on LastOpenedDate(theFile)
          set theStr to do shell script "date -r $(stat -f %a " & quoted form of (POSIX path of theFile) & ") +%Y-%m-%dT%H:%M:%S"
          ISODateStrToDate(theStr)
      end LastOpenedDate
      

      该函数使用以下帮助函数将作为ISO 8601 格式化字符串返回的最后打开的时间戳转换为AppleScript 日期:

      on ISODateStrToDate(theStr)
          set dt to (current date)
          set savedDelimeters to AppleScript's text item delimiters
          set AppleScript's text item delimiters to {"-", "T", ":"}
          set {dt's year, dt's month, dt's day, dt's hours, dt's minutes, dt's seconds} to (every text item of theStr)
          set AppleScript's text item delimiters to savedDelimeters
          return dt
      end ISODateStrToDate
      

      函数LastOpenedDate可以用alias、file或POSIX file作为参数调用,例如:

      LastOpenedDate(POSIX file "/var/log/system.log")
      

      返回

      date "Saturday, August 27, 2011 4:04:52 PM"
      

      在我的机器上。

      【讨论】:

      • 这是修改当前日期!如果 OP 想在程序的其他地方引用当前日期怎么办?
      • 不能修改current date。
      • 对...忘记了。对不起! :S
      • 抱歉,我花了这么长时间才解决这个问题...我正在做其他事情,从来没有时间完成我开始的清理脚本。
      猜你喜欢
      • 1970-01-01
      • 2019-02-26
      • 2010-09-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-21
      相关资源
      最近更新 更多