【photo_manager_eng_v3_copy.ahk】
全構文 ⇒
#Requires AutoHotkey v2.0
; エクスプローラーがアクティブな場合のみ、ショートカットキーを有効化
#HotIf WinActive("ahk_class CabinetWClass")
; 【無変換 + テンキー9】: デスクトップのexeをコピーせず、現在のパスを「引数」として直接起動する
vk1D & Numpad9:: {
currentDir := GetActiveExplorerPath()
if (currentDir == "") {
MsgBox("エクスプローラーのパスを取得できませんでした。", "エラー", "IconX")
return
}
exePath := "C:\Users\kawa\Desktop\photo_manager_eng_v4.exe"
if !FileExist(exePath) {
MsgBox("デスクトップにプログラムが見つかりません:`n" exePath, "エラー", "IconX")
return
}
try {
; ★ exeの後ろにスペースを空けてフォルダパスを引き渡します
Run('"' exePath '" "' currentDir '"')
ToolTip("プログラムを起動しました`n対象: " currentDir)
SetTimer(() => ToolTip(), -2000)
} catch as err {
MsgBox("プログラムの起動に失敗しました。`nエラー: " err.Message, "エラー", "IconX")
}
}
; 【無変換 + テンキー5】: selectedフォルダ内の指定画像を、Zドライブに新規作成したフォルダへコピー & 自動リネーム & selectedフォルダ削除
vk1D & Numpad5:: {
; 1. 現在アクティブなエクスプローラーのフォルダパスを取得
currentDir := GetActiveExplorerPath()
if (currentDir == "") {
MsgBox("エクスプローラーのパスを取得できませんでした。", "エラー", "IconX")
return
}
; 2. 現在のフォルダ内に「selected」フォルダが存在するか確認
selectedDir := currentDir "\selected"
if !DirExist(selectedDir) {
MsgBox("「selected」フォルダーが現在の場所に存在しません。`n処理を中断します。", "エラー", "IconX")
return
}
; 3. Zドライブ(保存先の基本パス)が存在するか確認
baseDestDir := "Z:\Saishin\sokorireki\photo\episode\rebel"
if !DirExist(baseDestDir) {
MsgBox("保存先ベースフォルダーが見つかりません。`nネットワーク接続(Zドライブ)を確認してください。", "エラー", "IconX")
return
}
; 4. 小窓を開いてフォルダ名の入力を促す
ib := InputBox("作成するフォルダー名を入力してください。", "新規フォルダーの作成とコピー", "w350 h130")
if (ib.Result == "Cancel" || ib.Value == "") {
return ; キャンセルまたは空欄の場合は処理を終了
}
newFolderName := ib.Value
; 5. 入力されたフォルダ名で、Zドライブ内にフォルダを新規作成
destDir := baseDestDir "\" newFolderName
if !DirExist(destDir) {
try {
DirCreate(destDir)
} catch as err {
MsgBox("フォルダーの作成に失敗しました:`n" destDir "`nエラー: " err.Message, "エラー", "IconX")
return
}
}
; 6. 「selected」内の「jpg」「heic」「png」ファイルをコピー
extensions := ["jpg", "heic", "png"]
copiedCount := 0
try {
for ext in extensions {
Loop Files, selectedDir "\*." ext {
FileCopy(A_LoopFilePath, destDir "\", 1) ; 1 は同名ファイルを上書き
copiedCount
}
}
; 7. コピー完了後、PowerShellを呼び出して自動リネームを実行
if (copiedCount > 0) {
; コピー中であることを一旦通知
ToolTip("コピー中... リネーム処理を準備しています。")
; 動的にリネーム用のPowerShellスクリプトを生成
RunRenamePowerShell(destDir, newFolderName)
; ★ 【後片付け】コピー元フォルダ内の「selected」フォルダを中身ごと強制削除する ★
if DirExist(selectedDir) {
try {
DirDelete(selectedDir, 1) ; 1を指定することで、フォルダ内が空でなくても丸ごと削除します
}
}
; 完了通知
ToolTip("コピー & リネーム完了!`n(元フォルダのselectedは自動削除しました)`n保存先: " destDir)
SetTimer(() => ToolTip(), -4000) ; 4秒後に通知を消去
} else {
MsgBox("「selected」フォルダー内に、コピー対象の画像(jpg, heic, png)が見つかりませんでした。", "お知らせ", "Iconi")
}
} catch as err {
MsgBox("コピー・リネーム処理中にエラーが発生しました。`nエラー: " err.Message, "エラー", "IconX")
}
}
#HotIf
; --- 以下、処理を行う関数群 ---
; コピーしたファイルを撮影日時(または更新日時)順に並び替え、入力フォルダ名で連番リネームする関数
RunRenamePowerShell(folderPath, newName) {
psTemplate := "
(
$folder = '[[FOLDER_PATH]]'
$list = @()
Get-ChildItem -LiteralPath
$folder -File | ForEach-Object {
$f = $_.FullName
$date =
$null
$out = & 'C:\Tools\exiftool.exe' -DateTimeOriginal -s3
$f 2>$null
if ($out) {
try {
$date = [DateTime]::ParseExact($out.Trim(), 'yyyy:MM:dd HH:mm:ss',
$null) } catch {}
}
if (-not
$date) {
$date = $_.LastWriteTime }
$list = ,[PSCustomObject]@{ Path =
$f; Date =
$date; Ext = $_.Extension }
}
$tempList = @()
foreach ($item in
$list) {
$tempName = [
System.IO.Path]::GetRandomFileName()
$item.Ext
$tempPath = Join-Path
$folder $tempName
Rename-Item -LiteralPath
$item.Path -NewName
$tempName -Force
$tempList = [PSCustomObject]@{ TempPath =
$tempPath; Date =
$item.Date; Ext =
$item.Ext }
}
$i = 1
$tempList | Sort-Object Date | ForEach-Object {
Rename-Item -LiteralPath $_.TempPath -NewName ('[[NEW_NAME]]_'
$i $_.Ext) -Force
$i
}
)"
psCode := StrReplace(psTemplate, "[[FOLDER_PATH]]", folderPath)
psCode := StrReplace(psCode, "[[NEW_NAME]]", newName)
tempPsFile := A_Temp "\ahk_rename_temp.ps1"
if FileExist(tempPsFile) {
FileDelete(tempPsFile)
}
FileAppend(psCode, tempPsFile, "UTF-8-RAW")
RunWait("powershell -NoProfile -ExecutionPolicy Bypass -File `"" tempPsFile "`"", , "Hide")
if FileExist(tempPsFile) {
FileDelete(tempPsFile)
}
}
; アクティブなエクスプローラーのパスをCOMオブジェクト経由で取得する関数
GetActiveExplorerPath() {
activeHwnd := WinExist("A")
try {
for window in ComObject("Shell.Application").Windows {
if (window.hwnd == activeHwnd) {
return window.Document.Folder.Self.Path
}
}
}
return ""
}