Wednesday, September 18, 2019

#1 2019-09-09 05:58:30 am

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 3577

How to get list of recent items

Infos about the recent documents are stored in : (path to application support from user domain as text) & "com.apple.sharedfilelist:com.apple.LSSharedFileList.RecentDocuments.sfl2"
Infos about the recent applications are stored in : (path to application support from user domain as text) & "com.apple.sharedfilelist:com.apple.LSSharedFileList.RecentApplications.sfl2"

How may we extract the names and/or the paths of the described recent items.

I found pieces of code which did that with old ways of storing these infos but none of them apply to the "new" one.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) lundi 9 septembre 2019  13:58:02

Offline

 

#2 2019-09-09 06:58:00 am

StefanK
Member
From:: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 11597
Website

Re: How to get list of recent items

Those files are binary property lists archived with NSKeyedArchiver. The URLs are security scoped bookmarks which can be resolved with NSURL API

This script extracts the URLs as string paths into a variable documentPaths

Applescript:


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

property |⌘| : a reference to current application

set recentDocumentsPath to POSIX path of (path to application support from user domain) & "com.apple.sharedfilelist/com.apple.LSSharedFileList.RecentDocuments.sfl2"
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|()
   else
       display dialog resolveError's localizedDescription as text
   end if
end repeat

Last edited by StefanK (2019-09-09 06:58:33 am)


regards

Stefan

Offline

 

#3 2019-09-09 08:26:37 am

KniazidisR
Member
Registered: 2019-03-03
Posts: 538

Re: How to get list of recent items

Thanks, Stefan, for this nice script.

I added to your script's end documentPaths to return it, and this is list of NSString paths in Script Debugger, and list of IDs in Script Editor. It is little strange for me.

So, to get as strings list always:

Applescript:

set end of documentPaths to documentURL's |path|() as string

Last edited by KniazidisR (2019-09-09 09:23:45 am)


macOS Mojave -- version 10.14.4
Safari -- version 12.1

Offline

 

#4 2019-09-09 12:48:47 pm

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 3577

Re: How to get list of recent items

Thank you StefanK and KniazidisR.

I added some instructions allowing us to define which recents items we want to get.

Applescript:

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

property |⌘| : a reference to current application

tell application "Finder"
   set cancelBtn to localized string "AL1" --> "Annuler"
   set OKBtn to localized string "AL4" --> "OK"
end tell
set begOfPath to (path to application support from user domain as text) & "com.apple.sharedfilelist:"
set item1 to "Documents"
set item2 to "Applications"
set item3 to "Choose an application"

set whatToDo to choose from list {item1, item2, item3} default items {item3}
if whatToDo is false then error number -128
set whatToDo to whatToDo's item 1
if whatToDo is item3 then
   set appName to name of (choose application)
   set appleApp to false
   if appName is in {"Keynote", "Numbers", "Pages"} then
       set appName to "Iwork." & appName
       set appleApp to true
   else if appName is "Script Editor" then
       set appName to "scripteditor2"
       set appleApp to true
   else if appName is "Imovie" then
       set appName to "imovieapp"
       set appleApp to true
   else if appName is "Xcode" then
       set appName to "dt.xcode"
       set appleApp to true
   else if appName is "QuickTime Player" then
       set appName to "quicktimeplayerx"
       set appleApp to true
   else if appName is "System Profiler???" then
       set appName to "systemprofiler"
       set appleApp to true
   else if appName is "BBEdit" then
       set appName to "com.barebones.bbedit"
   else if appName is "Libre Office" then
       set appName to "org.libreoffice.script"
   else if appName is "Script Debugger" then
       set appName to "com.latenightsw.scriptdebugger7"
   else if 5 = 6 then
       --
   end if
   if appleApp then
       set recentDocumentsPath to POSIX path of (begOfPath & "com.apple.LSSharedFileList.ApplicationRecentDocuments:com.apple." & appName & ".sfl2")
   else
       set recentDocumentsPath to POSIX path of (begOfPath & "com.apple.LSSharedFileList.ApplicationRecentDocuments:" & appName & ".sfl2")
   end if
