AppleScriptObjC – A More Comprehensive Touch Command
AppleScriptObjC – A More Comprehensive Touch Command
- Subject: AppleScriptObjC – A More Comprehensive Touch Command
- From: Christopher Stone <email@hidden>
- Date: Sat, 09 Jun 2018 23:43:10 -0500
Hey Folks,
On occasion I will use some old existing file as a template for something, and
when I do this I generally want the various date metadata reset to NOW().
I've used `touch` in the shell for this, but it is less than comprehensive and
cannot be depended upon to change the creation date.
I've also used Xcode's command line tool `SetFile`, but it too is less than
comprehensive.
So. I finally undertook the challenge of finding all the relevant keys for
AppleScriptObjC, and I think I've got them all in the appended script.
Enjoy.
--
Best Regards,
Chris
----------------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2018/06/09 22:06
# dMod: 2018/06/09 22:54
# Appl: Finder
# Task: A more comprehensive AppleScriptObjC replacement for the command line
utility `touch`.
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @ASObjC, @Finder, @AppleScriptObjC, @Touch
----------------------------------------------------------------
use AppleScript version "2.5"
use framework "Foundation"
use scripting additions
----------------------------------------------------------------
tell application "Finder" to set finderSelectionList to selection as alias list
if length of finderSelectionList = 0 then error "No files were selected in the
Finder!"
repeat with theItem in finderSelectionList
set thePath to POSIX path of theItem
set theDate to current date
set theURL to (current application's |NSURL|'s fileURLWithPath:thePath)
# Creation Date
set {resultOfCreationDate, theError} to (theURL's setResourceValue:theDate
forKey:(current application's NSURLCreationDateKey) |error|:(reference))
if resultOfCreationDate as boolean is false then error (theError's
localizedDescription() as text)
# Date Added
set {resultOfAddedToDirectoryDateKey, theError} to (theURL's
setResourceValue:theDate forKey:(current application's
NSURLAddedToDirectoryDateKey) |error|:(reference))
if resultOfAddedToDirectoryDateKey as boolean is false then error
(theError's localizedDescription() as text)
# Content Creation Date
set {resultOfContentCreationDate, theError} to (theURL's
setResourceValue:theDate forKey:(current application's
NSURLContentCreationDateKey) |error|:(reference))
if resultOfContentCreationDate as boolean is false then error (theError's
localizedDescription() as text)
# Content Modification Date -- also sets the file's modification date.
set {resultOfContentModificationDate, theError} to (theURL's
setResourceValue:theDate forKey:(current application's
NSURLContentModificationDateKey) |error|:(reference))
if resultOfContentModificationDate as boolean is false then error
(theError's localizedDescription() as text)
# Content Access Date
set {resultOfContentAccessDate, theError} to (theURL's
setResourceValue:theDate forKey:(current application's
NSURLContentAccessDateKey) |error|:(reference))
if resultOfContentAccessDate as boolean is false then error (theError's
localizedDescription() as text)
end repeat
# On my system this is required to get the changed file to update.
# Although it fails if more than one item is selected - until the window
decides it's good and ready.
tell application "Finder"
delay 0.75
update (theItem's parent as alias)
delay 0.75
update (theItem's parent as alias)
end tell
----------------------------------------------------------------
_______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users
This email sent to email@hidden