On 7 Jun 2015, at 2:25 am, Yvan KOENIG <email@hidden> wrote:
I'm now facing a new wall. I put the new code in the library file assuming that it will behave as it does in the script. I was hoping to be able to re-install XMLLib.osax.
Alas, when it's in the library file, the handler behaves exactly as the original one: it doesn't filter the obsolete pathnames and I really don't understand why.
There are two issues. First, the clash with NSURL. Because it's such a common scripting addition, it's easier to avoid the problem by referring to the class by name. So you can use this form:
current application's class "NSURL"
Then you don't have to worry about any clashes.
The second issue is how you check if the file exists. There are a couple of better ways: one is to ask the file manager directly, and the other is to check when resolving the bookmark data. So:
use framework "Foundation" on returnRecentDocsPaths() set defs to current application's NSUserDefaults's standardUserDefaults() defs's addSuiteNamed:"com.apple.recentitems" set theBookmarkData to (defs's valueForKeyPath:"RecentDocuments.CustomListItems.Bookmark") as list set thePaths to {} set theFileManager to current application's NSFileManager's defaultManager() repeat with aData in theBookmarkData set theInfo to (current application's class "NSURL"'s resourceValuesForKeys:{current application's NSURLPathKey} fromBookmarkData:aData) set thePath to (theInfo's valueForKey:(current application's NSURLPathKey)) # Code filtering the obsolete pathnames if (theFileManager's fileExistsAtPath:thePath) as boolean then set end of thePaths to thePath as text end if end repeat return thePaths end returnRecentDocsPaths
The alternative NSURL method is:
use framework "Foundation" on returnRecentDocsPaths() set defs to current application's NSUserDefaults's standardUserDefaults() defs's addSuiteNamed:"com.apple.recentitems" set theBookmarkData to (defs's valueForKeyPath:"RecentDocuments.CustomListItems.Bookmark") as list set thePaths to {} set theFileManager to current application's NSFileManager's defaultManager() repeat with aData in theBookmarkData -- the following returns missing value if URL can't be found set theURL to (current application's class "NSURL"'s URLByResolvingBookmarkData:aData options:((current application's NSURLBookmarkResolutionWithoutUI) + (current application's NSURLBookmarkResolutionWithoutMounting as integer)) relativeToURL:(missing value) bookmarkDataIsStale:(missing value) |error|:(missing value)) if theURL is not missing value then set end of thePaths to theURL's |path|() as text end if end repeat return thePaths end returnRecentDocsPaths
Lastly, it's probably a bit more efficient to read the preferences, rather than add them to those of the current application. So change this:
defs's addSuiteNamed:"com.apple.recentitems" set theBookmarkData to (defs's valueForKeyPath:"RecentDocuments.CustomListItems.Bookmark") as list
to this:
set theDict to defs's persistentDomainForName:"com.apple.recentitems" set theBookmarkData to (theDict's valueForKeyPath:"RecentDocuments.CustomListItems.Bookmark") as list
|