
ファイル・フォルダの各種属性(拡張属性Extended Attributes (EA))をリセットするクイックアクション【macOS】
【Safari・FireFox用Script Editorで開く】 |
| ソース | |
|---|---|
| 001 | #!/usr/bin/env osascript |
| 002 | #coding: utf-8 |
| 003 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
| 004 | (* |
| 005 | Automatorクイックアクション用 |
| 006 | Service Workflow |
| 007 | |
| 008 | 右クリックから |
| 009 | 選択実行で |
| 010 | ファイルの拡張属性を削除します |
| 011 | xattrコマンドを利用して -cオプションによる |
| 012 | エクステンド アトリビュートの削除を行います |
| 013 | |
| 014 | v1 初回作成 |
| 015 | v2 setFileの属性も削除するようにした |
| 016 | v2.1 ACLsの属性削除も行うようにした |
| 017 | v2.2 chflagsの解除も入れるようにした |
| 018 | v2.3 note公開版 |
| 019 | v2.4 ACLsの削除を再起処理にした |
| 020 | |
| 021 | |
| 022 | com.cocolog-nifty.quicktimer.icefloe *) |
| 023 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
| 024 | use AppleScript version "2.8" |
| 025 | use framework "Foundation" |
| 026 | use framework "AppKit" |
| 027 | use scripting additions |
| 028 | property refMe : a reference to current application |
| 029 | |
| 030 | |
| 031 | #################################### |
| 032 | #RUN |
| 033 | (* |
| 034 | on run {listAliasFilePath} |
| 035 | |
| 036 | |
| 037 | テスト用 |
| 038 | *) |
| 039 | on run |
| 040 | set aliasDefaultLocation to (path to desktop folder from user domain) as alias |
| 041 | set listUTI to {"public.item"} as list |
| 042 | set strMsg to ("ファイルを選んでください") as text |
| 043 | set strPrompt to ("ファイルを選んでください") as text |
| 044 | try |
| 045 | tell application "SystemUIServer" |
| 046 | activate |
| 047 | set listAliasFilePath to (choose file strMsg with prompt strPrompt default location (aliasDefaultLocation) of type listUTI with invisibles and multiple selections allowed without showing package contents) as list |
| 048 | end tell |
| 049 | on error strErrMes number numErrNo |
| 050 | log strErrMes & numErrNo |
| 051 | return false |
| 052 | end try |
| 053 | |
| 054 | |
| 055 | |
| 056 | set boolDone to (open listAliasFilePath) |
| 057 | return true |
| 058 | end run |
| 059 | |
| 060 | |
| 061 | #################################### |
| 062 | #ドロップOPEN |
| 063 | on open listAliasFilePath |
| 064 | #エイリアスにしてひとつずつ次工程に回す |
| 065 | repeat with itemAliasPath in listAliasFilePath |
| 066 | set aliasItemFilePath to itemAliasPath as alias |
| 067 | set boolDone to doDelXattr(aliasItemFilePath) |
| 068 | end repeat |
| 069 | |
| 070 | return true |
| 071 | end open |
| 072 | |
| 073 | #################################### |
| 074 | #拡張属性(extended attributes)の削除 |
| 075 | on doDelXattr(itemAliasPath) |
| 076 | |
| 077 | #パス フォルダのエイリアス |
| 078 | set aliasItemPath to itemAliasPath as alias |
| 079 | #パス UNIXパス |
| 080 | set strFilePath to (POSIX path of aliasItemPath) as text |
| 081 | #パスのエスケープ |
| 082 | set ocidFilePath to doPathEscape(strFilePath) |
| 083 | |
| 084 | ################### |
| 085 | set strCmd to ("/usr/bin/chflags nouchg,noschg \\\"" & ocidFilePath & "\\\"") as text |
| 086 | log "" & linefeed & strCmd & linefeed & "" |
| 087 | set strExec to ("/bin/zsh -c \"" & strCmd & "\"") as text |
| 088 | log "" & linefeed & strExec & linefeed & "" |
| 089 | try |
| 090 | log "コマンド開始" & ((current date) as text) |
| 091 | set strResnponse to (do shell script strExec) as text |
| 092 | log "コマンド終了" & ((current date) as text) |
| 093 | on error strErroMsg number numErrorNo |
| 094 | log strErroMsg & numErrorNo |
| 095 | # return false -->ここはエラーでも次に回す |
| 096 | end try |
| 097 | |
| 098 | ################### |
| 099 | set strCmd to ("/usr/bin/chflags nohidden \\\"" & ocidFilePath & "\\\"") as text |
| 100 | log "" & linefeed & strCmd & linefeed & "" |
| 101 | set strExec to ("/bin/zsh -c \"" & strCmd & "\"") as text |
| 102 | log "" & linefeed & strExec & linefeed & "" |
| 103 | try |
| 104 | log "コマンド開始" & ((current date) as text) |
| 105 | set strResnponse to (do shell script strExec) as text |
| 106 | log "コマンド終了" & ((current date) as text) |
| 107 | on error strErroMsg number numErrorNo |
| 108 | log strErroMsg & numErrorNo |
| 109 | # return false -->ここはエラーでも次に回す |
| 110 | end try |
| 111 | |
| 112 | |
| 113 | ################### |
| 114 | set recordInfoFor to (info for aliasItemPath) as record |
| 115 | set boolIsFolder to (folder of recordInfoFor) as boolean |
| 116 | if boolIsFolder is true then |
| 117 | set strCmd to ("/usr/bin/SetFile -a lcdeivzst \\\"" & ocidFilePath & "\\\"") as text |
| 118 | else if boolIsFolder is false then |
| 119 | set strCmd to ("/usr/bin/SetFile -a lst \\\"" & ocidFilePath & "\\\"") as text |
| 120 | end if |
| 121 | log "" & linefeed & strCmd & linefeed & "" |
| 122 | set strExec to ("/bin/zsh -c \"" & strCmd & "\"") as text |
| 123 | log "" & linefeed & strExec & linefeed & "" |
| 124 | try |
| 125 | log "コマンド開始" & ((current date) as text) |
| 126 | set strResnponse to (do shell script strExec) as text |
| 127 | log "コマンド終了" & ((current date) as text) |
| 128 | on error strErroMsg number numErrorNo |
| 129 | log strErroMsg & numErrorNo |
| 130 | # return false -->ここはエラーでも次に回す |
| 131 | end try |
| 132 | |
| 133 | ################### |
| 134 | set strCmd to ("/bin/chmod -RN \\\"" & ocidFilePath & "\\\"") as text |
| 135 | log "" & linefeed & strCmd & linefeed & "" |
| 136 | set strExec to ("/bin/zsh -c \"" & strCmd & "\"") as text |
| 137 | log "" & linefeed & strExec & linefeed & "" |
| 138 | try |
| 139 | log "コマンド開始" & ((current date) as text) |
| 140 | set strResnponse to (do shell script strExec) as text |
| 141 | log "コマンド終了" & ((current date) as text) |
| 142 | on error strErroMsg number numErrorNo |
| 143 | log strErroMsg & numErrorNo |
| 144 | # return false -->ここはエラーでも次に回す |
| 145 | end try |
| 146 | |
| 147 | |
| 148 | ################### |
| 149 | set strCmd to ("/usr/bin/xattr -rc \\\"" & ocidFilePath & "\\\"") as text |
| 150 | log "" & linefeed & strCmd & linefeed & "" |
| 151 | set strExec to ("/bin/zsh -c \"" & strCmd & "\"") as text |
| 152 | log "" & linefeed & strExec & linefeed & "" |
| 153 | try |
| 154 | log "コマンド開始" & ((current date) as text) |
| 155 | set strResnponse to (do shell script strExec) as text |
| 156 | log "コマンド終了" & ((current date) as text) |
| 157 | on error strErroMsg number numErrorNo |
| 158 | log strErroMsg & numErrorNo |
| 159 | return false |
| 160 | end try |
| 161 | |
| 162 | #ここまでにした |
| 163 | return true |
| 164 | |
| 165 | ################### |
| 166 | (* |
| 167 | set strCmd to ("/usr/bin/xattr -rd com.apple.ResourceFork \\\"" & ocidFilePath & "\\\"") as text |
| 168 | log "" & linefeed & strCmd & linefeed & "" |
| 169 | set strExec to ("/bin/zsh -c \"" & strCmd & "\"") as text |
| 170 | log "" & linefeed & strExec & linefeed & "" |
| 171 | try |
| 172 | log "コマンド開始" & ((current date) as text) |
| 173 | set strResnponse to (do shell script strExec) as text |
| 174 | log "コマンド終了" & ((current date) as text) |
| 175 | on error strErroMsg number numErrorNo |
| 176 | log strErroMsg & numErrorNo |
| 177 | return false |
| 178 | end try |
| 179 | |
| 180 | ################### |
| 181 | |
| 182 | set strCmd to ("/usr/bin/xattr -rd com.apple.quarantine \\\"" & ocidFilePath & "\\\"") as text |
| 183 | log "" & linefeed & strCmd & linefeed & "" |
| 184 | set strExec to ("/bin/zsh -c \"" & strCmd & "\"") as text |
| 185 | log "" & linefeed & strExec & linefeed & "" |
| 186 | try |
| 187 | log "コマンド開始" & ((current date) as text) |
| 188 | set strResnponse to (do shell script strExec) as text |
| 189 | log "コマンド終了" & ((current date) as text) |
| 190 | on error strErroMsg number numErrorNo |
| 191 | log strErroMsg & numErrorNo |
| 192 | return false |
| 193 | end try |
| 194 | *) |
| 195 | |
| 196 | |
| 197 | end doDelXattr |
| 198 | |
| 199 | |
| 200 | |
| 201 | |
| 202 | |
| 203 | |
| 204 | ######################## |
| 205 | #パスのエスケープ |
| 206 | to doPathEscape(strFilePath) |
| 207 | set strFilePath to strFilePath as text |
| 208 | set ocidFilePathStr to refMe's NSString's stringWithString:(strFilePath) |
| 209 | set ocidFilePath to ocidFilePathStr's stringByStandardizingPath() |
| 210 | set ocidFilePathURL to refMe's NSURL's fileURLWithPath:(ocidFilePath) isDirectory:(false) |
| 211 | set listEscChar to {"\\", "\"", "$", "`"} as list |
| 212 | repeat with itemEscChar in listEscChar |
| 213 | set ocidFilePath to (ocidFilePath's stringByReplacingOccurrencesOfString:(itemEscChar) withString:("\\\\\\" & itemEscChar & "")) |
| 214 | end repeat |
| 215 | set ocidFilePath to (ocidFilePath's stringByReplacingOccurrencesOfString:("!") withString:("\\\"'!'\\\"")) |
| 216 | |
| 217 | return ocidFilePath |
| 218 | end doPathEscape |
| AppleScriptで生成しました | |
