Modifying smart folders via applescript

Hi everyone,

Since OS X smart folders are simply plists (without the .plist extension), I had an idea to modify the search parameters of one programmatically. One of the commonly used plist strategies is to employ the command line utility ‘defaults’. Unfortunately, that only works with plists that have the .plist extension.

Is there another approach that that seems to be generally useful?


Plists can generally be read 3 ways. 1) as a text file, 2) using System Events, and 3) using the defaults command. So if defaults doesn’t work try the other 2.

For example, if I wanted to know the raw query from a saved search I could do either of these. And you could write to the plist in the same manner.

set searchPlist to (path to desktop folder as text) & "search.savedSearch"
tell application "System Events"
	set plist to property list file searchPlist
	set theValues to value of property list items of plist
	set theQuery to RawQuery of (item 1 of theValues)
end tell
set searchPlist to (path to desktop folder as text) & "search.savedSearch"
set textContents to read file searchPlist

set text item delimiters to "<key>RawQuery</key>
set a to text item 2 of textContents
set text item delimiters to "</string>"
set theQuery to text item 1 of a
set text item delimiters to ""
return theQuery

That looks great. I’ll give it a go. Thanks!