我最近一直在使用Apple Automator App来帮助我按顺序保存文件结构和文件名。
我找到了一个漂亮的脚本,以YYYY-MM-DD格式输出今天的日期并替换所选的文本。这很酷,因为我的文件和文件夹的基本数据结构是 YYYY-MM-DD-name-of-project 。
现在,我希望能够将提到的日期重命名为最后修改日期,以便跟踪我编辑的内容。 (基本上允许我只选择上一个日期,点击键盘快捷键调用服务脚本并覆盖它。)
我已经尝试了一些方法,但我似乎无法完成它。
非常感谢帮助!非常感谢你提前。 (我希望这不是重复。)
干杯!
如果您需要我正在讨论的脚本:
on todayISOformat()
set theDate to current date
set y to text -4 thru -1 of ("0000" & (year of theDate))
set m to text -2 thru -1 of ("00" & ((month of theDate) as integer))
set d to text -2 thru -1 of ("00" & (day of theDate))
return y & "-" & m & "-" & d
end todayISOformat
on run {input, parameters}
return todayISOformat()
end run
答案 0 :(得分:2)
此脚本根据以下两条规则重命名任何选定的文件:
YYYY-MM-DD-
开头,则会将此前缀替换为文件当前修改日期的日期。YYYY-MM-DD-
开头,则会将文件的当前修改日期放在文件名前面。不考虑文件夹。
tell application "Finder"
repeat with anItem in (get selection)
if class of anItem is not folder then
set {currentFileName, modificationDate} to {name, modification date} of anItem
set formattedModificationDate to my formatDate(modificationDate)
if text 11 of currentFileName is "-" then
set newFileName to formattedModificationDate & text 11 thru -1 of currentFileName
else
set newFileName to formattedModificationDate & "-" & currentFileName
end if
set name of contents of anItem to newFileName
end if
end repeat
end tell
on formatDate(aDate)
tell aDate to tell 100000000 + day * 1000000 + (its month) * 10000 + year as string ¬
to return text -4 thru -1 & "-" & text 4 thru 5 & "-" & text 2 thru 3
end formatDate
更新:
脚本只检查第11个字符是否为连字符。如果需要检查整个YYYY-MM-DD-
模式,您可以使用此处理程序
on validateYYYYMMDD(aDateString) -- returns true on success
try
tell aDateString
(text 1 thru 4) as integer
(text 6 thru 7) as integer
(text 9 thru 10) as integer
return text 5 is "-" and text 8 is "-" and text 11 is "-"
end tell
on error
return false
end try
end validateYYYYMMDD
或者如果您安装了SatImage.osax,它提供了没有shell调用的正则表达式搜索
on validateYYYYMMDD(aDateString) -- returns true on success
try
find text "^\\d{4}-(\\d{2}-){2}" in aDateString with regexp
return true
on error
return false
end try
end validateYYYYMMDD
要使用处理程序,请替换
行if text 11 of currentFileName is "-" then
带
if my validateYYYYMMDD(currentFileName) then
答案 1 :(得分:1)
您可以使用«class isot»
将日期转换为iso日期时间字符串。你只需要从中获取1到10的字符,只获得iso日期字符串。
tell application "Finder"
repeat with anItem in (get selection)
if class of anItem is not folder then
set {fileName, modificationDate} to {name, modification date} of anItem
set isoDate to text 1 thru 10 of (modificationDate as «class isot» as string)
if text 11 of fileName is "-" then set fileName to text 11 thru -1 of fileName
set newFileName to isoDate & "-" & fileName
set name of contents of anItem to newFileName
end if
end repeat
end tell
答案 2 :(得分:0)
例如,选择一个文件并获取该文件的最后修改日期。
当然,您可以再次使用您的例程来格式化日期:
set MyFile to choose file
tell application "Finder" to set DateUpdate to modification date of MyFile
set StringToday to todayISOformat(current date)-- get the string value of current date
set StringModif to todayISOformat(DateUpdate)-- get the string value of modification's date
on todayISOformat(LDate)
set y to text -4 thru -1 of ("0000" & (year of LDate))
set m to text -2 thru -1 of ("00" & ((month of LDate) as integer))
set d to text -2 thru -1 of ("00" & (day of LDate))
return y & "-" & m & "-" & d
end todayISOformat