| 001 | #! /usr/bin/env osascript |
| 002 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
| 003 | (* |
| 004 | 拡張子の無いデータで |
| 005 | exiftoolを使って拡張子判定をして拡張子を付与します |
| 006 | exiftoolの判定はまぁ9割は正しいかな |
| 007 |
|
| 008 |
|
| 009 | v1 初回作成 |
| 010 |
|
| 011 | com.cocolog-nifty.quicktimer.icefloe *) |
| 012 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
| 013 | use AppleScript version "2.8" |
| 014 | use framework "Foundation" |
| 015 | use framework "AppKit" |
| 016 | use scripting additions |
| 017 |
|
| 018 | property refMe : a reference to current application |
| 019 | set appFileManager to refMe's NSFileManager's defaultManager() |
| 020 |
|
| 021 | ################################################ |
| 022 | ###設定項目 ffmpegへのパス |
| 023 | #set strBinFilePath to ("/usr/local/bin/exiftool/exiftool") as text |
| 024 | set strBinFilePath to ("~/Library/Application Support/bin/exiftool/exiftool") as text |
| 025 | ################################################ |
| 026 | set ocidBinPathStr to refMe's NSString's stringWithString:(strBinFilePath) |
| 027 | set ocidBinPath to ocidBinPathStr's stringByStandardizingPath() |
| 028 | set ocidBinPath to ocidBinPath's stringByExpandingTildeInPath() |
| 029 | set ocidBinPath to ocidBinPath's stringByStandardizingPath() |
| 030 | set strBinFilePath to ocidBinPath as text |
| 031 |
|
| 032 |
|
| 033 | ############################# |
| 034 | ###入力ファイル |
| 035 | ############################# |
| 036 | tell current application |
| 037 | set strName to name as text |
| 038 | end tell |
| 039 | ####スクリプトメニューから実行したら |
| 040 | if strName is "osascript" then |
| 041 | tell application "Finder" to activate |
| 042 | else |
| 043 | tell current application to activate |
| 044 | end if |
| 045 | ############ デフォルトロケーション |
| 046 | set appFileManager to refMe's NSFileManager's defaultManager() |
| 047 | set ocidURLsArray to (appFileManager's URLsForDirectory:(refMe's NSDesktopDirectory) inDomains:(refMe's NSUserDomainMask)) |
| 048 | set ocidDesktopDirPathURL to ocidURLsArray's firstObject() |
| 049 | set aliasDefaultLocation to (ocidDesktopDirPathURL's absoluteURL()) as alias |
| 050 | ####UTIリスト |
| 051 | set listUTI to {"public.item"} as list |
| 052 | ####ダイアログを出す |
| 053 | tell 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 |
| 056 | end tell |
| 057 |
|
| 058 | repeat 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 | |
| 098 | end repeat |
| 099 |
|
| 100 |
|
| 101 |
|
| 102 | return |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 | ######################## |
| 108 | #パスのエスケープ |
| 109 | to 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 |
| 121 | end doPathEscape |