else
   set recentDocumentsPath to POSIX path of (begOfPath & "com.apple.LSSharedFileList.Recent" & whatToDo & ".sfl2")
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
           --else
           --display dialog resolveError's localizedDescription as text
       end if
   end repeat
   
   documentPaths as list
on error
   error "The selected application is not treated or has no sfl2 associated file"
end try

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) lundi 9 septembre 2019  20:48:42

Added instructions required to treat particular cases of Apple applications and code required to treat three non apple applications.
Complete to fit your needs after looking at the contents of the folder :

((path to application support from user domain as text) & "com.apple.sharedfilelist:com.apple.LSSharedFileList.ApplicationRecentDocuments:")

Last edited by Yvan Koenig (2019-09-09 02:28:00 pm)

Offline

 

#5 2019-09-09 01:13:35 pm

KniazidisR
Member
Registered: 2019-03-03
Posts: 538

Re: How to get list of recent items

Ivan Koenig.

I replaced your code line:

Applescript:

set appName to display dialog "Define an application" default answer "Iwork.numbers" buttons {cancelBtn, OKBtn} default button OKBtn cancel button cancelBtn

with this:

Applescript:

set appName to name of (choose application)

But I can’t understand. It seems that separately for applications the code does not work anyway?

Update: I apologize - your code works just fine if the application has the recent documents at the moment. If they are not already, an error is thrown. That is, you need to put the last part of the code in a try block. Or, in the if block:

Applescript:

if recentDocuments ≠ missing value then
   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
           --else
           --display dialog resolveError's localizedDescription as text
       end if
   end repeat
end if

Last edited by KniazidisR (2019-09-09 01:31:08 pm)


macOS Mojave -- version 10.14.4
Safari -- version 12.1

Offline

 

#6 2019-09-09 01:25:27 pm

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 3577

Re: How to get list of recent items

I'm quite sure that itsn't a good idea.

If you select Numbers, you will get the name "Numbers" while the system requires "Iwork.numbers"

There is a workaround:

Applescript:

set appName to name of (choose application)
if appName is in {"Keynote", "Numbers", "Pages"} then set appName to "Iwork." & appName
if appName is "Imovie" then set appName to "imovieapp"
# Complete with non-Apple applications

Of course don't ask me for an explanation of this non-standard behavior.

Maybe it would be useful to add "IBooks" to the list of applications requiring to be "corrected".


PS: Since 1943/12/31, my first name is Yvan and I'm accustomed to it. Please, don't torture it!

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) lundi 9 septembre 2019  21:22:36

Last edited by Yvan Koenig (2019-09-09 01:48:54 pm)

Offline

 

#7 2019-09-09 01:58:46 pm

KniazidisR
Member
Registered: 2019-03-03
Posts: 538

Re: How to get list of recent items

Yvan Koenig wrote:

There is a workaround:


This workaround is better for me, as I don't like manual operations. So, thanks for your script. It goes to my Scripts library in this form:

Applescript:


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

property |⌘| : a reference to current application

set {item1, item2, item3} to {"Documents", "Applications", "Choose an application"}
set whatToDo to choose from list {item1, item2, item3} default items {item3}
if whatToDo is false then error number -128
set whatToDo to whatToDo's item 1

if whatToDo is item3 then
   set appName to name of (choose application)
   if appName is in {"Keynote", "Numbers", "Pages"} then set appName to "Iwork." & appName
   set recentDocumentsPath to POSIX path of ((path to application support from user domain as text) & "com.apple.sharedfilelist:com.apple.LSSharedFileList.ApplicationRecentDocuments:com.apple." & ¬
       appName & ".sfl2")
else
   set recentDocumentsPath to POSIX path of ((path to application support from user domain as text) & "com.apple.sharedfilelist:com.apple.LSSharedFileList.Recent" & whatToDo & ".sfl2")
