Monday, December 9, 2019

#1 2019-11-23 07:08:26 pm

andrewbluepiano
Member
Registered: 2019-11-20
Posts: 10
Website

write dictionary to plist file as value for key

Hi all,
I am trying to parse the metadata for a folder and its entire contents to a plist file, and store the metadata as a value for a key that is the file/folders name. Using the fourm, and various other sources, I have almost gotten it working. I currently can parse an entire folder and its contents (function not shown), return that as a list of POSIX paths, which I then can grab the metadata for and return as a dictionary using getMetaDataDict().

My question is how I can then write that dictionary to the plist file. The ways I am seeing people write info to dictionaries in plist's involves going item by item. I would like to avoid having to parse over the contents of the fileMetaDataDict because that would also mean detecting when the values are arrays/dicts and whatnot. Which I can do, but I would hope there is an easier way.

Applescript:


on getMetaDataDict(fileRef)
set fileAlias to fileRef as POSIX file as alias
set mdItem to current application's NSMetadataItem's alloc()'s initWithURL:fileAlias
set theMetadata to (mdItem's valuesForAttributes:(mdItem's attributes())) as record
set itemMetaDataDict to current application's NSDictionary's dictionaryWithDictionary:theMetadata
return itemMetaDataDict
end getMetaDataDict

on getMetaData(getMeta, outputLocation, shellPassword)
-- maybe PlistBuddy needs to get involved
if getMeta as boolean then
set fileLocation to outputLocation & "MetaData/"
do shell script "mkdir " & fileLocation
set scriptLocation to (current application's NSBundle's mainBundle()'s resourcePath() as text) & "/subScripts/getEverything.scpt"
set getEverything to load script current application's POSIX file scriptLocation

tell getEverything
set allContents to runGet()
end tell
-- everything is POSIX paths of stuff

-- set metaDataDict to {}

tell application "System Events"
set metaDataDict to make new property list item with properties {kind:record}
set metaDataFilePath to (fileLocation & "MetaData.plist")
set metaDataFile to make new property list file with properties {contents:metaDataDict, name:metaDataFilePath}
end tell

repeat with oneFile in allContents
set fileMetaDataDict to getMetaDataDict(oneFile)
display alert "3"
tell application "System Events"
tell property list items of metaDataFile
make new property list item at end with properties {kind:record, name:(name of (info for (oneFile as POSIX file))), value:fileMetaDataDict}
end tell
end tell
end repeat

end if
end getMetaData


Filed under: dictionary, write, metadata, plist

Offline

 

#2 2019-11-23 09:44:33 pm

andrewbluepiano
Member
Registered: 2019-11-20
Posts: 10
Website

Re: write dictionary to plist file as value for key

Update: This is how I am doing it, but I would think there is a more efficient way

Applescript:


on getMetaData(getMeta, outputLocation, shellPassword)
-- maybe PlistBuddy needs to get involved
if getMeta as boolean then
display alert "This function is recursive throughout the entire directory selected. It will follow aliases, symlinks, whatever. If you select a directory whose contents you arent aware of, there is a high potential to end up scanning the entire filesystem. Be careful." with title "WARNING"
set metaDataStartTime to current date
set fileLocation to outputLocation & "MetaData/"
set outputFileLocation to outputLocation & "MetaData/metadata.plist"
do shell script "mkdir " & fileLocation
set appLocation to (current application's NSBundle's mainBundle()'s resourcePath() as text)
set scriptLocation to appLocation & "/subScripts/getEverything.scpt"
set getEverything to load script current application's POSIX file scriptLocation

tell getEverything
set allContents to runGet()
end tell
-- everything is POSIX paths of stuff

-- set metaDataDict to {}

tell application "System Events"
set metaDataDict to make new property list item with properties {kind:record}
set metaDataFilePath to (fileLocation & "MetaData.plist")
set metaDataFile to make new property list file with properties {contents:metaDataDict, name:metaDataFilePath}
end tell
set a to 0
repeat with oneFile in allContents
display alert (quoted form of (name of (info for (oneFile as POSIX file))))
do shell script "mdls " & quoted form of oneFile & " -plist " & appLocation & "/tmp.plist"
do shell script "/usr/libexec/PlistBuddy -c \"Add :" & a & " dict\" " & outputFileLocation
do shell script "/usr/libexec/PlistBuddy -c \"Merge " & appLocation & "/tmp.plist :" & a & " \" " & outputFileLocation
set a to (a + 1)
end repeat
timeStamp(outputLocation, "metadata backup start time", metaDataStartTime)
end if
end getMetaData

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)