{"id":935,"date":"2021-02-05T19:41:57","date_gmt":"2021-02-06T02:41:57","guid":{"rendered":"http:\/\/10.0.1.201\/?p=935"},"modified":"2025-11-30T12:21:02","modified_gmt":"2025-11-30T19:21:02","slug":"applescript-to-write-finder-tag-from-folder-name","status":"publish","type":"post","link":"https:\/\/strawhousepig.net\/wordpress\/2021\/02\/05\/applescript-to-write-finder-tag-from-folder-name\/","title":{"rendered":"AppleScript to write Finder tag from folder name"},"content":{"rendered":"<p>This is a weird one that came from <a href=\"https:\/\/www.reddit.com\/r\/scripting\/comments\/km97wu\/macos_script_or_terminal_command_to_convert_alias\/\">a request on reddit.<\/a><\/p>\n<p>Not really useful (to me) as intended, but I envision it could be re-worked to a FolderAction that would help tag photos. Especially if coupled with a file sorter. But incorporating the tag writing into the sorter would be much more sane.<\/p>\n<p>The commented out <code>do shell script<\/code> lines were meant as a way to read existing tags and then add the folder name after them. That didn&#8217;t work out thanks to <code>xattr -p<\/code> spitting out hexadecimal instead of a plist array, which is the format used when writing the tags. WTF, Apple&#8230;<\/p>\n<pre>-- http:\/\/strawhousepig.net\/\non run\n  set _drop to {}\n  set end of _drop to (choose folder)\n  my do_it(_drop)\nend run\n\non open _drop\n  my do_it(_drop)\nend open\n\non do_it(_drop)\n  display dialog \"WARNING: This script will overwrite ALL tags of files in or targeted from the opened folder with the name of the opened folder.\" with icon 0\n  repeat with d in _drop\n    if folder of (info for d) is true then\n      set f to {}\n      try\n        tell application \"Finder\"\n          set _tag to \"\" &amp; name of (info for d) &amp; \"\"\n          set _files to (every item in d)\n          repeat with f in _files\n            if alias of (info for f as alias) is true then\n              set f to original item of f as alias\n            end if\n-- 'xattr -p' will print the value for a named metadata ID. Naturally that value is printed as hexadecimal. :|\n-- set f_plist to (do shell script \"xattr -p com.apple.metadata:_kMDItemUserTags \" &amp; quoted form of POSIX path of (f as alias))\n-- set _tag to (do shell script \"echo \\\"\" &amp; f_plist &amp; \"\\\" | egrep -o \\\"*\\\"\") &amp; _tag\n            set tag_plist to \"\" &amp; _tag &amp; \"\"\n            do shell script \"xattr -w com.apple.metadata:_kMDItemUserTags \" &amp; quoted form of tag_plist &amp; \" \" &amp; quoted form of (POSIX path of f)\n          end repeat\n        end tell\n      on error _err\n        display dialog _err\n      end try\n    end if\n  end repeat\nend do_it<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>This is a weird one that came from a request on reddit. Not really useful (to me) as intended, but I envision it could be re-worked to a FolderAction that would help tag photos. Especially if coupled with a file sorter. But incorporating the tag writing into the sorter would be much more sane. The [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[10,24,28],"class_list":["post-935","post","type-post","status-publish","format-standard","hentry","category-code","tag-applescript-2","tag-macos","tag-os-x"],"_links":{"self":[{"href":"https:\/\/strawhousepig.net\/wordpress\/wp-json\/wp\/v2\/posts\/935","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/strawhousepig.net\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/strawhousepig.net\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/strawhousepig.net\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/strawhousepig.net\/wordpress\/wp-json\/wp\/v2\/comments?post=935"}],"version-history":[{"count":1,"href":"https:\/\/strawhousepig.net\/wordpress\/wp-json\/wp\/v2\/posts\/935\/revisions"}],"predecessor-version":[{"id":1058,"href":"https:\/\/strawhousepig.net\/wordpress\/wp-json\/wp\/v2\/posts\/935\/revisions\/1058"}],"wp:attachment":[{"href":"https:\/\/strawhousepig.net\/wordpress\/wp-json\/wp\/v2\/media?parent=935"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/strawhousepig.net\/wordpress\/wp-json\/wp\/v2\/categories?post=935"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/strawhousepig.net\/wordpress\/wp-json\/wp\/v2\/tags?post=935"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}