【Mac】ExcelVBAでダウンロードフォルダの中のファイルから一番新しいファイルのパスを取得する方法。に引き続き誰の役に立つのかわからないシリーズ。
自分用メモとして記載しておきます。
AppleScript
tell application "Finder"
set myfiles to a reference to (sort (get files of (path to downloads folder)) by creation date)
set myfile to end of myfiles
set ext to name extension of myfile
set newNameNoExt to "変更後ファイル名_拡張子なし" as Unicode text
set newName to newNameNoExt & "." & ext as Unicode text
set newPathFileNAme to (path to downloads folder) & newName as Unicode text
set cnt to 1
repeat with tmpFile in myfiles
if newPathFileNAme = (tmpFile as Unicode text) then
set newName to newNameNoExt & " _" & cnt & "." & ext as Unicode text
set newPathFileNAme to (path to downloads folder) & newName as Unicode text
set cnt to cnt + 1
end if
end repeat
set name of myfile to newName
return newPathFileNAme
end tell
説明付き
説明付きで記載します。実際には説明部分は不要なので、上記のコードをコピーしてください。
-- Finderで処理します。
tell application "Finder"
-- これでダウンロードフォルダの中のファイルを取得して更新日付の古いやつを頭にしてmyfilesにいれます。
set myfiles to a reference to (sort (get files of (path to downloads folder)) by creation date)
-- myfilesの中から一番最後(一番新しい)ファイルを抜き出します。
set myfile to end of myfiles
-- myfileの拡張子を抜き出します。
set ext to name extension of myfile
-- 変更後のファイル名を拡張子なしで指定します。
set newNameNoExt to "変更後ファイル名_拡張子なし" as Unicode text
-- 変更後のファイル名を拡張子つきで指定します。
set newName to newNameNoExt & "." & ext as Unicode text
-- ファイルパス付きで新しいファイルを指定します。
set newPathFileNAme to (path to downloads folder) & newName as Unicode text
-- すでに新しいファイル名と同じファイルがあった時ようにカウントアップします。
set cnt to 1
-- ダウンロードフォルダになるファイル全て処理します。
repeat with tmpFile in myfiles
-- 新しいファイル名と同一のファイル名がある場合には、ファイル名の最後にカウントをつけます。
if newPathFileNAme = (tmpFile as Unicode text) then
-- カウントをつけたファイル名を定義します。
set newName to newNameNoExt & " _" & cnt & "." & ext as Unicode text
-- カウントをつけたファイルのパスつきで定義します。
set newPathFileNAme to (path to downloads folder) & newName as Unicode text
set cnt to cnt + 1
end if
end repeat
-- ファイル名を書き換えます。
set name of myfile to newName
-- 新しいファイル名をパスつきで返します。
return newPathFileNAme
end tell
VBA用
VBAからMacScript呼び出すときには、ダブルコーテーションの付け方が面倒なので、念のため。新しいファイル名には、処理中で指定する、FnameとMyDateで指定していますが、ご自身のコードにあわせて変えてください。
' ダウンロードしたファイルのパスを取得する。
' ファイル名も同時に変更してしまう。
s = "tell application ""Finder""" & vbCrLf & _
"set myfiles to a reference to (sort (get files of (path to downloads folder)) by creation date)" & vbCrLf & _
"set myfile to end of myfiles" & vbCrLf & _
"set ext to name extension of myfile" & vbCrLf & _
"set newNameNoExt to """ & Fname & "_" & MyDate & """ as Unicode text" & vbCrLf & _
"set newName to newNameNoExt & " & """.""" & " & ext as Unicode text" & vbCrLf & _
"set newPathFileNAme to (path to downloads folder) & newName as Unicode text" & vbCrLf & _
"set cnt to 1" & vbCrLf & _
"repeat with tmpFile in myfiles" & vbCrLf & _
"if newPathFileNAme = (tmpFile as Unicode text) then" & vbCrLf & _
"set newName to newNameNoExt &" & """ _""" & " & cnt & " & """.""" & " & ext as Unicode text" & vbCrLf & _
"set newPathFileNAme to (path to downloads folder) & newName as Unicode text" & vbCrLf & _
"set cnt to cnt + 1" & vbCrLf & _
"end if" & vbCrLf & _
"end repeat" & vbCrLf & _
"set name of myfile to newName" & vbCrLf & _
"return newPathFileNAme" & vbCrLf & _
"end tell"
'Debug.Print s
str = MacScript(s)
最後に
わかってしまえば、まぁそれでいけるよな、と思うのですが、なかなかAppleScriptのお作法になれることが出来ずに簡単なことができないです。たぶん、もう少し簡単なコードで同じこと実現できる気がするのですが。。。
以上です。


