finder - update date last opened when opening app

tell application "TextEdit"
	activate
end tell

-using this it opens the application but it doesn’t update the “date last opened” column field.

  • However if you use the UI and double click and open the application it updates the “date last opened” field to the corresponding app.

Is there a way to update the “date last opened” through apple script?

You can use the shell, which changes the date last opened in Finder on Monterey:

do shell script "open -a TextEdit"

The following will also do the job.

-- revised 2022.07.06
use AppleScript version "2.7"
use framework "AppKit"
use framework "Foundation"
use scripting additions

set appPath to POSIX path of (path to application "TextEdit")
set appURL to current application's class "NSURL"'s fileURLWithPath:(appPath)
set theWorkspace to current application's NSWorkspace's sharedWorkspace()
set startOptions to current application's NSWorkspaceOpenConfiguration's configuration()
set startOptions's activates to true
theWorkspace's openApplicationAtURL:appURL configuration:startOptions completionHandler:(missing value)

thanks! that worked.

Hi peavine.

I can confirm that your shell script also works on my Mojave system. With regard to the ASObjC one:

Does the path to application command not still work?

set appAlias to (path to application "TextEdit")

However, that said, the NSWorkspaceOpenConfiguration class and NSWorkspace’s openApplicationAtURL:configuration:completionHandler: method were introduced with macOS 10.15 and aren’t available in Mojave or earlier. There’s a launchApplicationAtURL:options:configuration:error: method that’s deprecated as from 10.15, but I’ve not yet looked into how to use that. The plain openURL: method isn’t deprecated in the Xcode 11.3.1 documentation and it brings TextEdit to the front, but it doesn’t update the “date last opened” column in the Mojave Finder.

If it were relevant in the light of the above, I’d also point out that the automatic bridging of aliases and files to NSURL only started with Mac OS 10.11 “El Capitan”. In the short time that AppleScriptObjC could be used in scripts before that, it was necessary to use the longer method:

use AppleScript version "2.3.1" -- Mac OS 10.9 "Mavericks" or later.
use framework "Foundation"
use scripting additions

set appPath to POSIX path of (path to application "TextEdit")
set appURL to current application's class "NSURL"'s fileURLWithPath:(appPath)

Thanks Nigel for the comments. For as long as I have been writing ASObjC scripts (perhaps two years), I’ve been negligent in not including a use-AppleScript-version statement. There’s no reason for this other than laziness. I’ve modified my script above to include your suggestions.

The on-line documentation used to include information in the upper-right corner as to the frameworks and AppleScript version required. A few months ago, Apple apparently changed this documentation, omitting that information, but I’ll look around to see if it’s available elsewhere. FWIW, the following is a link to the NSWorkspace documentation I’ve been using, which illustrates this point:

https://developer.apple.com/documentation/appkit/nsworkspace?language=objc

I tested my two script suggestions from post 2 (as revised) and my original ASObjC script, which I’ve included below. The timing results with all required frameworks in memory were:

ASObjC script from post 2 - 304 milliseconds
Shell script from post 2 - 43 milliseconds
ASObjC script below - 1 millisecond

My original ASOjbC script is:

-- old script
use framework "AppKit"
use framework "Foundation"
use scripting additions

set appAlias to "Macintosh HD:System:Applications:TextEdit.app:" as alias
set theWorkspace to current application's NSWorkspace's sharedWorkspace()
set startOptions to current application's NSWorkspaceOpenConfiguration's configuration()
set startOptions's activates to true
theWorkspace's openApplicationAtURL:appAlias configuration:startOptions completionHandler:(missing value)

But since you’re using AppKit stuff which wasn’t introduced until macOS 10.15, the minimum AppleScript version should reflect that. :slight_smile: The AppleScript version in macOS 10.14 is “2.7”. I don’t know if it’s the same or higher in 10.15.

PS.

The macOS version required seems to be included at the top of the entry for each individual item.

Thanks. I missed that.

This works! However is there a way to prevent the textedit window from appearing or being visible?

I have tried various approaches and the best I can achieve is the window only appearing before it is closed etc.

The following does what you want on my Ventura computer:

set theApp to "/System/Applications/TextEdit.app/"
do shell script "open -j " & quoted form of theApp

If the above doesn’t work, try the following:

use framework "AppKit"
use framework "Foundation"
use scripting additions

set theApp to "/System/Applications/TextEdit.app"
set theApp to current application's |NSURL|'s fileURLWithPath:theApp
set theWorkspace to current application's NSWorkspace's sharedWorkspace()
set startOptions to current application's NSWorkspaceOpenConfiguration's configuration()
set startOptions's hides to true
theWorkspace's openApplicationAtURL:theApp configuration:startOptions completionHandler:(missing value)

At least on Catalina v10.15.7, the Last Opened Date for Apple preinstalled applications remains “–” (that is, not initialized). Always, even when double-click the app manually.

Thanks KniazidisR. I tested my first script on my Ventura computer with a few other apps (Weather.app, TV.app, Stickies.app) that didn’t have a date last opened, and they now have a date last opened. So, things changed at some point.