指定ファイルのFinderタグを取得/設定/追加を行うAppleScriptです。
もともとはShane Stanleyが数年前に書いたScriptですが、Cocoaっぽいハンドラ記述だと慣れていないScripterには敷居が高いので、OLD Style AppleScript風のハンドラに書き換えたものです。
AppleScript名:Finderファイルタグの設定や取得 |
–Created By Shane Stanley use AppleScript version "2.4" use scripting additions use framework "Foundation" property |NSURL| : a reference to current application’s |NSURL| property NSOrderedSet : a reference to current application’s NSOrderedSet property NSURLTagNamesKey : a reference to current application’s NSURLTagNamesKey set anAlias to (choose file) set aRes to getTagsForPath(anAlias) of me — get the tags on getTagsForPath(anAlias) set aURL to |NSURL|’s fileURLWithPath:(POSIX path of anAlias) set {theResult, theTags} to aURL’s getResourceValue:(reference) forKey:(NSURLTagNamesKey) |error|:(missing value) if theTags = missing value then return {} — because when there are none, it returns missing value return theTags as list end getTagsForPath — set the tags, replacing any existing on setTagsForPath(tagList, anAlias) set aURL to |NSURL|’s fileURLWithPath:(POSIX path of anAlias) aURL’s setResourceValue:tagList forKey:(NSURLTagNamesKey) |error|:(missing value) end setTagsForPath — add to existing tags on addTagsForPath(tagList, anAlias) set aURL to |NSURL|’s fileURLWithPath:(POSIX path of anAlias) — get existing tags set {theResult, theTags} to aURL’s getResourceValue:(reference) forKey:(NSURLTagNamesKey) |error|:(missing value) if theTags ≠ missing value then — add new tags set tagList to (theTags as list) & tagList set tagList to (NSOrderedSet’s orderedSetWithArray:tagList)’s allObjects() — delete any duplicates end if aURL’s setResourceValue:tagList forKey:(NSURLTagNamesKey) |error|:(missing value) end addTagsForPath |
More from my site
(Visited 315 times, 2 visits today)