I’m still struggling with AppleScript syntax (so what else is new?).
I know how to read the names of all the Macro Groups in a kmmacros plist file. I do this by loading the plist into memory (at least, I assume that’s what’s happening) like this:
tell application "System Events"
set _plist to (value of property list file plistFileName)
set _groupNames to {}
repeat with _item in _plist
set end of _groupNames to |name| of _item
end repeat
return _groupNames
end tell
I could even change the names of the Macro Groups by "set"ting them. But since the file is only in memory, it doesn’t actually update the file on disk (at least, I assume that’s what’s happening).
I’ve found lots of examples of how to update values in a plist file, but none of them work with a plist file with a top-level array. That’s the syntax I can’t figure out.
Hey Dan,
Have a look at this:
set pListFilePathPosix to "~/Documents/Keyboard Maestro Stuff/Exported Macros/test_macro.kmmacros"
tell application "System Events"
# You've now created an AppleScript Record Object.
set _plist to value of property list file pListFilePathPosix
end tell
# So we're now working with a record instead of a plist.
set _plist to item 1 of _plist
set theMacros to Macros of _plist
set theTopLevelActions to |Actions| of item 1 of theMacros
-Chris
I don’t see how this is different from what I do. Unless you’re telling me that the parens around (value of property list file …) return a different result than if I didn’t have them.
Sure, you use a direct reference to “item 1 of _plist” instead of iterating it, but the result is no different, since there’s only one entry in the top-level array.
And unless up has suddenly become down, changing a value in your _plist, or theMacros, or anything else, will not get persisted back to disk.
So I’ll bite - how is this different?
Once you’ve modified the object in memory (record/dictionary of keyValues, or array of such records/dictionaries), you can write that transformed object back out to a plist, e.g. with something like writePlist() here: Reading and writing plists from Execute Script actions
(where ‘plist’ means a kmMacros file in this case, of course)
1 Like
Hey Dan,
The major point is to show that you you're not working with a plist file or a plist item but an AppleScript Record.
Writing a value to a Property List file on-disk is easy:
set plistFilePosix to "~/Documents/Keyboard Maestro Stuff/Exported Macros/Focussed Finder Window Trigger.kmmacros"
tell application "System Events"
tell property list file plistFilePosix
tell property list item 1
tell property list item "Name"
properties
set its value to "NEW NAME" --> Set a break point and examine properties before and after.
properties
end tell
end tell
end tell
end tell
But I don't know how to do that in memory of if it's even possible.
-Chris
2 Likes