How to Get List of Recent Items?

I rarely leave a script as is more than 5 days so don’t be surprised if I post a new version.

I understood that I may ask the system to make the link between an apple ID and the name of the application.
Given that I was able to allow us to choose among a list of true applications names.
Here I have one case of file which have no available application name so I treat this sort of case but most of us will never see that at work.

use AppleScript version "2.5"
use framework "Foundation"
use scripting additions

property |⌘| : a reference to current application

Germaine()

on Germaine()
	set begOfPath to (path to application support from user domain as text) & "com.apple.sharedfilelist:"
	set begOfPathLong to begOfPath & "com.apple.LSSharedFileList.ApplicationRecentDocuments:"
	
	tell application "Finder" to set theFiles to name of files of folder begOfPathLong
	
	set theApps to {}
	repeat with anItem in theFiles
		set appID to text 1 thru -6 of anItem
		try
			set appName to name of (application id appID)
		on error
			set appName to appID
		end try
		set end of theApps to appName
	end repeat
	# I disabled FavoriteVolumes because selecting it urged the system to open numerous dmg files.
	set specialCases to {"RecentApplications", "RecentDocuments", "FavoriteItems"} # , "FavoriteVolumes"
	set theChoices to specialCases & theApps
	
	set appName to choose from list theChoices
	if appName is false then error number -128
	
	set appName to appName's item 1
	if appName is in specialCases then
		set recentDocumentsPath to POSIX path of (begOfPath & "com.apple.LSSharedFileList." & appName & ".sfl2")
	else
		repeat with i from 1 to count theApps
			if appName = item i of theApps then
				set recentDocumentsPath to POSIX path of (begOfPathLong & item i of theFiles)
				exit repeat
			end if
		end repeat
	end if
	
	try
		set plistData to |⌘|'s NSData's dataWithContentsOfFile:recentDocumentsPath
		set recentDocuments to |⌘|'s NSKeyedUnarchiver's unarchiveObjectWithData:plistData
		set documentPaths to {}
		repeat with aDocument in (recentDocuments's objectForKey:"items")
			set documentBookmark to (aDocument's objectForKey:"Bookmark")
			set {documentURL, resolveError} to (|⌘|'s NSURL's URLByResolvingBookmarkData:documentBookmark options:0 relativeToURL:(missing value) bookmarkDataIsStale:(missing value) |error|:(reference))
			if resolveError is missing value then
				set end of documentPaths to documentURL's |path|() as string
			end if
		end repeat
		
		documentPaths as list
	on error
		error "The selected application is not treated or has no sfl2 associated file"
	end try
	
end Germaine

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) mercredi 11 septembre 2019 21:17:06

Hi! I came across this thread because I am looking for a way to parse the recent items of Affinity Designer 2. There seems to be a bug that deletes the items from the sfl2 file. But there is a very similar-looking file located here:

~/Library/Application Support/Affinity Designer 2/mru.dat

How would I need to modify the script to make it work? Here is a look inside that file:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
	<key>$archiver</key>
	<string>NSKeyedArchiver</string>
	<key>$objects</key>
	<array>
		<string>$null</string>
		<dict>
			<key>$class</key>
			<dict>
				<key>CF$UID</key>
				<integer>7</integer>
			</dict>
			<key>NS.objects</key>
			<array>
				<dict>
					<key>CF$UID</key>
					<integer>2</integer>
				</dict>
			</array>
		</dict>
		<dict>
			<key>$class</key>
			<dict>
				<key>CF$UID</key>
				<integer>6</integer>
			</dict>
			<key>RecentDocumentInfo.Access.Date</key>
			<dict>
				<key>CF$UID</key>
				<integer>4</integer>
			</dict>
			<key>RecentDocumentInfo.Bookmark</key>
			<dict>
				<key>CF$UID</key>
				<integer>3</integer>
			</dict>
			<key>RecentDocumentInfo.Pinned</key>
			<false/>
		</dict>
		<data>
		Ym9va8ACAAAAAAQQMAAAAAmELqouS7lYiG4o5L2Cz5G+qd5dmpc/pbI4SNbn
		KteQvAEAAAQAAAADAwAAAAgAKAUAAAABAQAAVXNlcnMAAAAGAAAAAQEAAGhp
		c2NoYQAABwAAAAEBAABEZXNrdG9wAA4AAAABAQAAcGhpbmQuYWZkZXNpZ24A
		ABAAAAABBgAAEAAAACAAAAAwAAAAQAAAAAgAAAAEAwAAnlgAAAAAAAAIAAAA
		BAMAAP6gAwAAAAAACAAAAAQDAAAciwQAAAAAAAgAAAAEAwAAQEiSCAAAAAAQ
		AAAAAQYAAHAAAACAAAAAkAAAAKAAAAAIAAAAAAQAAEHFAoYmdC7AGAAAAAEC
		AAABAAAAAAAAAA8AAAAAAAAAAAAAAAAAAAAIAAAABAMAAAIAAAAAAAAABAAA
		AAMDAAD1AQAACAAAAAEJAABmaWxlOi8vLw0AAAABAQAATWFjaW50b3NoIFNT
		RAAAAAgAAAAEAwAAAFChG3MAAAAIAAAAAAQAAEHE7Eu9AAAAJAAAAAEBAAA1
		QTc4MzBFRS00QTVBLTQ5NTUtQTcwNi0wMkE4MkVERDIyMkMYAAAAAQIAAIEA
		AAABAAAA7xMAAAEAAAAAAAAAAAAAAAEAAAABAQAALwAAAAAAAAABBQAAzAAA
		AP7///8BAAAAAAAAABAAAAAEEAAAWAAAAAAAAAAFEAAAsAAAAAAAAAAQEAAA
		2AAAAAAAAABAEAAAyAAAAAAAAAACIAAAqAEAAAAAAAAFIAAAFAEAAAAAAAAQ
		IAAAJAEAAAAAAAARIAAAXAEAAAAAAAASIAAAPAEAAAAAAAATIAAATAEAAAAA
		AAAgIAAAiAEAAAAAAAAwIAAAtAEAAAAAAAABwAAA+AAAAAAAAAARwAAAIAAA
		AAAAAAASwAAACAEAAAAAAAAQ0AAABAAAAAAAAAA=
		</data>
		<dict>
			<key>$class</key>
			<dict>
				<key>CF$UID</key>
				<integer>5</integer>
			</dict>
			<key>NS.time</key>
			<real>705844686.606498</real>
		</dict>
		<dict>
			<key>$classes</key>
			<array>
				<string>NSDate</string>
				<string>NSObject</string>
			</array>
			<key>$classname</key>
			<string>NSDate</string>
		</dict>
		<dict>
			<key>$classes</key>
			<array>
				<string>RecentDocumentInfo</string>
				<string>NSObject</string>
			</array>
			<key>$classname</key>
			<string>RecentDocumentInfo</string>
		</dict>
		<dict>
			<key>$classes</key>
			<array>
				<string>NSMutableArray</string>
				<string>NSArray</string>
				<string>NSObject</string>
			</array>
			<key>$classname</key>
			<string>NSMutableArray</string>
		</dict>
	</array>
	<key>$top</key>
	<dict>
		<key>root</key>
		<dict>
			<key>CF$UID</key>
			<integer>1</integer>
		</dict>
	</dict>
	<key>$version</key>
	<integer>100000</integer>
</dict>
</plist>

The script in post #9 of this thread doesn’t list the .sfl or .sfl2 file for your application? Are you sure? This file has binary data and your .dat file has text (XML) format.

.dat files are created by applications for use by the application itself and will not be easily decrypted. Especially not having this paid app on your computer. Perhaps one of the experienced users has it to help with the problem.

It does list the file for Affinity Designer 2. But due to a bug that file does not have any recent bookmarks once you quit the application. The bug has been reported several months ago. No fix yet.

If you open one of the .sfl2 files in:

~/Library/Application Support/com.apple.sharedfilelist/com.apple.LSSharedFileList.ApplicationRecentDocuments

In a text editor like BBEdit you will see that it is very similar to the .dat file.

I guess @StefanK or @Shane_Stanley would know if it’s possible or not.

Here is the error I get when running the script for the .dat file:

**error** "*** -[NSKeyedUnarchiver decodeObjectForKey:]: cannot decode object of class (RecentDocumentInfo) for key (NS.objects) because no class named \"RecentDocumentInfo\" was found; the class needs to be defined in source code or linked in from a library (ensure the class is part of the correct target). If the class was renamed, use setClassName:forClass: to add a class translation mapping to NSKeyedUnarchiver" number -2700

I guess that is bad news.

Your key is recentDocumentInfo
Class is NSObject