【AppleScript】Finder上でフォルダの日付部分を変更したい

お世話になります。


Finder上で、「23.00.00 本日のメモ」というフォルダがあるとします。

日付部分は半角英数でドットで区切られており、必ず8文字です。その後に半角スペースがあり、続けて何かしらの名前が入っている形です。


最初の8文字を、今日の日付に変更するというスクリプトを作ろうと思い、下記のようなものをやってみました。


tell application "Finder"
	activate
	
	-------日付を取得
	set nowTime to my DateAndTIme(current date)
    (これは、ネットにあった日付取得のスクリプトを参考にして別記してます)
	
	set x to selection as alias
	set y to name of x
	
	set z to characters 1 thru 8 of y as text
	
	set AppleScript's text item delimiters to z
	set Str to text items of y
	
	set AppleScript's text item delimiters to nowTime
	set Str to Str as string
	set name of folder x to Str
	
end tell


これではうまく変更ができませんでした。

どこがおかしいのか分かる人がいらっしゃったら教えていただきたいです。また、文字列の置換ではなくても、何かスクリプトで他のやり方があれば教えてください。

宜しくお願いいたします。


macOS Big Sur

バージョン11.6.7を使用しています。

iMac Pro

投稿日 2023/02/09 22:35

返信
スレッドに付いたマーク ランキングトップの返信

投稿日 2023/02/09 23:24

こんな感じになるのかな?

tell application "Finder"
	activate
	
	-------日付を取得
	set nowTime to my DateAndTIme(current date)
	--(これは、ネットにあった日付取得のスクリプトを参考にして別記してます)
	
	set x to selection as alias
	set y to name of x
	
	set z to characters 1 thru 8 of y as text
	
	set name of folder x to my replaceText(y, z, nowTime)
	
end tell

on replaceText(theText, serchStr, replaceStr)
	set tmp to AppleScript's text item delimiters
	set AppleScript's text item delimiters to serchStr
	set theList to every text item of theText
	set AppleScript's text item delimiters to replaceStr
	set theText to theList as string
	set AppleScript's text item delimiters to tmp
	return theText
end replaceText


追記)

参考:Managements of AppleScript's text item delimiters

返信: 7
スレッドに付いたマーク ランキングトップの返信

2023/02/09 23:24 Pajerow への返信

こんな感じになるのかな?

tell application "Finder"
	activate
	
	-------日付を取得
	set nowTime to my DateAndTIme(current date)
	--(これは、ネットにあった日付取得のスクリプトを参考にして別記してます)
	
	set x to selection as alias
	set y to name of x
	
	set z to characters 1 thru 8 of y as text
	
	set name of folder x to my replaceText(y, z, nowTime)
	
end tell

on replaceText(theText, serchStr, replaceStr)
	set tmp to AppleScript's text item delimiters
	set AppleScript's text item delimiters to serchStr
	set theList to every text item of theText
	set AppleScript's text item delimiters to replaceStr
	set theText to theList as string
	set AppleScript's text item delimiters to tmp
	return theText
end replaceText


追記)

参考:Managements of AppleScript's text item delimiters

2023/02/09 23:07 s55k1n8 への返信

このコードだと最初の1回のみしか正しく動作しないでしょう?


原因は...

>AppleScript's text item delimiters

これの使い方です。。

このままだとnowTimeの値に置き換わったままになってしまって、以後の全てのスクリプトにまで影響してしまいます。


以下のページの「置換」の例にあるように、使用する前に退避、使用後に復元するようにしましょう。

鳶嶋工房 / AppleScript / Tips / 文字列の検索・置換



一度下記を実行してAppleScript's text item delimitersを初期化しておきましょう。

set AppleScript's text item delimiters to null

2023/02/10 01:47 s55k1n8 への返信

s55k1n8 さんによる書き込み:

ドットや半角スペースが原因で問題が起きたことはありませんでした。
ドットは拡張子だと勘違いされちゃうんですかね・・・?


.app や .rtfd など、拡張子として使われる文字列以外は大丈夫ですよ。ダメな場合は Finder が警告してくれます。




あと、スペースについてですが、ネット上に時々出回ってる出来の悪いスクリプトだと問題が起きる場合がありますが、まあ問題ないと考えて良いでしょうね。


2023/02/09 23:32 xy への返信

フォルダ名に半角スラッシュを使用しておかしくなってしまったことはあるのですが、

ドットや半角スペースが原因で問題が起きたことはありませんでした。

ドットは拡張子だと勘違いされちゃうんですかね・・・?ちょっと調べて検討してみます。

ご回答ありがとうございました。

2023/02/09 23:39 Pajerow への返信

まさにその通りで、一番最初の1回しか動作しなくて???状態でした。

自分で書いたものの最初と最後に


set tmp to AppleScript's text item delimiters

と

set AppleScript's text item delimiters to tmp

をつけたら正しく動作しましたが、

このようにまとめることが出来るんですね。


ご回答ありがとうございました。

このスレッドはシステム、またはAppleコミュニティチームによってロックされました。 問題解決の参考になる情報であれば、どの投稿にでも投票いただけます。またコミュニティで他の回答を検索することもできます。

【AppleScript】Finder上でフォルダの日付部分を変更したい

Apple サポートコミュニティへようこそ
Apple ユーザ同士でお使いの製品について助け合うフォーラムです。Apple Account を使ってご参加ください。