20260611

ファイル・フォルダの各種属性(拡張属性Extended Attributes (EA))をリセットするクイックアクション

ファイル・フォルダの各種属性(拡張属性Extended Attributes (EA))をリセットするクイックアクション

NOTE記事一覧ですnote.com

【Safari・FireFox用Script Editorで開く】 |

Del_XATTR.scpt.scpt
ソース
001#!/usr/bin/env osascript
002#coding: utf-8
003----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
004(*
005Automatorクイックアクション用
006Service Workflow
007
008右クリックから
009選択実行で
010ファイルの拡張属性を削除します
011xattrコマンドを利用して -cオプションによる
012エクステンド アトリビュートの削除を行います
013
014v1 初回作成
015v2 setFileの属性も削除するようにした
016v2.1 ACLsの属性削除も行うようにした
017v2.2 chflagsの解除も入れるようにした
018v2.3 note公開版
019v2.4 ACLsの削除を再起処理にした
020v3 SIP非対応のフォルダへ移動してから属性を削除する方法に変更
021
022
023
024
025com.cocolog-nifty.quicktimer.icefloe *)
026----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
027use AppleScript version "2.8"
028use framework "Foundation"
029use framework "AppKit"
030use scripting additions
031property refMe : a reference to current application
032
033
034####################################
035#RUN
036(*
037on run {listAliasFilePath}
038   
039   
040テスト用
041      *)
042on run
043   set aliasDefaultLocation to (path to desktop folder from user domain) as alias
044   set listUTI to {"public.item"} as list
045   set strMsg to ("ファイルを選んでください") as text
046   set strPrompt to ("ファイルを選んでください") as text
047   try
048      tell application "SystemUIServer"
049         activate
050         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
051      end tell
052   on error strErrMes number numErrNo
053      log strErrMes & numErrNo
054      return false
055   end try
056   
057   
058   
059   set boolDone to (open listAliasFilePath)
060   return true
061end run
062
063
064####################################
065#ドロップOPEN
066on open listAliasFilePath
067   #エイリアスにしてひとつずつ次工程に回す
068   repeat with itemAliasPath in listAliasFilePath
069      set aliasItemFilePath to itemAliasPath as alias
070      set boolDone to doMoveAndReturn(aliasItemFilePath)
071   end repeat
072   
073   return boolDone
074end open
075
076
077####################################
078#隔離-->処理-->戻す
079on doMoveAndReturn(itemAliasPath)
080   #
081   set appFileManager to refMe's NSFileManager's defaultManager()
082   set ocidTempDirURL to appFileManager's temporaryDirectory()
083   set ocidUserTemp to ocidTempDirURL's URLByDeletingLastPathComponent()
084   set ocidTemporaryItemsPathURL to ocidUserTemp's URLByAppendingPathComponent:("TemporaryItems") isDirectory:(true)
085   #
086   set ocidUUID to refMe's NSUUID's alloc()'s init()
087   set ocidUUIDString to ocidUUID's UUIDString
088   set ocidMoveDirPathURL to ocidTemporaryItemsPathURL's URLByAppendingPathComponent:(ocidUUIDString) isDirectory:(true)
089   #
090   set ocidAttrDict to refMe's NSMutableDictionary's alloc()'s init()
091   # 777-->511 755-->493 700-->448 766-->502 
092   ocidAttrDict's setValue:(511) forKey:(refMe's NSFilePosixPermissions)
093   set listDone to appFileManager's createDirectoryAtURL:(ocidMoveDirPathURL) withIntermediateDirectories:(true) attributes:(ocidAttrDict) |error|:(reference)
094   #
095   set aliasFilePath to itemAliasPath as alias
096   set strFilePath to (POSIX path of aliasFilePath) as text
097   set ocidFilePathStr to refMe's NSString's stringWithString:(strFilePath)
098   set ocidFilePathStr to ocidFilePathStr's precomposedStringWithCanonicalMapping()
099   set ocidFilePath to ocidFilePathStr's stringByStandardizingPath()
100   set ocidFilePath to ocidFilePath's stringByExpandingTildeInPath()
101   set ocidFilePathURL to refMe's NSURL's fileURLWithPath:(ocidFilePath) isDirectory:(false)
102   set ocidFileName to ocidFilePathURL's lastPathComponent()
103   #
104   set ocidMoveFilePathURL to ocidMoveDirPathURL's URLByAppendingPathComponent:(ocidFileName) isDirectory:(false)
105   set ocidMoveFilePath to ocidMoveFilePathURL's |path|()
106   #
107   set appFileManager to refMe's NSFileManager's defaultManager()
108   set listDone to (appFileManager's moveItemAtURL:(ocidFilePathURL) toURL:(ocidMoveFilePathURL) |error|:(reference))
109   #
110   set boolDone to doDelXattr(ocidMoveFilePath)
111   if boolDone is false then
112      set appSharedWorkspace to refMe's NSWorkspace's sharedWorkspace()
113      set boolDone to appSharedWorkspace's openURL:(ocidMoveDirPathURL)
114      set appFinderArray to refMe's NSRunningApplication's runningApplicationsWithBundleIdentifier:("com.apple.finder")
115      set appFinder to appFinderArray's firstObject()
116      set boolDone to appFinder's activateWithOptions:(refMe's NSApplicationActivateIgnoringOtherApps)
117      return false
118   end if
119   #
120   set listDone to (appFileManager's moveItemAtURL:(ocidMoveFilePathURL) toURL:(ocidFilePathURL) |error|:(reference))
121   if (first item of listDone) is false then
122      set appSharedWorkspace to refMe's NSWorkspace's sharedWorkspace()
123      set boolDone to appSharedWorkspace's openURL:(ocidMoveDirPathURL)
124      set appFinderArray to refMe's NSRunningApplication's runningApplicationsWithBundleIdentifier:("com.apple.finder")
125      set appFinder to appFinderArray's firstObject()
126      set boolDone to appFinder's activateWithOptions:(refMe's NSApplicationActivateIgnoringOtherApps)
127      return false
128   end if
129   
130   return true
131end doMoveAndReturn
132
133####################################
134#拡張属性(extended attributes)の削除
135on doDelXattr(argFilePath)
136   #パス UNIXパス
137   set strFilePath to (argFilePath) as text
138   set aliasItemPath to (POSIX file strFilePath) as alias
139   #パスのエスケープ
140   set ocidFilePath to doPathEscape(strFilePath)
141   
142   ###################
143   set strCmd to ("/usr/bin/chflags nouchg,noschg \\\"" & ocidFilePath & "\\\"") as text
144   log "" & linefeed & strCmd & linefeed & ""
145   set strExec to ("/bin/zsh -c \"" & strCmd & "\"") as text
146   log "" & linefeed & strExec & linefeed & ""
147   try
148      log "コマンド開始" & ((current date) as text)
149      set strResnponse to (do shell script strExec) as text
150      log "コマンド終了" & ((current date) as text)
151   on error strErroMsg number numErrorNo
152      log strErroMsg & numErrorNo
153      #      return false -->ここはエラーでも次に回す
154   end try
155   
156   ###################
157   set strCmd to ("/usr/bin/chflags nohidden \\\"" & ocidFilePath & "\\\"") as text
158   log "" & linefeed & strCmd & linefeed & ""
159   set strExec to ("/bin/zsh -c \"" & strCmd & "\"") as text
160   log "" & linefeed & strExec & linefeed & ""
161   try
162      log "コマンド開始" & ((current date) as text)
163      set strResnponse to (do shell script strExec) as text
164      log "コマンド終了" & ((current date) as text)
165   on error strErroMsg number numErrorNo
166      log strErroMsg & numErrorNo
167      #      return false -->ここはエラーでも次に回す
168   end try
169   
170   
171   ###################
172   set recordInfoFor to (info for aliasItemPath) as record
173   set boolIsFolder to (folder of recordInfoFor) as boolean
174   if boolIsFolder is true then
175      set strCmd to ("/usr/bin/SetFile -a lcdeivzst \\\"" & ocidFilePath & "\\\"") as text
176   else if boolIsFolder is false then
177      set strCmd to ("/usr/bin/SetFile -a lst \\\"" & ocidFilePath & "\\\"") as text
178   end if
179   log "" & linefeed & strCmd & linefeed & ""
180   set strExec to ("/bin/zsh -c \"" & strCmd & "\"") as text
181   log "" & linefeed & strExec & linefeed & ""
182   try
183      log "コマンド開始" & ((current date) as text)
184      set strResnponse to (do shell script strExec) as text
185      log "コマンド終了" & ((current date) as text)
186   on error strErroMsg number numErrorNo
187      log strErroMsg & numErrorNo
188      #      return false -->ここはエラーでも次に回す
189   end try
190   
191   ###################
192   set strCmd to ("/bin/chmod -RN \\\"" & ocidFilePath & "\\\"") as text
193   log "" & linefeed & strCmd & linefeed & ""
194   set strExec to ("/bin/zsh -c \"" & strCmd & "\"") as text
195   log "" & linefeed & strExec & linefeed & ""
196   try
197      log "コマンド開始" & ((current date) as text)
198      set strResnponse to (do shell script strExec) as text
199      log "コマンド終了" & ((current date) as text)
200   on error strErroMsg number numErrorNo
201      log strErroMsg & numErrorNo
202      #      return false -->ここはエラーでも次に回す
203   end try
204   
205   
206   ###################
207   set strCmd to ("/usr/bin/xattr -rc \\\"" & ocidFilePath & "\\\"") as text
208   log "" & linefeed & strCmd & linefeed & ""
209   set strExec to ("/bin/zsh -c \"" & strCmd & "\"") as text
210   log "" & linefeed & strExec & linefeed & ""
211   try
212      log "コマンド開始" & ((current date) as text)
213      set strResnponse to (do shell script strExec) as text
214      log "コマンド終了" & ((current date) as text)
215   on error strErroMsg number numErrorNo
216      log strErroMsg & numErrorNo
217      return false
218   end try
219   
220   #ここまでにした
221   return true
222   
223   ###################
224   (*
225      set strCmd to ("/usr/bin/xattr -rd com.apple.ResourceFork \\\"" & ocidFilePath & "\\\"") as text
226      log "" & linefeed & strCmd & linefeed & ""
227      set strExec to ("/bin/zsh -c \"" & strCmd & "\"") as text
228      log "" & linefeed & strExec & linefeed & ""
229      try
230         log "コマンド開始" & ((current date) as text)
231         set strResnponse to (do shell script strExec) as text
232         log "コマンド終了" & ((current date) as text)
233      on error strErroMsg number numErrorNo
234         log strErroMsg & numErrorNo
235         return false
236      end try
237   
238   ###################
239   
240   set strCmd to ("/usr/bin/xattr -rd com.apple.quarantine \\\"" & ocidFilePath & "\\\"") as text
241   log "" & linefeed & strCmd & linefeed & ""
242   set strExec to ("/bin/zsh -c \"" & strCmd & "\"") as text
243   log "" & linefeed & strExec & linefeed & ""
244   try
245      log "コマンド開始" & ((current date) as text)
246      set strResnponse to (do shell script strExec) as text
247      log "コマンド終了" & ((current date) as text)
248   on error strErroMsg number numErrorNo
249      log strErroMsg & numErrorNo
250      return false
251   end try
252   *)
253   
254   
255end doDelXattr
256
257
258
259
260
261
262########################
263#パスのエスケープ
264to doPathEscape(strFilePath)
265   set strFilePath to strFilePath as text
266   set ocidFilePathStr to refMe's NSString's stringWithString:(strFilePath)
267   set ocidFilePath to ocidFilePathStr's stringByStandardizingPath()
268   set ocidFilePathURL to refMe's NSURL's fileURLWithPath:(ocidFilePath) isDirectory:(false)
269   set listEscChar to {"\\", "\"", "$", "`"} as list
270   repeat with itemEscChar in listEscChar
271      set ocidFilePath to (ocidFilePath's stringByReplacingOccurrencesOfString:(itemEscChar) withString:("\\\\\\" & itemEscChar & ""))
272   end repeat
273   set ocidFilePath to (ocidFilePath's stringByReplacingOccurrencesOfString:("!") withString:("\\\"'!'\\\""))
274   
275   return ocidFilePath
276end doPathEscape
AppleScriptで生成しました