mixiユーザー(id:2760672)

2018年05月22日09:17

194 view

ファイル名にキーワードを入れるAppleScript

 exif を編集する前に、取りあえず名前にキーワードを混ぜる AppleScript ドロップレットを作ってみた。

 以下のスクリプトをスクリプトエディタに入れて、ドロップレットとして保存する。ファイル(複数可)をドロップレットにドロップするとキーワードを尋ねてくるので、入力するとファイル名の末尾に "_" に続いてそのワードが入る。ex. img255.jpg -> img255_wow.jpg

-- addWord2Name droplet 2018.5.21

on open fileList --ドロップレットにファイルがドロップされた時に実行される
-- ファイル名の末尾に付けるキーワードを入力
display dialog "ファイル名に付加するワードを入力" default answer "wow"
set myKeyWord to (text returned of the result) as Unicode text --返り値リストからテキストを取り出す
if myKeyWord is not "" then -- 文字列が入っていたら以下を実行
-- ドロップされた全てのファイルの末尾にキーワードを付加
my editName(fileList, myKeyWord)
else
display dialog "ファイル名は変更されませんでした。" buttons "OK" default button "OK"
end if
end open

on editName(fileList, myKeyWord)
repeat with tgFileAlias in fileList -- ドロップされた全てのファイルについて繰り返し
set tgPath to tgFileAlias as Unicode text -- ファイルのフルパス文字列を得る
set parentPath to getParentPathFromFullPath(tgPath) -- 親フォルダのパス
set fileName to getFileNameFromFullPath(tgPath) -- ファイル名
set realName to getRealNameFromFullName(fileName) -- 拡張子を含まないファイル名
set extName to getExtNameFromFullName(fileName) -- 拡張子
-- ファイル名に "_" に続いてキーワードを付加 ex. img255.jpg -> img255_wow.jpg
set newName to (realName & "_" & myKeyWord & "." & extName) as Unicode text
tell application "Finder"
set name of file fileName of folder parentPath to newName
end tell
end repeat
end editName

on getParentPathFromFullPath(tgPath) -- フルパス文字列からフォルダパス(コロンで終わらない)を得る
set the AppleScript's text item delimiters to ":"
set colonCount to (the number of text items of tgPath) - 1
return (text items 1 thru colonCount of tgPath) as Unicode text
end getParentPathFromFullPath

on getFileNameFromFullPath(tgPath) -- フルパス文字列からファイル名を得る
set the AppleScript's text item delimiters to ":"
return (last text item of tgPath) as Unicode text
end getFileNameFromFullPath

on getRealNameFromFullName(fileName) -- ファイル名から拡張子を除いた名前を得る
set the AppleScript's text item delimiters to "."
set commaCount to (the number of text items of fileName) - 1
return (text items 1 thru commaCount of fileName) as Unicode text
end getRealNameFromFullName

on getExtNameFromFullName(fileName) -- ファイル名から拡張子(ピリオドを含まない)を得る
set the AppleScript's text item delimiters to "."
return (last text item of fileName) as Unicode text
end getExtNameFromFullName

 たったこれだけのスクリプトに2日かかった。 C は好きだけど C++ は無理、HyperTalk は好きだけど AppleScript は苦手。注釈がたくさん付いているのは当然「自分のため」。AppleScript は視認性があまり良くないので、いちいちサブルーチンに分けてみた。こうしておくと使い回しが出来てラクだし。

 久しぶりのプログラミングだった。目が辛いなぁ...
1 0

コメント

mixiユーザー

ログインしてコメントを確認・投稿する