20260505

[AppleScript] フォルダカスタマイズの内容を初期化して初期表示に戻す

[AppleScript] フォルダカスタマイズの内容を初期化して初期表示に戻す

NOTE記事一覧ですnote.com

【スクリプトエディタで開く】 |

フォルダカスタマイズの削除.scpt.scpt
ソース
001#!/usr/bin/env osascript
002#coding: utf-8
003----+----1----+----2----+----3----+----4----+----5----+----6----+----7--
004(*
005
006macOS26以降用
007フォルダカスタマイズの削除
008
009
010v1 初回作成
011
012com.cocolog-nifty.quicktimer.icefloe *)
013----+----1----+----2----+----3----+----4----+----5----+----6----+----7--
014use AppleScript version "2.8"
015use scripting additions
016
017
018
019set listAliasDirPath to (choose folder with multiple selections allowed) as list
020
021
022repeat with itemAliasDirPath in listAliasDirPath
023   #一応エイリアスで確定させておく
024   set aliasDirPath to itemAliasDirPath as alias
025   
026   #旧OS用のインデックス番号を0にリセットして
027   tell application "Finder"
028      tell folder aliasDirPath
029         set label index to 0
030      end tell
031   end tell
032   
033   #### DELETE TAG
034   #UNIXパス
035   set strFilePath to (POSIX path of aliasDirPath) as text
036   #アトリビュートのリストを出して
037   set strCmd to ("/usr/bin/xattr -l '" & strFilePath & "'") as text
038   set strStdOut to (do shell script strCmd) as text
039   #アトリビュートのキーが
040   set strKey to ("com.apple.metadata:_kMDItemUserTags") as text
041   #含まれているなら
042   if strStdOut contains strKey then
043      try
044         #削除
045         set strCmd to ("/usr/bin/xattr -d " & strKey & " '" & strFilePath & "'") as text
046         do shell script strCmd
047      on error strErrMsg number numErrNo
048         log "No: " & numErrNo & linefeed & "Error:" & strErrMsg & ""
049         log "ERROR: " & strFilePath & ""
050      end try
051   end if
052   
053   #### DELETE ICON
054   #FINDERでフォルダ指定してアップデートで削除される場合が多い
055   tell application "Finder"
056      tell folder aliasDirPath
057         update
058      end tell
059   end tell
060   #一応確認して
061   set strCmd to ("/usr/bin/xattr -l '" & strFilePath & "'") as text
062   set strStdOut to (do shell script strCmd) as text
063   #キーが含まれているなら
064   set strKey to ("com.apple.icon.folder#S") as text
065   #削除する
066   if strStdOut contains strKey then
067      try
068         set strCmd to ("/usr/bin/xattr -d " & strKey & " '" & strFilePath & "'") as text
069         do shell script strCmd
070      on error strErrMsg number numErrNo
071         log "No: " & numErrNo & linefeed & "Error:" & strErrMsg & ""
072         log "ERROR: " & strFilePath & ""
073      end try
074   end if
075   
076   
077end repeat
078
079
080return
081
082
AppleScriptで生成しました