This is my first “Zotero” thing. Arguably not very related to translation, but who knows. It’s an attempt at answering the following comment on the Zotero forum:
https://forums.zotero.org/discussion/comment/454302/#Comment_454302
Once you have used “Show in Finder” in the main Zotero window, all the selected items that have a local attachment will open in Finder in their own Zotero managed window. Now, if you want to add the same tag to each of them, you’ll have to apply the tag one by one to each window contents. Above 3 items, it’s not fun anymore.
This script checks that only the Zotero managed Finder windows are concerned by the tag change and apply the set tag (here “Zotero”) to the front window selection, close the window, etc. until the last window is closed.
Once you have done that, you can use Finder’s tag list to find all your newly tagged files. I even added a commented out line if you want to get the “mdfind” output of a search on that tag.
use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use scripting additions
# use Shane Stanley's TAG library located here
# https://lists.apple.com/archives/applescript-users/2015/Jan/msg00193.html
# I’ve copied the code below, but you can put it in a script library and load it with a "use" statement as above
set myTag to "Zotero"
tell application "Finder"
set nbofWindows to number of Finder windows
repeat with myWIndow from 1 to nbofWindows
try
if name of container of target of front Finder window is "storage" then
(my setTags:{myTag} forPath:(POSIX path of (selection as alias)))
tell front Finder window to close
# the tag name here can be changed as you need
# or you can script a selection from an Applescript menu, etc.
end if
on error
tell front Finder window to close
end try
end repeat
end tell
# To get the list of files with that tag:
# set filePath to do shell script "mdfind -name 'kMDItemUserTags == \"" & myTag & "\"cdw'"
on returnTagsFor:posixPath -- get the tags
set aURL to current application's |NSURL|'s fileURLWithPath:posixPath -- make URL
set {theResult, theTags} to aURL's getResourceValue:(reference) forKey:(current application's NSURLTagNamesKey) |error|:(missing value)
if theTags = missing value then return {} -- because when there are none, it returns missing value
return theTags as list
end returnTagsFor:
on setTags:tagList forPath:posixPath -- set the tags, replacing any existing tags
set aURL to current application's |NSURL|'s fileURLWithPath:posixPath -- make URL
aURL's setResourceValue:tagList forKey:(current application's NSURLTagNamesKey) |error|:(missing value)
end setTags:forPath:
on addTags:tagList forPath:posixPath -- add to existing tags
set aURL to current application's |NSURL|'s fileURLWithPath:posixPath -- make URL
-- get existing tags
set {theResult, theTags} to aURL's getResourceValue:(reference) forKey:(current application's NSURLTagNamesKey) |error|:(missing value)
if theTags ≠ missing value then -- add new tags
set tagList to (theTags as list) & tagList
set tagList to (current application's NSOrderedSet's orderedSetWithArray:tagList)'s allObjects() -- delete any duplicates
end if
aURL's setResourceValue:tagList forKey:(current application's NSURLTagNamesKey) |error|:(missing value)
end addTags:forPath: