20260519

[Applescript]拡張子の無いファイルにexiftoolで拡張子を調べて拡張子を付ける

[Applescript]拡張子の無いファイルにexiftoolで拡張子を調べて拡張子を付ける

NOTE記事一覧ですnote.com
 

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

exif2extension.applescript.scpt
ソース
001#! /usr/bin/env osascript
002----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
003(*
004拡張子の無いデータで
005exiftoolを使って拡張子判定をして拡張子を付与します
006exiftoolの判定はまぁ9割は正しいかな
007
008
009v1 初回作成
010
011com.cocolog-nifty.quicktimer.icefloe *)
012----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
013use AppleScript version "2.8"
014use framework "Foundation"
015use framework "AppKit"
016use scripting additions
017
018property refMe : a reference to current application
019set appFileManager to refMe's NSFileManager's defaultManager()
020
021################################################
022###設定項目 ffmpegへのパス
023#set strBinFilePath to ("/usr/local/bin/exiftool/exiftool") as text
024set strBinFilePath to ("~/Library/Application Support/bin/exiftool/exiftool") as text
025################################################
026set ocidBinPathStr to refMe's NSString's stringWithString:(strBinFilePath)
027set ocidBinPath to ocidBinPathStr's stringByStandardizingPath()
028set ocidBinPath to ocidBinPath's stringByExpandingTildeInPath()
029set ocidBinPath to ocidBinPath's stringByStandardizingPath()
030set strBinFilePath to ocidBinPath as text
031
032
033#############################
034###入力ファイル
035#############################
036tell current application
037   set strName to name as text
038end tell
039####スクリプトメニューから実行したら
040if strName is "osascript" then
041   tell application "Finder" to activate
042else
043   tell current application to activate
044end if
045############ デフォルトロケーション
046set appFileManager to refMe's NSFileManager's defaultManager()
047set ocidURLsArray to (appFileManager's URLsForDirectory:(refMe's NSDesktopDirectory) inDomains:(refMe's NSUserDomainMask))
048set ocidDesktopDirPathURL to ocidURLsArray's firstObject()
049set aliasDefaultLocation to (ocidDesktopDirPathURL's absoluteURL()) as alias
050####UTIリスト
051set listUTI to {"public.item"} as list
052####ダイアログを出す
053tell application "SystemUIServer"
054   activate
055   set listAliasFilePath to (choose file with prompt "ファイルを選んでください" default location (aliasDefaultLocation) of type listUTI with invisibles, showing package contents and multiple selections allowed) as list
056end tell
057
058repeat with itemFilePath in listAliasFilePath
059   
060   ###入力ファイル
061   set strFilePath to (POSIX path of itemFilePath) as text
062   set ocidFilePathStr to (refMe's NSString's stringWithString:(strFilePath))
063   set ocidFilePath to ocidFilePathStr's stringByStandardizingPath()
064   set ocidFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidFilePath) isDirectory:false)
065   #拡張子を取る
066   set ocidBaseFilePathURL to ocidFilePathURL's URLByDeletingPathExtension()
067   
068   #############################
069   #拡張子の取得
070   set strBinFilePath to doPathEscape(strBinFilePath)
071   set strFilePath to doPathEscape(strFilePath)
072   set strCommandText to ("\"" & strBinFilePath & "\"  -FileTypeExtension \"" & strFilePath & "\"") as text
073   try
074      #コマンド実行
075      set strResponse to (do shell script strCommandText) as text
076      #戻り値をスペース区切りでリストにして
077      set AppleScript's text item delimiters to " "
078      set listResponse to every text item of strResponse
079      set AppleScript's text item delimiters to ""
080      #最後の項目が戻り値としての拡張子
081      set strExtension to last item of listResponse
082   on error
083      set strExtension to "" as text
084   end try
085   #拡張子を付与して
086   set ocidSaveFilePathURL to (ocidBaseFilePathURL's URLByAppendingPathExtension:(strExtension))
087   #移動でリネーム
088   set listDone to (appFileManager's moveItemAtURL:(ocidFilePathURL) toURL:(ocidSaveFilePathURL) |error|:(reference))
089   
090   if (item 1 of listDone) is true then
091      log "正常処理"
092   else if (item 2 of listDone) ≠ (missing value) then
093      log (item 2 of listDone)'s code() as text
094      log (item 2 of listDone)'s localizedDescription() as text
095      return "エラーしました"
096   end if
097   
098end repeat
099
100
101
102return
103
104
105
106
107########################
108#パスのエスケープ
109to doPathEscape(strFilePath)
110   set strFilePath to strFilePath as text
111   set ocidFilePathStr to refMe's NSString's stringWithString:(strFilePath)
112   set ocidFilePath to ocidFilePathStr's stringByStandardizingPath()
113   set ocidFilePathURL to refMe's NSURL's fileURLWithPath:(ocidFilePath) isDirectory:(false)
114   set listEscChar to {"\\", "\"", "$", "`"} as list
115   repeat with itemEscChar in listEscChar
116      set ocidFilePath to (ocidFilePath's stringByReplacingOccurrencesOfString:(itemEscChar) withString:("\\" & itemEscChar & ""))
117   end repeat
118   set ocidFilePath to (ocidFilePath's stringByReplacingOccurrencesOfString:("!") withString:("\"'!'\""))
119   
120   return ocidFilePath
121end doPathEscape
AppleScriptで生成しました