Hi all.
So I know I can get MDLS to output its information in some vaguely plist-y format.
Is there a way I can then insert that into a my plist file as the value for a key without having to basically read each value and then create a new plist object and insert that into the file? Or is that what im going to have to do?
Trying to recursively store the metadata from MDLS in a plist file from a (Choose folder)
The easiest way is not to use mdls:
use AppleScript version "2.5" -- macOS 10.11 or later
use framework "Foundation"
use scripting additions
set theFile to (choose file)
set theDest to POSIX path of (choose file name with prompt "Save as" default name "Untitled.plist")
set mdItem to current application's NSMetadataItem's alloc()'s initWithURL:theFile
set theKeys to (mdItem's attributes())
set theMetadata to mdItem's valuesForAttributes:theKeys
set theData to current application's NSPropertyListSerialization's dataWithPropertyList:theMetadata format:(current application's NSPropertyListBinaryFormat_v1_0) options:0 |error|:(missing value)
theData's writeToFile:theDest atomically:false
So here’s where im at. Hopefully this sheds some light on what im trying to do. Have been at this for about an hour now. I think I am close though. :lol:
on getMetaData(getMeta, outputLocation, shellPassword)
if getMeta as boolean then
set fileLocation to outputLocation & "MetaData/"
do shell script "mkdir " & fileLocation
set folderDict to {}
set metaDataDict to {}
tell application "Finder"
set file_list to entire contents of (choose folder with prompt "Please select directory.") as alias list
repeat with oneFile in file_list
set the folderDict to the folderDict & (POSIX path of oneFile)
end repeat
end tell
repeat with oneFile in folderDict
-- make new property list item at end with properties {kind:date, name:"Program Start Time", value:startTime}
--display dialog oneFile
set fileAlias to oneFile as POSIX file as alias
set mdItem to current application's NSMetadataItem's alloc()'s initWithURL:fileAlias
set theMetadata to {}
try
set theMetadata to (mdItem's valuesForAttributes:(mdItem's attributes())) as record
end try
set the metaDataDict to metaDataDict & theMetaData
end repeat
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}
tell property list items of metaDataFile
repeat with oneData in metaDataDict
make new property list item at end with properties {kind:record, name:((_kMDItemDisplayNameWithExtensions of oneData) as text), value:oneData }
end repeat
end tell
end tell
end if
end getMetaData
I’m afraid it doesn’t shed enough light for me to understand…
Sorry for the poor explanation, I posted the question at like 6 am after an entire night of working on the app this is a part of, and after being stuck working on this specific part for over an hour.
The problem was I was trying to store values as ‘records’, which I now know… isn’t really a thing in this situation. Going to see if I can get it working now that I know it needs to be a dictionary, and will circle back if I am still having issues.
Thanks for taking the time to try and help someone who is clearly new to AppleScript! :lol: .
Going to move this to the ObjC area as this is no longer a general AppleScript issue.
Have greatly improved things. Only issue now is actually storing the metadata dictionary in the plist. Everything works besides getting fileMetaDataDict to actually go into the plist.
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.
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
Did you look at my code above? it shows you how to do that very thing.