【AppleScript】Finder上の選択ファイルを複製後、複製したものを選択状態にするには

Finder上で選択している複数のファイルを、特定の文字列を追加して複製するというスクリプトを実行します。


tell application "Finder"
	activate
	
	set theString to "-a" --追加する文字
	
	set Allsel to selection --選択ファイル
	
	repeat with sel in Allsel
		set FileName to name of sel as string --選択ファイル名
		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) & (theString as Unicode text) & (preFix as Unicode text) -- 複製したファイル名を変更
	end repeat
end tell


実行後、複製する前に選択していたファイルが選択状態になっているのですが、複製した方のファイルを選択状態にしたかったので、選択ファイル複製のコードの後に



		select duplicatedFile


上記コードを追加してやってみたのですが、複数複製した時に、複製した方の一つのファイルしか選択状態になりませんでした。

selectでは出来ないのでしょうか。

分かる方がいらっしゃったら教えてください。

iMac

投稿日 2022/11/13 21:10

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

投稿日 2022/11/14 21:47

リネーム後のパス名のリストを作成し、それを select すれば良いと思います。


例えばこんな感じ。


tell application "Finder"
    activate

    set theString to "-a" --追加する文字

    set Allsel to selection as alias list --選択ファイル
    set ff to {} -- リネーム後のファイルのパスのリスト

    repeat with sel in Allsel
        set FileName to name of sel as text --選択ファイル名
        set FileNameChrList to every character of FileName --選択ファイル名の文字列
        set tmpFileName to reverse of FileNameChrList as 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 DirName to sel's container as text
        set NewFileName to (retFileName as text) & (theString as text) & (preFix as text)

        set duplicatedFile to duplicate sel --選択ファイルを複製
        set name of duplicatedFile to NewFileName -- 複製したファイル名を変更

        set end of ff to (DirName & NewFileName) as alias
    end repeat

    select ff
end tell


# as string、as Unicode text、selection は次のように勝手に変更しました。

as string => as text

as Unicode text => as text

selection => selection as alias list


動作確認: macOS 10.14.6


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

2022/11/14 21:47 s55k1n8 への返信

リネーム後のパス名のリストを作成し、それを select すれば良いと思います。


例えばこんな感じ。


tell application "Finder"
    activate

    set theString to "-a" --追加する文字

    set Allsel to selection as alias list --選択ファイル
    set ff to {} -- リネーム後のファイルのパスのリスト

    repeat with sel in Allsel
        set FileName to name of sel as text --選択ファイル名
        set FileNameChrList to every character of FileName --選択ファイル名の文字列
        set tmpFileName to reverse of FileNameChrList as 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 DirName to sel's container as text
        set NewFileName to (retFileName as text) & (theString as text) & (preFix as text)

        set duplicatedFile to duplicate sel --選択ファイルを複製
        set name of duplicatedFile to NewFileName -- 複製したファイル名を変更

        set end of ff to (DirName & NewFileName) as alias
    end repeat

    select ff
end tell


# as string、as Unicode text、selection は次のように勝手に変更しました。

as string => as text

as Unicode text => as text

selection => selection as alias list


動作確認: macOS 10.14.6


2022/11/14 19:20 s55k1n8 への返信

s55k1n8 さん、こんにちは。


すでにご確認済みかもしれませんが、関連する情報として以下の記事をご参照ください。


今後とも Apple サポートコミュニティをよろしくお願いします。


※この投稿では、引き続きユーザの皆様からの情報を募集しています。

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

【AppleScript】Finder上の選択ファイルを複製後、複製したものを選択状態にするには

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