Applescript 获取文件的上次打开日期

发布于 2024-12-01 23:26:20 字数 187 浏览 0 评论 0原文

我可以使用
将 modDate 设置为字符串形式的文件修改日期以获取文件的最后修改日期,以及
将 modDate 设置为字符串形式的文件的创建日期以获取文件的创建日期。

是否有类似 last opening date 的内容来获取文件上次打开的日期?

I can use
set modDate to the modification date of theFile as string to get the last modified date of a file, and
set modDate to the creation date of theFile as string to get the date when the file was created.

Is there anything like last opened date to get the date when the file was last opened?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

我ぃ本無心為│何有愛 2024-12-08 23:26:20

是的。有一个名为 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

Yes. There is a UNIX command called kMDItemLastUsedDate that returns the date the target item was last used.

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

However, this command doesn't return a literal date object. Instead, it returns a date object in ISO 8601:2004 format (YYYY-MM-DD HH:MM:SS) which, if you try to put date before it, you'll get a syntax error.

Here is the revised script:

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
又怨 2024-12-08 23:26:20

我遇到的问题是想要获取仅在聚光灯元数据中可用的日期(图像文件的内容创建日期“kMDItemContentCreationDate” - 我认为来自相机的原始日期)。所以我想出了这个;注意:为了我自己的清晰/强迫症,我使用了“复制”和“讲述”。 “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

i had the problem of wanting to get a date that was only available in the spotlight metadata (image file's content created date "kMDItemContentCreationDate" - original date from camera, i think). so i came up with this; note: i used "copy" and "tell" for my own clarity/ocd. there is an "as date" coercion for "do shell script" but it just gave me different errors. there is also simpler and better "awk"'s to do more/better things but the "- name " gives just the one mdls value you ask for.

(* gets the mdls value of "metaDate" ie one of the many available metadata dates
"qpImg" is the "quoted form of posix path" of some file
the awk strips it down to just the actual date/time string *)

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

(* takes mdls info dates in form "2012-01-19 14:37:38 -500" and makes it applescripty.
"inText" is a posix path that is converted to it's "quoted form" on the fly.
the "%x %r" is "standard numeric" date and 12hr time and can be coerced via "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'"

-- the two can be combined using 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
沉睡月亮 2024-12-08 23:26:20

没有纯粹的 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一个aliasfilePOSIX file 作为参数,例如:

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

返回。

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

在我的机器上

There isn't a pure AppleScript solution to obtain a file's last access date. Using a combination of the shell tools stat and date you can construct an AppleScript helper function that provides the last opened date:

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

The function uses the following helper function to convert the last opened time stamp which is returned as a ISO 8601 formatted string to an AppleScript date:

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

The function LastOpenedDate can be invoked with an alias, file or POSIX file as an argument, e.g.:

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

returns

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

on my machine.

夜司空 2024-12-08 23:26:20

上述解决方案在我的 Mac 上均无法正常工作。我决定找出答案——为什么?事实证明,mdls shell 命令读取结果时没有考虑正确的时区。或者,它需要一些额外的帮助才能做到这一点,我不知道。 (由于某种原因,不使用 mdls 的解决方案也无法正常工作)。

我必须找到一个 AsObjC 解决方案,它可以在任何时区和时间获取正确的上次打开日期(Finder 视图中的文件)。避免其他本地化或日期格式硬编码问题:

use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use scripting additions

set posixPath to POSIX path of (choose file)

set theURL to (current application's |NSURL|)'s fileURLWithPath:posixPath
set mdItem to current application's NSMetadataItem's alloc()'s initWithURL:theURL
(mdItem's valueForAttribute:"kMDItemLastUsedDate") as date

None of the above solutions work properly on my Mac. I decided to find out - why? It turned out that the mdls shell command reads the result without taking into account the proper time zone. Or, it needs some additional help to do it, I don't know. (The solution where mdls is not used also does not work correctly for some reason).

I had to find a AsObjC-solution that gets the correct Date Last Opened (of file in Finder view) in any time zone & avoids other localization or date format hardcoding issues:

use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use scripting additions

set posixPath to POSIX path of (choose file)

set theURL to (current application's |NSURL|)'s fileURLWithPath:posixPath
set mdItem to current application's NSMetadataItem's alloc()'s initWithURL:theURL
(mdItem's valueForAttribute:"kMDItemLastUsedDate") as date
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文