001 | #!/usr/bin/env osascript |
---|
002 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
---|
003 | # com.cocolog-nifty.quicktimer.icefloe |
---|
004 | # |
---|
005 | # 同名ファイルに対応 |
---|
006 | # |
---|
007 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
---|
008 | use AppleScript version "2.8" |
---|
009 | use scripting additions |
---|
010 | |
---|
011 | |
---|
012 | |
---|
013 | #ファイルなりフォルダなりを選択する |
---|
014 | tell application "Finder" |
---|
015 | activate |
---|
016 | set listAliasPath to selection |
---|
017 | end tell |
---|
018 | #ダイアログ |
---|
019 | set strName to (name of current application) as text |
---|
020 | if strName is "osascript" then |
---|
021 | tell application "Finder" to activate |
---|
022 | else |
---|
023 | tell current application to activate |
---|
024 | end if |
---|
025 | set aliasIconPath to (POSIX file "/System/Library/CoreServices/Finder.app/Contents/Resources/Finder.icns") as alias |
---|
026 | tell application "Finder" |
---|
027 | set strDefaultAnswer to (the clipboard as text) as text |
---|
028 | end tell |
---|
029 | |
---|
030 | try |
---|
031 | set recordResponse to (display dialog "拡張子を入力してください\n『.』カンマ不要" with title "入力してください" default answer strDefaultAnswer buttons {"OK", "キャンセル"} default button "OK" cancel button "キャンセル" with icon aliasIconPath giving up after 30 without hidden answer) |
---|
032 | on error |
---|
033 | log "エラーしました" |
---|
034 | return "エラーしました" |
---|
035 | end try |
---|
036 | if true is equal to (gave up of recordResponse) then |
---|
037 | return "時間切れですやりなおしてください" |
---|
038 | end if |
---|
039 | if "OK" is equal to (button returned of recordResponse) then |
---|
040 | set strResponse to (text returned of recordResponse) as text |
---|
041 | else |
---|
042 | log "キャンセルしました" |
---|
043 | return "キャンセルしました" |
---|
044 | end if |
---|
045 | ##改行とタブを削除しておく |
---|
046 | set strDistExtension to doReplace(strResponse, "\n", "") |
---|
047 | set strDistExtension to doReplace(strDistExtension, "\t", "") |
---|
048 | set strDistExtension to doReplace(strDistExtension, " ", "") |
---|
049 | #処理開始 |
---|
050 | tell application "Finder" |
---|
051 | |
---|
052 | repeat with itemAliasPath in listAliasPath |
---|
053 | #エイリアスで確定 |
---|
054 | set aliasPath to itemAliasPath as alias |
---|
055 | #プロパティのレコードを取得して |
---|
056 | set recordProperties to properties of aliasPath |
---|
057 | #クラスを取得 |
---|
058 | set classPath to class of recordProperties |
---|
059 | ##フォルダの場合 |
---|
060 | if classPath is folder then |
---|
061 | ##フォルダの場合は処理しない |
---|
062 | else |
---|
063 | #フォルダでないならファイルだから |
---|
064 | #元のファイル名 |
---|
065 | set strOrgFileName to name of aliasPath as text |
---|
066 | #カンマでリストにして |
---|
067 | set strDelim to AppleScript's text item delimiters |
---|
068 | set AppleScript's text item delimiters to "." |
---|
069 | set listFileName to every text item of strOrgFileName |
---|
070 | set AppleScript's text item delimiters to strDelim |
---|
071 | set numCntList to (count of listFileName) as integer |
---|
072 | if numCntList = 1 then |
---|
073 | set boolNoExtension to true as boolean |
---|
074 | #拡張子無しファイル |
---|
075 | set strBaseFileName to (listFileName) as list |
---|
076 | #移動時のファイル名 |
---|
077 | set strSetFileName to (strBaseFileName & "." & strDistExtension) as text |
---|
078 | else |
---|
079 | set boolNoExtension to false as boolean |
---|
080 | #拡張子を取得して |
---|
081 | set strExtension to (last item of listFileName) as text |
---|
082 | #最後の項目を削除 |
---|
083 | set listBaseFileName to (items 1 thru -2 of listFileName) as list |
---|
084 | #残りの項目をカンマで繋いでベースファイル名 |
---|
085 | #ファイル名リストの数を数える |
---|
086 | set numCntList to (count of listBaseFileName) as integer |
---|
087 | #リストの結合 |
---|
088 | repeat with itemNo from 1 to (numCntList) by 1 |
---|
089 | set itemBaseFileName to (item itemNo of listBaseFileName) as text |
---|
090 | if itemNo = 1 then |
---|
091 | set strBaseFileName to (itemBaseFileName) as text |
---|
092 | else |
---|
093 | set strBaseFileName to (strBaseFileName & "." & itemBaseFileName) as text |
---|
094 | end if |
---|
095 | end repeat |
---|
096 | #移動時のファイル名 |
---|
097 | set strSetFileName to (strBaseFileName & "." & strDistExtension) as text |
---|
098 | end if |
---|
099 | #上位の親フォルダ |
---|
100 | set aliasContainerDirPath to (container of itemAliasPath) as alias |
---|
101 | #同名フォルダの有無チェック |
---|
102 | set booFileChk to (exists of (file strSetFileName of folder aliasContainerDirPath)) as boolean |
---|
103 | |
---|
104 | #無ければ |
---|
105 | if booFileChk is false then |
---|
106 | #変更して |
---|
107 | tell itemAliasPath |
---|
108 | set name to strSetFileName |
---|
109 | end tell |
---|
110 | else if booFileChk is true then |
---|
111 | #最大100まで同名フォルダ処理を行う |
---|
112 | #同名ファイルがあった場合のカウンタ |
---|
113 | set numCntNo to 1 as integer |
---|
114 | set boolDoneChange to false as boolean |
---|
115 | repeat while boolDoneChange is false |
---|
116 | #同名がある場合は数字を付与していく |
---|
117 | set strSetFileName to (strBaseFileName & " " & numCntNo & "." & strDistExtension) as text |
---|
118 | log strSetFileName |
---|
119 | #同名フォルダの有無チェック |
---|
120 | set booFileChk to (exists of (file strSetFileName of folder aliasContainerDirPath)) as boolean |
---|
121 | log booFileChk |
---|
122 | #無ければ |
---|
123 | if booFileChk is false then |
---|
124 | #変更して |
---|
125 | tell itemAliasPath |
---|
126 | set name to strSetFileName |
---|
127 | end tell |
---|
128 | #リピートを抜ける |
---|
129 | set boolDoneChange to true as boolean |
---|
130 | else |
---|
131 | #カウントアップ |
---|
132 | set numCntNo to numCntNo + 1 as integer |
---|
133 | end if |
---|
134 | |
---|
135 | end repeat |
---|
136 | end if |
---|
137 | |
---|
138 | |
---|
139 | end if |
---|
140 | end repeat |
---|
141 | end tell |
---|
142 | |
---|
143 | ### |
---|
144 | #置換 |
---|
145 | to doReplace(argOrignalText, argSearchText, argReplaceText) |
---|
146 | set strDelim to AppleScript's text item delimiters |
---|
147 | set AppleScript's text item delimiters to argSearchText |
---|
148 | set listDelim to every text item of argOrignalText |
---|
149 | set AppleScript's text item delimiters to argReplaceText |
---|
150 | set strReturn to listDelim as text |
---|
151 | set AppleScript's text item delimiters to strDelim |
---|
152 | return strReturn |
---|
153 | end doReplace |
---|