04. ファイルやフォルダのリネームと1階層上移動 ― 2013-04-22
リネームも移動も02.で述べたようなFinder上で選択しているファイルやフォルダが対象です。どちらも一時期何度も手作業に迫られて書きました。
まずリネームですが、クリップボードのテキストを対象の名前とします。
set new_name to the clipboard as text
tell application "Finder"
set select_item to (selection as alias)
if class of select_item = folder then
set name of select_item to new_name
else
set item_ext to name extension of select_item
if item_ext = "" then
set name of select_item to new_name
else
set name of select_item to new_name & "." & item_ext
end if
end if
activate
tell application "System Events"
keystroke return
end tell
end tell
最後のkeystroke return部分は変更後の名前を短くすることが多かったので付け加えたのですが、不要なら外してください。
この名前変更はredoが利きませんのでご注意ください。
これは単数の対象向けですが、複数対象ならpropertyを設定して(同一タスク名+日付や連番)のような名前変更ができますね。
次に1階層上移動ですが、親フォルダ内で子フォルダの▶マークをクリックした時に見えた対象を選択して実行する、そんな用途です。
tell application "Finder"
set alist to selection
repeat with theAlias in alist
set tgtFolder to parent of (parent of theAlias as alias) as alias
move theAlias to tgtFolder
end repeat
end tell
複数対象可能です。マウス操作でもできますが案外難しいので作りました。
こんな小さなスクリプトでも必要なものをちょこちょこ書き貯めておくと、いざという時助かります。
まずリネームですが、クリップボードのテキストを対象の名前とします。
set new_name to the clipboard as text
tell application "Finder"
set select_item to (selection as alias)
if class of select_item = folder then
set name of select_item to new_name
else
set item_ext to name extension of select_item
if item_ext = "" then
set name of select_item to new_name
else
set name of select_item to new_name & "." & item_ext
end if
end if
activate
tell application "System Events"
keystroke return
end tell
end tell
最後のkeystroke return部分は変更後の名前を短くすることが多かったので付け加えたのですが、不要なら外してください。
この名前変更はredoが利きませんのでご注意ください。
これは単数の対象向けですが、複数対象ならpropertyを設定して(同一タスク名+日付や連番)のような名前変更ができますね。
次に1階層上移動ですが、親フォルダ内で子フォルダの▶マークをクリックした時に見えた対象を選択して実行する、そんな用途です。
tell application "Finder"
set alist to selection
repeat with theAlias in alist
set tgtFolder to parent of (parent of theAlias as alias) as alias
move theAlias to tgtFolder
end repeat
end tell
複数対象可能です。マウス操作でもできますが案外難しいので作りました。
こんな小さなスクリプトでも必要なものをちょこちょこ書き貯めておくと、いざという時助かります。
コメント
トラックバック
このエントリのトラックバックURL: http://darzilin.asablo.jp/blog/2013/04/22/6787044/tb
※なお、送られたトラックバックはブログの管理者が確認するまで公開されません。
コメントをどうぞ
※メールアドレスとURLの入力は必須ではありません。 入力されたメールアドレスは記事に反映されず、ブログの管理者のみが参照できます。
※なお、送られたコメントはブログの管理者が確認するまで公開されません。