end if

set plistData to |⌘|'s NSData's dataWithContentsOfFile:recentDocumentsPath
set recentDocuments to |⌘|'s NSKeyedUnarchiver's unarchiveObjectWithData:plistData
set documentPaths to {}

if recentDocuments ≠ missing value then
   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
       else
           display dialog resolveError's localizedDescription as text
       end if
   end repeat
end if

documentPaths

Last edited by KniazidisR (2019-09-09 02:07:36 pm)


macOS Mojave -- version 10.14.4
Safari -- version 12.1

Offline

 

#8 2019-09-09 02:15:13 pm

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 3577

Re: How to get list of recent items

I edited the script in message #4 so that it treat some non-standard Apple naming and show how to complete with non Apple products (I coded three examples).

I would be glad to know why many applications aren't proposed by choose application.
It's the case for many applications which I bought from the App Store and which are stored in a subfolder named "Applications_MAS" . Xcode or Stuffit Expander are proposed  but EasyDraw or EasyFind aren't.
Same behavior which many applications stored in a subfolder named "Applications perso". BBEdit or Script Debugger are proposed but Libre Office isn't.
Some applications stored in the Applications folder are missing too: Data Rescue.app, DiskMaker X 7 for High Sierra.app, GIMP-2.10.app, Malwarebytes.app are some of them

For info, here is a list of the sfl2 files dedicated to applications which are available on my SSD:

com.apple.console.sfl2
com.apple.dt.xcode.sfl2
com.apple.imovieapp.sfl2
com.apple.iphoto.sfl2
com.apple.iwork.keynote.sfl2
com.apple.iwork.numbers.sfl2
com.apple.iwork.pages.sfl2
com.apple.photos.sfl2
com.apple.preview.sfl2
com.apple.quicktimeplayerx.sfl2
com.apple.scripteditor2.sfl2
com.apple.systemprofiler.sfl2
com.apple.textedit.sfl2
com.autodesk.sketchbook.sfl2
com.barebones.bbedit.sfl2
com.barebones.textwrangler.sfl2
com.charlessoft.pacifist.sfl2
com.colliderli.iina.sfl2
com.etresoft.etrecheck4.sfl2
com.github.atom.sfl2
com.hankinsoft.osx.sqliteprofessional.sfl2
com.hp.customer.uploader.sfl2
com.latenightsw.scriptdebugger7.sfl2
com.mkanda.ocrtools.sfl2
com.ridiculousfish.hexfiend.sfl2
com.seriflabs.affinityphoto.sfl2
com.smileonmymac.pdfpenpro.sfl2
net.sourceforge.grandperspectiv.sfl2
org.clindberg.manopen.sfl2
org.libreoffice.script.sfl2
org.videolan.vlc.sfl2

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) lundi 9 septembre 2019  22:14:29

Last edited by Yvan Koenig (2019-09-09 02:52:36 pm)

Offline

 

#9 2019-09-09 10:43:09 pm

KniazidisR
Member
Registered: 2019-03-03
Posts: 538

Re: How to get list of recent items

Hi, Yvan.

The list above without extensions is list of CFBundleIdentifier. I don't know how, but is seems, this maybe used for automated getting required by system  names.

How can I convert application name to its CFBundleIdentifier?

Applescript:

set appName to name of (choose application)
set appName to id of application appName

I applyed this and... Hm, it works:

Applescript:


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

property |⌘| : a reference to current application

set {item1, item2, item3} to {"Documents", "Applications", "Choose an application"}
set whatToDo to choose from list {item1, item2, item3} default items {item3}
if whatToDo is false then error number -128
set whatToDo to whatToDo's item 1

if whatToDo is item3 then
   set appName to name of (choose application)
   set appName to id of application appName
   set recentDocumentsPath to POSIX path of ((path to application support from user domain as text) & "com.apple.sharedfilelist:com.apple.LSSharedFileList.ApplicationRecentDocuments:" & ¬
       appName & ".sfl2")
