• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Problem with Mavericks/Yosemite library
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Problem with Mavericks/Yosemite library


  • Subject: Re: Problem with Mavericks/Yosemite library
  • From: Shane Stanley <email@hidden>
  • Date: Sun, 07 Jun 2015 11:33:54 +1000

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

-- 
Shane Stanley <email@hidden>
<www.macosxautomation.com/applescript/apps/>

 _______________________________________________
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

References: 
 >Problem with Mavericks/Yosemite library (From: Yvan KOENIG <email@hidden>)

  • Prev by Date: numbers in words
  • Next by Date: Re: numbers in words
  • Previous by thread: Problem with Mavericks/Yosemite library
  • Next by thread: numbers in words
  • Index(es):
    • Date
    • Thread