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 <http://troybrant.net/blog/2010/09/simple-xml-to-nsdictionary-converter/>
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
else
-- 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
else
-- 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