Having a little fun, here’s a bit terse but succinct approach to getting the filename of files with a specific tag and put them on the clipboard…
set {od, AppleScript's text item delimiters} to {AppleScript's text item delimiters, linefeed}
tell application id "DNtp" to set the clipboard to (get filename of (contents of (current database) whose (tags contains "iCloud")) as string)
set AppleScript's text item delimiters to od
Extending this a little further, this lets you add a tag to look for and generates a plain text file with the results…
set {od, AppleScript's text item delimiters, searchTerm} to {AppleScript's text item delimiters, linefeed, text returned of (display dialog "Enter case-sensitive tag to find:" default answer "")}
tell application id "DNtp" to create record with {name:searchTerm & " Files", type:text, content:(get filename of (contents of (current database) whose (tags contains searchTerm)) as string)} in current group
set AppleScript's text item delimiters to od
While these are fun, bear in mind, they’re a stacked deck to some degree. That doesn’t mean they’re unsafe or pointless. It just means they aren’t error-trapped, so the second one will generate an empty text file if there are no matches.
To address that and make it a little more readable for learning, with a bit of error-trapping…
tell application id "DNtp"
set od to AppleScript's text item delimiters
set searchTerm to ""
repeat while searchTerm is ""
set searchTerm to text returned of (display dialog "Enter case-sensitive tag to find:" default answer "")
end repeat
set matchedFiles to get filename of (contents of (current database) whose (tags contains searchTerm))
if matchedFiles is not {} then
set AppleScript's text item delimiters to linefeed
create record with {name:searchTerm & " Files", type:text, content:matchedFiles as string} in current group
set AppleScript's text item delimiters to od
else
log message "No files with the tag " & searchTerm & " were found in this database."
end if
end tell
And here’s a different – and error-trapped – approach using a tags:
search…
tell application id "DNtp"
set od to AppleScript's text item delimiters
set searchTerm to ""
repeat while searchTerm is ""
set searchTerm to text returned of (display dialog "Enter case-sensitive tags to find. Separate them with semicolons, e.g, one;two" default answer "")
end repeat
set matchedFiles to search "tags:" & searchTerm in (root of (current database))
if matchedFiles is not {} then
set fileList to {}
set AppleScript's text item delimiters to linefeed
repeat with theFile in matchedFiles
copy name of theFile to end of fileList
end repeat
create record with {name:searchTerm & " Files", type:text, content:fileList as string} in current group
set AppleScript's text item delimiters to od
else
log message "No files with the tag(s) " & searchTerm & " were found in this database."
end if
end tell
… when nothing is found …
Epilogue
In case anyone is curious, this post also shows the iterative process often found in scripting. The scripts started with my first three-liner, just to push the limits of brevity for fun. It evolved from there adding some flexibility, then expanding it out for teaching, then showing a secondary approach to finding the files.
In many cases, the evolution is the opposite direction, moving from verbose and extra cautious, with singleton variables (only defined and used once) and complex, to cleaner and more concise code at the end. In other cases, it expands and contracts, from simple and fragile, to overly-complicated, then back to poetry