else
   set recentDocumentsPath to POSIX path of ((path to application support from user domain as text) & "com.apple.sharedfilelist:com.apple.LSSharedFileList.Recent" & whatToDo & ".sfl2")
end if

set plistData to |⌘|'s NSData's dataWithContentsOfFile:recentDocumentsPath
set recentDocuments to |⌘|'s NSKeyedUnarchiver's unarchiveObjectWithData:plistData
set documentPaths to {}

if recentDocuments ≠ missing value then
   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
       else
           display dialog resolveError's localizedDescription as text
       end if
   end repeat
end if

documentPaths

Last edited by KniazidisR (2019-09-10 01:20:40 am)


macOS Mojave -- version 10.14.4
Safari -- version 12.1

Offline

 

#10 2019-09-10 01:08:06 am

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 3577

Re: How to get list of recent items

Here is a new version which trigger only existing files.

Applescript:

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:"
   
   set specialCases to {"Applications", "Documents"}
   tell application "Finder"
       set theChoices to specialCases & name of files of folder begOfPathLong
   end tell
   
   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.Recent" & appName & ".sfl2")
   else
       set recentDocumentsPath to POSIX path of (begOfPathLong & appName)
   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

I built another version in which I use a list of apps derived manually from the list of available files.
I feel that a version which doesn't require some duty from the user is better.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) mardi 10 septembre 2019  09:07:24

Offline

 

#11 2019-09-10 01:36:55 am

KniazidisR
Member
Registered: 2019-03-03
Posts: 538

Re: How to get list of recent items

Thanks, Yvan. I tried your last version too. I liked it. Now, with our last posts your code has not 1 but 2 extremely cool solutions. Thanks again to Stefan for starting point. smile

Last edited by KniazidisR (2019-09-10 01:41:08 am)


macOS Mojave -- version 10.14.4
Safari -- version 12.1

Offline

 

#12 2019-09-10 05:58:19 am

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 3577

Re: How to get list of recent items

Thanks.

As I am pig headed, I repeat what I already wrote:

I would be glad to know why many applications aren't proposed by choose application.
It's the case for many applications which I bought from the App Store and which are stored in a subfolder named "Applications_MAS" . Xcode or Stuffit Expander are proposed  but EasyDraw or EasyFind aren't.
Same behavior which many applications stored in a subfolder named "Applications perso". BBEdit or Script Debugger are proposed but Libre Office isn't.
Some applications stored in the Applications folder are missing too: Data Rescue.app, DiskMaker X 7 for High Sierra.app, GIMP-2.10.app, Malwarebytes.app are some of them


And I add a question: where is iBooks storing its list of recents items?
It has its own Recent items menu but I see no file storing such infos and the Recents Items > Documents menu doesn't display books open in iBooks.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) mardi 10 septembre 2019  13:57:58

Offline

 

#13 2019-09-10 06:21:34 am

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 5894

Re: How to get list of recent items

Yvan Koenig wrote:

I would be glad to know why many applications aren't proposed by choose application.



Because they aren't scriptable?


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Offline

 

#14 2019-09-10 07:17:20 am

KniazidisR
Member
Registered: 2019-03-03
Posts: 538

Re: How to get list of recent items

From the Apple command reference:

"The choose application dialog initially presents a list of all applications registered with the system. To choose an application not in that list, use the Browse button, which allows the user to choose an application anywhere in the file system."

As I understand, on Mac system registered apps is those from identified by Apple developers... It is intresting,were on Mac is registered apps path... It should be managed by Gatekeeper

Yvan Koenig wrote:

I would be glad to know why many applications aren't proposed by choose application.


So, my answer is: no, all applications are proposed by choose application, with Browse button in this dialog.

Last edited by KniazidisR (2019-09-10 07:50:19 am)


macOS Mojave -- version 10.14.4
Safari -- version 12.1

Offline

 

#15 2019-09-10 07:47:41 am

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 5894

