【AppleScript】Finder上で複数個選択し、ファイルをそれぞれ同じ階層に文字列を追加して複製するスクリプト

お世話になります。


Finder上でフォルダが複数あり、その中にファイルがそれぞれ入っているとします。

ファイルを選択し、それぞれのファイルと同じ階層に文字列を追加し複製したいです。


tell application "Finder"
	activate
	
	display dialog "追加する文字を入力" default answer "-001" -- 追加する文字を指定
	set NewName to text returned of the result as Unicode text -- 変更後のファイル名をNewNameに格納
	
	set sel to get selection --選択ファイル
	set ObjInfo to info for sel as alias --選択ファイルの情報を取得
	set FileName to name of ObjInfo as Unicode text --選択ファイルのファイル名をFileNameに格納
	set FileNameChrList to every character of FileName --ファイル名の文字を分割
	set tmpFileName to reverse of FileNameChrList as Unicode text -- ファイル名テキストの順番を逆にする
	set c to offset of "." in tmpFileName -- ファイル名の"."がテキストの先頭から何番目にあるか
	set preFix to reverse of (characters 1 thru c of tmpFileName) -- "."から最後までの文字を取り出す("."含めた拡張子)
	set retFileName to reverse of (characters (c + 1) thru (length of tmpFileName) of tmpFileName) -- "."から最初までの文字を取り出す("."の前のファイル名)
	
	set duplicatedFile to duplicate sel --選択ファイルを複製
	set name of duplicatedFile to (retFileName as Unicode text) & (NewName as Unicode text) & (preFix as Unicode text) -- 複製したファイルを変更
end tell


一つのファイルだけなら上記のスクリプトで一応出来ました。

これを複数選択して、それぞれのファイルと同階層にやりたいです。

どうやったら出来るのか教えていただきたいです。


macOS Catalina バージョン10.15.7の環境になります。

iMac Pro

投稿日 2022/07/11 19:51

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

投稿日 2022/07/11 20:10

下記の下線部分を変更、追加すればいけると思います。

変更:1箇所 ①

追加:2行 ②③


tell application "Finder"

activate


display dialog "追加する文字を入力" default answer "-001" -- 追加する文字を指定

set NewName to text returned of the result as Unicode text -- 変更後のファイル名をNewNameに格納


setAllsel to get selection --選択ファイル

repeat with sel in Allsel

set ObjInfo to info for sel as alias --選択ファイルの情報を取得

set FileName to name of ObjInfo as Unicode text --選択ファイルのファイル名をFileNameに格納

set FileNameChrList to every character of FileName --ファイル名の文字を分割

set tmpFileName to reverse of FileNameChrList as Unicode text -- ファイル名テキストの順番を逆にする

set c to offset of "." in tmpFileName -- ファイル名の"."がテキストの先頭から何番目にあるか

set preFix to reverse of (characters 1 thru c of tmpFileName) -- "."から最後までの文字を取り出す("."含めた拡張子)

set retFileName to reverse of (characters (c + 1) thru (length of tmpFileName) of tmpFileName) -- "."から最初までの文字を取り出す("."の前のファイル名)


set duplicatedFile to duplicate sel --選択ファイルを複製

set name of duplicatedFile to (retFileName as Unicode text) & (NewName as Unicode text) & (preFix as Unicode text) -- 複製したファイルを変更 

end repeat

end tell



類似の質問

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

2022/07/11 20:10 s55k1n8 への返信

下記の下線部分を変更、追加すればいけると思います。

変更:1箇所 ①

追加:2行 ②③


tell application "Finder"

activate


display dialog "追加する文字を入力" default answer "-001" -- 追加する文字を指定

set NewName to text returned of the result as Unicode text -- 変更後のファイル名をNewNameに格納


setAllsel to get selection --選択ファイル

repeat with sel in Allsel

set ObjInfo to info for sel as alias --選択ファイルの情報を取得

set FileName to name of ObjInfo as Unicode text --選択ファイルのファイル名をFileNameに格納

set FileNameChrList to every character of FileName --ファイル名の文字を分割

set tmpFileName to reverse of FileNameChrList as Unicode text -- ファイル名テキストの順番を逆にする

set c to offset of "." in tmpFileName -- ファイル名の"."がテキストの先頭から何番目にあるか

set preFix to reverse of (characters 1 thru c of tmpFileName) -- "."から最後までの文字を取り出す("."含めた拡張子)

set retFileName to reverse of (characters (c + 1) thru (length of tmpFileName) of tmpFileName) -- "."から最初までの文字を取り出す("."の前のファイル名)


set duplicatedFile to duplicate sel --選択ファイルを複製

set name of duplicatedFile to (retFileName as Unicode text) & (NewName as Unicode text) & (preFix as Unicode text) -- 複製したファイルを変更 

end repeat

end tell



2022/07/11 21:11 s55k1n8 への返信

メインのロジックももう少し簡単にできます。

tell application "Finder"
	activate
	
	display dialog "追加する文字を入力" default answer "-001" -- 追加する文字を指定
	set AddName to text returned of the result as text
	
	set AllSel to selection
	repeat with Sel in AllSel
		set SelExt to Sel's name extension --拡張子
		set SelName to (Sel's name as text)'s items 1 thru -(((Sel's name extension as text)'s length) + 2) as text --ファイル名
		set duplicatedFile to duplicate Sel
		set name of duplicatedFile to (SelName & AddName & "." & SelExt) -- 複製したファイルを変更 
	end repeat
	
end tell


シェルスクリプトを使えばさらに簡単になるのですが、それは下記などでしらべてください。

ファイルパスからファイル名や拡張子を自由に取り出す - ザリガニが見ていた...。

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

【AppleScript】Finder上で複数個選択し、ファイルをそれぞれ同じ階層に文字列を追加して複製するスクリプト

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