20260604

【exiftool】メタデータの削除

【exiftool】メタデータの削除

NOTE記事一覧ですnote.com
 

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

Del_Meta.scpt.scpt
ソース
001#!/usr/bin/env osascript
002#coding: utf-8
003----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
004(*
005前提条件
006PDFKitで保存するのでCreatorの属性にQuartz PDFContextが入る
007別名で保存からアプリケーションでドロップレットになります
008
009EXIFTOOLを使います
010簡易インストール機能がついています
011インストール先は
012/Users/ユーザーID/Library/Application Support/bin/exiftool
013
014メタデータを綺麗さっぱりクリーニングしますPDF専用
015
016v1 初回作成
017v1.1 不具合修正
018v1.2 パスのエスケープを見直し
019
020com.cocolog-nifty.quicktimer.icefloe *)
021----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
022use AppleScript version "2.8"
023use framework "Foundation"
024use framework "AppKit"
025use framework "PDFKit"
026use scripting additions
027property refMe : a reference to current application
028
029
030###################
031#run
032#on run {listAliasFilePath}
033
034(*
035   #テスト用ダイアログ
036      *)
037on run
038   set listAliasFilePath to {} as list
039   if listAliasFilePath = {} then
040      
041      ###ダイアログを前面に出す
042      set appFileManager to refMe's NSFileManager's defaultManager()
043      set ocidUserDesktopPathArray to (appFileManager's URLsForDirectory:(refMe's NSDesktopDirectory) inDomains:(refMe's NSUserDomainMask))
044      set ocidUserDesktopPath to ocidUserDesktopPathArray's firstObject()
045      set listChooseFileUTI to {"public.data"} as list
046      set strMesText to ("ファイルを選んでください") as text
047      set strPromptText to ("ファイルを選んでください") as text
048      
049      tell application "SystemUIServer"
050         activate
051         set listAliasFilePath to (choose file strMesText with prompt strPromptText default location (ocidUserDesktopPath as alias) of type listChooseFileUTI with invisibles and multiple selections allowed without showing package contents) as list
052      end tell
053   end if
054   
055   
056   set boolDone to (open listAliasFilePath)
057   return doQuitSelf()
058end run
059
060###################
061#open
062on open listAliasFilePath
063   
064   ###################
065   #インストールチェック
066   set refChk to doChkInstall("exiftool")
067   if refChk is false then
068      set refInstall to doInstallBin()
069      if refInstall is false then
070         log "インストールに失敗しました"
071         tell application "Finder"
072            activate
073            display alert ("exiftoolのインストールに失敗しました" & linefeed & "処理を中止します" & "") buttons {"OK"} default button "OK" cancel button "OK" as informational giving up after 10
074         end tell
075         return false
076      else
077         set strExifPath to refInstall as text
078      end if
079   else
080      set strExifPath to refChk as text
081   end if
082   #exiftoolパス処理
083   set ocidExifPathStr to refMe's NSString's stringWithString:(strExifPath)
084   set ocidExifPath to ocidExifPathStr's stringByStandardizingPath()
085   set ocidExifPathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidExifPath) isDirectory:false)
086   set ocidExifPath to ocidExifPathURL's |path|()
087   
088   
089   #ファイルリストを順番に
090   repeat with itemAliasFilePath in listAliasFilePath
091      #エイリアス
092      set aliasItemFilePath to itemAliasFilePath as alias
093      #Finder情報を取得して
094      set recordInfoFor to (info for aliasItemFilePath) as record
095      #フォルダか?確認して
096      set boolIsDir to (folder of recordInfoFor) as boolean
097      #フォルダではない=ファイルで判定処理
098      if boolIsDir is false then
099         set boolDone to doAction(aliasItemFilePath, ocidExifPath)
100      end if
101   end repeat
102   
103   return boolDone
104end open
105
106
107###################
108#実行されるのはこれ
109to doAction(aliasItemFilePath, ocidExifPath)
110   #NULLテキスト初期設定
111   set strNullText to (space) as text
112   set ocidNULLstring to (refMe's NSString's stringWithString:(strNullText))
113   
114   #ファイルエイリアスリストを順番の処理
115   set aliasFilePath to aliasItemFilePath as alias
116   set strFilePath to (POSIX path of aliasFilePath) as text
117   set ocidFilePathStr to (refMe's NSString's stringWithString:(strFilePath))
118   set ocidFilePath to ocidFilePathStr's stringByStandardizingPath()
119   set ocidFilePathURL to refMe's NSURL's alloc()'s initFileURLWithPath:(ocidFilePath) isDirectory:(false)
120   #ファイル名処理
121   set ocidFileName to ocidFilePathURL's lastPathComponent()
122   set ocidBaseFileName to ocidFileName's stringByDeletingPathExtension()
123   set strMakeDirName to ("" & ocidBaseFileName & "_メタ削除済") as text
124   set ocidContainerDirPathURL to ocidFilePathURL's URLByDeletingLastPathComponent()
125   set ocidSaveDirPathURL to ocidContainerDirPathURL's URLByAppendingPathComponent:(strMakeDirName) isDirectory:(true)
126   #保存先新規フォルダ(同階層)
127   set appFileManager to refMe's NSFileManager's defaultManager()
128   set ocidAttrDict to refMe's NSMutableDictionary's alloc()'s init()
129   ocidAttrDict's setValue:(511) forKey:(refMe's NSFilePosixPermissions)
130   set listDone to appFileManager's createDirectoryAtURL:(ocidSaveDirPathURL) withIntermediateDirectories:(true) attributes:(ocidAttrDict) |error|:(reference)
131   #保存先
132   set ocidSaveFilePathURL to ocidSaveDirPathURL's URLByAppendingPathComponent:(ocidFileName) isDirectory:(false)
133   ##処理できそうならファイルをコピーして
134   set appFileManager to refMe's NSFileManager's defaultManager()
135   set listDone to (appFileManager's copyItemAtURL:(ocidFilePathURL) toURL:(ocidSaveFilePathURL) |error|:(reference))
136   set ocidSaveFilePath to ocidSaveFilePathURL's |path|()
137   #パスエスケープ
138   set ocidExifPath to doPathEscape(ocidExifPath)
139   set ocidSaveFilePath to doPathEscape(ocidSaveFilePath)
140   
141   ###メタ削除
142   #コマンド整形
143   set strCmd to ("\"" & ocidExifPath & "\" -all= -overwrite_original \"" & ocidSaveFilePath & "\"") as text
144   #コマンド実行
145   try
146      set strJsonString to (do shell script strCmd) as text
147   on error strErrMsg number numErrNo
148      log strErrMsg & numErrNo
149      set strJsonString to strErrMsg
150      return false
151   end try
152   return true
153end doAction
154
155########################
156#パスのエスケープ
157to doPathEscape(strFilePath)
158   set strFilePath to strFilePath as text
159   set ocidFilePathStr to refMe's NSString's stringWithString:(strFilePath)
160   set ocidFilePath to ocidFilePathStr's stringByStandardizingPath()
161   set ocidFilePathURL to refMe's NSURL's fileURLWithPath:(ocidFilePath) isDirectory:(false)
162   set listEscChar to {"\\", "\"", "$", "`"} as list
163   repeat with itemEscChar in listEscChar
164      set ocidFilePath to (ocidFilePath's stringByReplacingOccurrencesOfString:(itemEscChar) withString:("\\" & itemEscChar & ""))
165   end repeat
166   set ocidFilePath to (ocidFilePath's stringByReplacingOccurrencesOfString:("!") withString:("\"'!'\""))
167   
168   return ocidFilePath
169end doPathEscape
170
171
172##############################
173#exiftool インストール
174#管理者権限不要タイプ
175to doInstallBin()
176   set strVerURL to ("https://exiftool.org/ver.txt") as text
177   set strInstrallDirPath to ("~/Library/Application Support/bin/exiftool") as text
178   #インストール先
179   set ocidInstrallDirPathStr to refMe's NSString's stringWithString:(strInstrallDirPath)
180   set ocidInstrallDirPath to ocidInstrallDirPathStr's stringByStandardizingPath()
181   set ocidInstrallDirPathURL to refMe's NSURL's alloc()'s initFileURLWithPath:(ocidInstrallDirPath) isDirectory:(true)
182   set ocidBinFilePathURL to ocidInstrallDirPathURL's URLByAppendingPathComponent:("exiftool") isDirectory:(false)
183   set strBinPath to ocidBinFilePathURL's |path|() as text
184   #フォルダを作る
185   set appFileManager to refMe's NSFileManager's defaultManager()
186   set ocidAttrDict to refMe's NSMutableDictionary's alloc()'s init()
187   ocidAttrDict's setValue:(448) forKey:(refMe's NSFilePosixPermissions)
188   set listDone to appFileManager's createDirectoryAtURL:(ocidInstrallDirPathURL) withIntermediateDirectories:true attributes:(ocidAttrDict) |error|:(reference)
189   set strInstrallDirPath to (ocidInstrallDirPathURL's |path|()) as text
190   #バージョン番号を取得する
191   set ocidVerURLStr to refMe's NSString's stringWithString:(strVerURL)
192   set ocidVerURL to refMe's NSURL's alloc()'s initWithString:(ocidVerURLStr)
193   set listResponse to refMe's NSString's stringWithContentsOfURL:(ocidVerURL) encoding:(refMe's NSUTF8StringEncoding) |error|:(reference)
194   set strVer to (first item of listResponse) as text
195   #バージョンチェック 同じならインストール処理しない
196   set strCmd to ("\"" & strBinPath & "\" -ver") as text
197   try
198      set strResponse to (do shell script strCmd) as text
199      if strVer is strResponse then
200         return strBinPath
201      end if
202   end try
203   #ダウンロードディレクトリ
204   set ocidTempDirURL to appFileManager's temporaryDirectory()
205   set ocidUUID to refMe's NSUUID's alloc()'s init()
206   set ocidUUIDString to ocidUUID's UUIDString
207   set ocidSaveDirPathURL to ocidTempDirURL's URLByAppendingPathComponent:(ocidUUIDString) isDirectory:(true)
208   ocidAttrDict's setValue:(511) forKey:(refMe's NSFilePosixPermissions)
209   set listDone to appFileManager's createDirectoryAtURL:(ocidSaveDirPathURL) withIntermediateDirectories:true attributes:(ocidAttrDict) |error|:(reference)
210   #解凍先ディレクトリ
211   set ocidExpandDirPathURL to ocidSaveDirPathURL's URLByAppendingPathComponent:("ExifTool") isDirectory:(true)
212   set strExpandDirPath to (ocidExpandDirPathURL's |path|()) as text
213   #URLとファイル名の整形
214   set strPkgName to ("ExifTool-" & strVer & ".pkg") as text
215   #保存URL
216   set ocidSavePkgFilePathURL to ocidSaveDirPathURL's URLByAppendingPathComponent:(strPkgName)
217   set strSavePkgFilePath to (ocidSavePkgFilePathURL's |path|()) as text
218   #URL
219   set strURL to ("https://exiftool.org/" & strPkgName & "") as text
220   set ocidURLStr to refMe's NSString's stringWithString:(strURL)
221   set ocidURL to refMe's NSURL's alloc()'s initWithString:(ocidURLStr)
222   #ダウンロード
223   set ocidOption to (refMe's NSDataReadingMappedAlways)
224   set listResponse to refMe's NSData's alloc()'s initWithContentsOfURL:(ocidURL) options:(ocidOption) |error|:(reference)
225   set ocidPkgData to (first item of listResponse)
226   #保存
227   set ocidOption to (refMe's NSDataWritingAtomic)
228   set listDone to ocidPkgData's writeToURL:(ocidSavePkgFilePathURL) options:(ocidOption) |error|:(reference)
229   #pkg解凍
230   set strCmd to "/usr/sbin/pkgutil --expand-full \"" & strSavePkgFilePath & "\" \"" & strExpandDirPath & "\"" as text
231   try
232      do shell script strCmd
233   on error strErrMsg number numErrNo
234      log strErrMsg & numErrNo
235      return false
236   end try
237   #古い方をゴミ箱に
238   set listDone to (appFileManager's trashItemAtURL:(ocidInstrallDirPathURL) resultingItemURL:(ocidInstrallDirPathURL) |error|:(reference))
239   #ユーザーディレクトリにコピー
240   set ocidDittoDirPathURL to ocidExpandDirPathURL's URLByAppendingPathComponent:("Payload/usr/local/bin") isDirectory:(true)
241   set strDittoDirPath to (ocidDittoDirPathURL's |path|()) as text
242   set strCmd to "/usr/bin/ditto \"" & strDittoDirPath & "\" \"" & strInstrallDirPath & "\"" as text
243   try
244      do shell script strCmd
245   on error strErrMsg number numErrNo
246      log strErrMsg & numErrNo
247      return false
248   end try
249   
250   #ダウンロード解凍前データをゴミ箱へ
251   set appFileManager to refMe's NSFileManager's defaultManager()
252   set listDone to (appFileManager's trashItemAtURL:(ocidSaveDirPathURL) resultingItemURL:(ocidSaveDirPathURL) |error|:(reference))
253   
254   
255   return strBinPath
256   
257end doInstallBin
258
259##############################
260#すでにインストール済みで
261#ユーザーがRC設定している場合は
262#パスが通るのでこれがエラーにならない
263to doChkInstall(argBinName)
264   try
265      #ランチD
266      set strCmd to ("/bin/zsh -c '/usr/bin/which " & argBinName & "'")
267      set strResponse to (do shell script strCmd) as text
268      return strResponse
269   on error strErrMsg number numErrNo
270      set strCmd to ("/bin/zsh -c \"[[ -f '/usr/local/bin/" & argBinName & "' ]] && echo \\\"TRUE\\\" || echo \\\"FALSE\\\"\"")
271      set strResponse to (do shell script strCmd) as text
272      if strResponse is "FALSE" then
273         return false
274      else if strResponse is "TRUE" then
275         set strResponse to ("/usr/local/bin/" & argBinName & "")
276      end if
277   end try
278end doChkInstall
279
280##############################
281to doQuitSelf()
282   set listBundleID to {"com.apple.automator.xpc.runner", "com.apple.automator.xpc.workflowServiceRunner"} as list
283   repeat with itemBundleID in listBundleID
284      set ocidAppArray to (refMe's NSRunningApplication's runningApplicationsWithBundleIdentifier:(itemBundleID))
285      repeat with itemApp in ocidAppArray
286         try
287            set boolDone to itemApp's terminate()
288         end try
289      end repeat
290   end repeat
291   try
292      tell application "System Events" to quit
293   end try
294   refMe's NSRunningApplication's terminateAutomaticallyTerminableApplications()
295   return true
296end doQuitSelf
AppleScriptで生成しました