Re: How to get list of recent items

KniazidisR wrote:

From the Apple command reference:



I suspect that's well out of date — the dialog changed quite a long time ago (when script apps moved to bundles, I suspect — it was the move to bundles with Info.plist scriptability entries that made it feasible to easily check which are scriptable).

Go into Script Editor and choose File -> Open Dictionary. The list will obviously only show scriptable apps and libraries. Click on the Kind column so applications are sorted before script libraries.

Leave the dialog open and go into Script Debugger and run this script:

Applescript:

choose application

Compare the lists in the two dialogs. I think you'll find they're identical.

Last edited by Shane Stanley (2019-09-10 07:55:26 am)


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Offline

 

#16 2019-09-10 08:05:40 am

KniazidisR
Member
Registered: 2019-03-03
Posts: 538

Re: How to get list of recent items

You are right. I ran your scripts, Shane. The dialogs are almost same for Open dictionary and for Choose application. smile

But this may again be a coincidence, based on Apple’s policy, to highlight the programs of those developers who pay. In any case, the user can select any application in the choose application through the "Browse" button, and this is important for this topic.

And, is  one important difference: press Browse in Open dictionary, go to Applications folder. You can't choose nonscriptable app. But doing this with Browse in Choose application dialog you can choose any application. So, I think, all is related to  money and not to scriptability.

I don't know, Shane, may be one scriptable app non registered to work on Mac?

Last edited by KniazidisR (2019-09-10 08:21:39 am)


macOS Mojave -- version 10.14.4
Safari -- version 12.1

Offline

 

#17 2019-09-10 08:47:42 am

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 3577

Re: How to get list of recent items

Shane Stanley wrote:
Yvan Koenig wrote:

I would be glad to know why many applications aren't proposed by choose application.



Because they aren't scriptable?




Thank you Shane.
It makes sense.
I don't believe that it's a problem of money because most of the applications bought thru MacAppStore aren't proposed although they came from registered developers. The proposed ones are : Amphetamine, Aperture, JSON Helper, Omnigrafle, SiteSucker, Stuffit Expander, The Unarchiver, XCode, Instruments and Simulator which belong to Xcode and iBooks Author. All of them are scriptable.

Being forced to click Browse is really annoying so I will stay with the script which offer only the applications which have a related sfl file.

So now the remaining question is : where is iBook storing it's list of recent items?
It's curious that an application made by Apple doesn't apply the standard scheme.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) mardi 10 septembre 2019  16:47:05

Offline

 

#18 2019-09-10 05:52:15 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 5894

Re: How to get list of recent items

KniazidisR wrote:

But this may again be a coincidence



You're a hard man to convince. Save an applet in /Applications. See that it doesn't show up in choose application. Now open its Info.plist file and add:

    <key>NSAppleScriptEnabled</key>
    <true/>

Open the script in an editor, make a change and save. Now try choose application again.

No money changed hands.


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Offline

 

#19 2019-09-10 05:54:06 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 5894

Re: How to get list of recent items

Yvan Koenig wrote:

It's curious that an application made by Apple doesn't apply the standard scheme.



Not necessarily. Isn't it one of those ported iOS apps? They may well follow a different scheme.


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Offline

 

#20 2019-09-10 09:40:57 pm

KniazidisR
Member
Registered: 2019-03-03
Posts: 538

Re: How to get list of recent items

Shane Stanley wrote:


You're a hard man to convince.


Yes it's true. But you convinced me with the last test. It is very rare for a knowledgeable person to share his knowledge so easily. Thank you, Shane, for the great help on this site. I always read your posts with interest.

Last edited by KniazidisR (2019-09-10 09:41:30 pm)


macOS Mojave -- version 10.14.4
Safari -- version 12.1

Offline

 

#21 2019-09-11 01:19:43 pm

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 3577

Re: 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.

Applescript:

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

Last edited by Yvan Koenig (2019-09-12 02:56:33 am)

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)