Script stopped working after HD migration

The script below (and a couple others with the same handler) stopped working after I migrated to a new HD.

This line in the GetFileNames(folderWithFiles, fileExtension) handler

	set fileNames to (thePaths's pathsMatchingExtensions:{fileExtension}) as list

generates this AppleScript Execution Error:

"missing value doesn’t understand the “pathsMatchingExtensions_” message."

Any suggestions?

use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use script "fileManagerLib"
use scripting additions
property lastChoice : {}
property dictStack : missing value -- stack to hold array of dictionaries
property textInProgress : "" -- string to collect text as it is found
property anError : missing value -- if we get an error, store it here

set scriptLibrariesFolder to path to library folder from user domain as text
set scriptLibrariesFolder to scriptLibrariesFolder & "Script Libraries:"
set myScriptLibraries to my GetFileNames(scriptLibrariesFolder, "scptd")

set listTitle to "Script Libraries"
set listPrompt to "Which script library's dictionary would you like to try to open?" & return & return & "If the library has no dictionary it will try to open it as a script"
set okbuttonName to "This One"
set multipleSelections to true
set emptySelectionAllowed to false

set userChoice to choose from list myScriptLibraries ¬
   with title listTitle ¬
   with prompt listPrompt ¬
   OK button name okbuttonName ¬
   multiple selections allowed false ¬
   default items lastChoice ¬
   empty selection allowed emptySelectionAllowed

if userChoice is false then return
set lastChoice to userChoice
set scriptList to {}
repeat with thisScript in userChoice
   set thisScript to thisScript as alias
   set theTable to read thisScript
   set xmlString to theTable
   set dictionaryAsRecord to (its makeRecordWithXML:xmlString)
   set myDict to dictionary of dictionaryAsRecord
   tell myDict
      tell its attributes
         set myTitle to its title
      end tell
      set mySuites to suites of myDict
   end tell
end repeat

-- Based on <>

on makeRecordWithXML:xmlString
   -- set up properties
   set my dictStack to current application's NSMutableArray's array() -- empty mutable array
   dictStack's addObject:(current application's NSMutableDictionary's dictionary()) -- add empty mutable dictionary
   set my textInProgress to current application's NSMutableString's |string|() -- empty mutable string
   -- convert XML from string to data
   set anNSString to current application's NSString's stringWithString:xmlString
   set theData to anNSString's dataUsingEncoding:(current application's NSUTF8StringEncoding)
   -- initialize an XML parser with the data
   set theNSXMLParser to current application's NSXMLParser's alloc()'s initWithData:theData
   -- set this script to be the parser's delegate
   theNSXMLParser's setDelegate:me
   -- tell it to parse the XML
   set theResult to theNSXMLParser's parse()
   if theResult then -- went OK, get first item on stack
      return ((my dictStack)'s firstObject()) as record
   else -- error, so return error
      error (my anError's localizedDescription() as text)
   end if
end makeRecordWithXML:

-- this is an XML parser delegate method. Called when new element found
on parser:anNSXMLParser didStartElement:elementName namespaceURI:aString qualifiedName:qName attributes:aRecord
   -- store reference to last item on the stack
   set parentDict to my dictStack's lastObject()
   -- make new child
   set childDict to current application's NSMutableDictionary's dictionary()
   -- if there are attributes, add them as a record with key "attributes"
   if aRecord's |count|() > 0 then
      childDict's setValue:aRecord forKey:"attributes"
   end if
   -- see if there's already an item for this key
   set existingValue to parentDict's objectForKey:elementName
   if existingValue is not missing value then
      -- there is, so if it's an array, store it...
      if (existingValue's isKindOfClass:(current application's NSMutableArray)) as boolean then
         set theArray to existingValue
         -- otherwise create an array and add it
         set theArray to current application's NSMutableArray's arrayWithObject:existingValue
         parentDict's setObject:theArray forKey:elementName
      end if
      -- then add the new dictionary to the array
      theArray's addObject:childDict
      -- add new dictionary directly to the parent
      parentDict's setObject:childDict forKey:elementName
   end if
   -- also add the new dictionary to the end of the stack
   (my dictStack)'s addObject:childDict
end parser:didStartElement:namespaceURI:qualifiedName:attributes:

-- this is an XML parser delegate method. Called at the end of an element
on parser:anNSXMLParser didEndElement:elementName namespaceURI:aString qualifiedName:qName
   -- if any text has been stored, add it as a record with key "contents"
   if my textInProgress's |length|() > 0 then
      set dictInProgress to my dictStack's lastObject()
      dictInProgress's setObject:textInProgress forKey:"contents"
      -- reset textInProgress property for next element
      set my textInProgress to current application's NSMutableString's |string|()
   end if
   -- remove last item from the stack
   my dictStack's removeLastObject()
end parser:didEndElement:namespaceURI:qualifiedName:

-- this is an XML parser delegate method. Called when string is found. May be called repeatedly
on parser:anNSXMLParser foundCharacters:aString
   -- only append string if it's not solely made of space characters (which should be, but aren't, caught by another delegate method)
   if (aString's stringByTrimmingCharactersInSet:(current application's NSCharacterSet's whitespaceAndNewlineCharacterSet()))'s |length|() > 0 then
      (my textInProgress)'s appendString:aString
   end if
end parser:foundCharacters:

-- this is an XML parser delegate method. Called when there's an error
on parser:anNSXMLParser parseErrorOccurred:anNSError
   set my anError to anNSError
end parser:parseErrorOccurred:

--> {|character|:{firstName:{|contents|:"Saga"}, lastName:{|contents|:"Norén"}, city:{|contents|:"Malmö"}, partner:{firstName:{|contents|:"Martin"}, lastName:{|contents|:"8"}, city:{|contents|:"København"}, attributes:{approach:"dogged"}}}}

on GetFileNames(folderWithFiles, fileExtension)
   local fileNames
   set folderPath to folderWithFiles
   set folderWithFiles to POSIX path of (folderWithFiles)
   set thePaths to current application's NSFileManager's defaultManager()'s contentsOfDirectoryAtPath:folderWithFiles |error|:(missing value)
   set fileNames to (thePaths's pathsMatchingExtensions:{fileExtension}) as list
   set filesWithDictionaries to {}
   set filesWithOutDictionaries to {}
   set saveTID to AppleScript's text item delimiters
   set AppleScript's text item delimiters to {return}
   repeat with thisFileName in fileNames
      set dictPath to (folderPath & thisFileName as text) & ":Contents:Resources:"
      set dictPosixPath to POSIX path of dictPath
      set thePaths to (current application's NSFileManager's defaultManager()'s contentsOfDirectoryAtPath:dictPosixPath |error|:(missing value))
      set fileNames to (thePaths's pathsMatchingExtensions:{"sdef"}) as list
      if fileNames is not {} then
         set filePath to dictPath & (fileNames as text)
         set the end of filesWithDictionaries to filePath
      end if
   end repeat
   set AppleScript's text item delimiters to saveTID
   return filesWithDictionaries
end GetFileNames

That message is telling you that thePaths is missing value at that point. So change the previous line to:

set {thePaths, theError} to (current application's NSFileManager's defaultManager()'s contentsOfDirectoryAtPath:dictPosixPath |error|:(reference))
if thePaths = missing value then error (theError's localizedDescription()) as text

That should give you more idea of what’s happening. You don’t say what version of the OS you’re now running, but my guess is a permissions error.

This is the error now:

The folder “Script Libraries” doesn’t exist.

So I went looking for it. I found it in the Finder, opened it, everything seemed in order. I ran the script again and it worked.

–> Script Debugger 7.0.9 (7A85)
–> Mac OS 10.11.6 (15G31)


FWIW, I reckon you save a bit of grief in the long run adding error code like that.

That definitely helped. I’m also thinking I could rewrite that to use filemanager which would make it easier to trouble shoot… except filemanager wouldn’t have worked either?

I’m not sure what you mean, but the simple rule-of-thumb is that if there’s a parameter called error, you can use reference to return its contents. Testing for failure depends on the method. if it normally returns a boolean, generally false means an error. If it should return an object like the array here, then you can test for missing value.