Applescript 获取文件的上次打开日期
我可以使用将 modDate 设置为字符串形式的文件修改日期
以获取文件的最后修改日期,以及将 modDate 设置为字符串形式的文件的创建日期
以获取文件的创建日期。
是否有类似 last opening date
的内容来获取文件上次打开的日期?
I can useset modDate to the modification date of theFile as string
to get the last modified date of a file, andset 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
是的。有一个名为
kMDItemLastUsedDate
的 UNIX 命令,它返回目标项目上次使用的日期。但是,此命令不会返回文字日期对象。相反,它返回 ISO 8601:2004 格式 (YYYY-MM-DD HH:MM:SS) 的日期对象,如果您尝试在其前面放置
date
,您将收到语法错误。这是修改后的脚本:
Yes. There is a UNIX command called
kMDItemLastUsedDate
that returns the date the target item was last used.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:
我遇到的问题是想要获取仅在聚光灯元数据中可用的日期(图像文件的内容创建日期“kMDItemContentCreationDate” - 我认为来自相机的原始日期)。所以我想出了这个;注意:为了我自己的清晰/强迫症,我使用了“复制”和“讲述”。 “do shell script”有一个“as date”强制,但它只是给了我不同的错误。还有更简单、更好的“awk”可以做更多/更好的事情,但“-name”只给出您要求的一个 mdls 值。
(* 获取“metaDate”的 mdls 值,即许多可用元数据日期之一
“qpImg”是某个文件的“posix路径的引用形式”
awk 将其剥离为实际的日期/时间字符串 *)
(* 采用“2012-01-19 14:37:38 -500”形式的 mdls 信息日期并使其成为苹果脚本。
“inText”是一个 posix 路径,会动态转换为其“引用形式”。
“%x %r”是“标准数字”日期和 12 小时时间,可以通过“as date”强制 *)
—— 可以使用 xargs 将两者组合起来
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 *)
(* 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" *)
-- the two can be combined using xargs
没有纯粹的 AppleScript 解决方案来获取文件的上次访问日期。使用 shell 工具的组合 stat 和 date 您可以构建一个提供上次打开日期的 AppleScript 辅助函数:
该函数使用以下辅助函数来转换上次打开的时间戳,该时间戳以 ISO 8601 格式的字符串转换为 AppleScript 日期:
可以使用以下命令调用函数
LastOpenedDate
一个alias
、file
或POSIX file
作为参数,例如:返回。
在我的机器上
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:
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:
The function
LastOpenedDate
can be invoked with analias
,file
orPOSIX file
as an argument, e.g.:returns
on my machine.
上述解决方案在我的 Mac 上均无法正常工作。我决定找出答案——为什么?事实证明,mdls shell 命令读取结果时没有考虑正确的时区。或者,它需要一些额外的帮助才能做到这一点,我不知道。 (由于某种原因,不使用 mdls 的解决方案也无法正常工作)。
我必须找到一个 AsObjC 解决方案,它可以在任何时区和时间获取正确的上次打开日期(Finder 视图中的文件)。避免其他本地化或日期格式硬编码问题:
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: