« Export image file or get Base64 Photoshop layers | トップページ | [AppleScripts]iPhoneアプリ用に最適化されたPNGフォーマット“CgBI” iOS formatのPNGファイルを閲覧可能な形式に戻す »

[okwave]Applescriptでファイル名の一部を置換


Applescriptでファイル名の一部を置換 | MacintoshのQ&A【OKWave】


を作ってみます。

ポイントは
『数字の部分』の処理ですね。
後は文字を置き換えれば良いのでさほど難しくない。

ちょっと考えて
こんな風に考えてみました。


Website_image20130404_04717


on run
------------------------------------------------【設定項目】ファイル選択ダイアログのデフォルトのディレクトリ
set theDefLoc to path to desktop from user domain
------------------------------------------------【設定項目】Uniform Type Identifier指定
------------------------------------------------詳しくは http://goo.gl/6jAQa Uniform Type Identifierを見てください
set theFileType to "public.pdf,com.adobe.pdf" as text
---簡素の記述も出来ますPDFの場合---set theFileType to "PDF" as text
---ムービーファイルの場合---set theFileType to "public.movie,com.adobe.flash-video" as text
------------------------------------------------のファイルタイプをリスト形式に整形する
set AppleScript's text item delimiters to {","}
set theFileTypeList to every text item of theFileType
------------------------------------------------【設定項目】プロンプトの文言改行が使えます\nを入れます
------------------------------------------------Uniform Type Identifierとメッセージ
set theWithPrompt to theFileType & "\nファイルをえらんでください\n"
------------------------------------------------残りのメッセージ
set theWithPromptMes to "ファイルをドロップしても\n動作します"
------------------------------------------------ダイアログを出して選択されたファイルは「open」に渡す
open (choose file default location theDefLoc ¬
with prompt theWithPrompt & theWithPromptMes ¬
of type theFileTypeList ¬
invisibles true ¬
with multiple selections allowed without showing package contents)
end run


on open theDropObj
-----繰り返しのはじまり
repeat with theObjFiles in theDropObj

----ファイルインフォを取得
set theInfoChooseFile to info for theObjFiles
----拡張子を取得
set theExtension to name extension of theInfoChooseFile
----ファイル名を取得
set theFileName to name of theInfoChooseFile
----ファイルのエイリアスをテキストに変換
set theFilePath to (theObjFiles as alias) as text

----ファイル名の文字数を数える
set theCntFileName to the length of characters of theFileName as number
----パスの文字数を数える
set theCntFilePath to the length of characters of theFilePath as number
----パスの文字数 ー ファイル名でディレクトリ名の文字数を計算する
set theCntDirPath to theCntFilePath - theCntFileName as number
----ファイルのディレクトリ名を定義する
set theDirName to characters 1 thru theCntDirPath of (theFilePath as text)
set theDirName to my ReplaceText(theDirName, ",", "") as alias

--------ファイル名から必要部分を抜き出し
set theMainFileName to do shell script "echo " & theFileName & " | awk -F\"[\" '{print($1)}'"
set theAfterFileName to do shell script "echo " & theFileName & " | awk -F\"[\" '{print($2)}'"
--------不要な文字を削除する
set theAfterFileName to my ReplaceText(theAfterFileName, "[", "")
set theAfterFileName to my ReplaceText(theAfterFileName, "]", "")
set theAfterFileName to my ReplaceText(theAfterFileName, "-", "")
set theAfterFileName to my ReplaceText(theAfterFileName, ".", "")
------出来た文字を数字にしておく
set theFileNumber to my ReplaceText(theAfterFileName, theExtension, "") as number


----桁数によって連番を取得する
if theFileNumber < 100 then
set theFileNumber to character 1 of (theFileNumber as text) as text
set theFileNumber to my ReplaceText(theFileNumber, ",", "") as number
set theFileNumber to my ZeroSuppress(theFileNumber) as text
else if theFileNumber < 10000 then
set theFileNumber to characters 1 thru 2 of (theFileNumber as text) as text
set theFileNumber to my ReplaceText(theFileNumber, ",", "") as number
set theFileNumber to my ZeroSuppress(theFileNumber) as text
else
set theFileNumber to characters 1 thru 3 of (theFileNumber as text) as text
set theFileNumber to my ReplaceText(theFileNumber, ",", "") as number
set theFileNumber to my ZeroSuppress(theFileNumber) as text
end if
----新しいファイル名を作成
set theNewFileName to theMainFileName & "P" & theFileNumber & "." & theExtension


----ファイル名を変更する
tell application "Finder"
set name of file theFileName of folder theDirName to theNewFileName
end tell
end repeat
end open








----文字の置き換えサブルーチン
to ReplaceText(theText, orgStr, newStr)
set oldDelim to AppleScript's text item delimiters
set AppleScript's text item delimiters to orgStr
set tmpList to every text item of theText
set AppleScript's text item delimiters to newStr
set tmpStr to tmpList as text
set AppleScript's text item delimiters to oldDelim
return tmpStr
end ReplaceText


----ゼロサプレスのサブルーチン
to ZeroSuppress(n)
if n < 10 then
return "00" & n
else if n < 100 then
return "0" & n
else
return n as text
end if
end ZeroSuppress


「q8024033.rtf」をダウンロード


「q8024033.zip」をダウンロード

|

« Export image file or get Base64 Photoshop layers | トップページ | [AppleScripts]iPhoneアプリ用に最適化されたPNGフォーマット“CgBI” iOS formatのPNGファイルを閲覧可能な形式に戻す »

AppleScriptRename」カテゴリの記事