--@action Name pictures by what they show --@version 1.0.0 --@author TheFile.Ninja --@id ai-picture-names --@description Asks your AI provider (Preferences › AI) for a short name for each marked picture, shows every new name, and renames them when you agree. --@tags ai, rename, pictures --@requires ai, file_write -- For screenshots and phone photos called IMG_2041.jpg. The pictures go to -- the provider set in Preferences › AI, shrunk, eight at a time; nothing is -- renamed before you have seen the list. Ctrl+Z undoes each rename. local LANGUAGE = "English" -- the language of the new names local PICTURES = { jpg = true, jpeg = true, png = true, webp = true, gif = true, bmp = true, heic = true } local SYSTEM = "You name picture files. For each picture, in the order given, write a short file name that says what " .. "it shows: 2 to 6 words in " .. LANGUAGE .. ", lower case, joined by hyphens, no extension, no dates. " .. 'Answer with JSON only: {"names": ["first-name", "second-name"]}.' if not ai.available() then ui.message("No AI provider is set up: Preferences › AI.", "Name pictures") return end if not ai.supports_images() then ui.message("The model chosen in Preferences › AI takes no pictures. Choose one that does.", "Name pictures") return end local pictures = {} for _, f in ipairs(pane.active():effective_selection()) do if f.is_file and PICTURES[f.extension:lower()] then pictures[#pictures + 1] = f end end if #pictures == 0 then ui.message("Mark the pictures first, or put the cursor on one.", "Name pictures") return end -- Words joined by hyphens, without punctuation, at most 60 characters. -- (Lua's patterns know ASCII only: letters of other languages are kept as they are.) local function clean(name) name = tostring(name or ""):lower():gsub("%.%a+$", "") -- an extension the answer added name = name:gsub("[%s_%-]+", " "):gsub("%p", ""):gsub("^%s+", ""):gsub("%s+$", ""):gsub("%s+", "-") local cut = utf8.offset(name, 61) if cut then name = name:sub(1, cut - 1):gsub("%-$", "") end return name end local prog = ui.progress("Naming pictures") local proposals, taken = {}, {} for first = 1, #pictures, 8 do if prog:is_cancelled() then return end prog:update(math.floor((first - 1) * 100 / #pictures)) local batch = { table.unpack(pictures, first, math.min(first + 7, #pictures)) } local ok, answer = pcall(ai.extract, "Name these " .. #batch .. " pictures.", { system = SYSTEM, images = batch, temperature = 0.2 }) if not ok then prog:finish() ui.message(tostring(answer), "Name pictures", "error") return end for i, f in ipairs(batch) do local base = clean(answer and answer.names and answer.names[i]) if base ~= "" then -- Never two the same, and never a name the folder has already. local name, n = base .. f.ext:lower(), 1 while taken[path.join(path.parent(f.full_path), name):lower()] or (name:lower() ~= f.name:lower() and fs.exists(path.join(path.parent(f.full_path), name))) do n = n + 1 name = base .. "-" .. n .. f.ext:lower() end taken[path.join(path.parent(f.full_path), name):lower()] = true if name ~= f.name then proposals[#proposals + 1] = { item = f, name = name } end end end end prog:finish() if #proposals == 0 then ui.message("Nothing to rename.", "Name pictures") return end local lines = {} for i, pr in ipairs(proposals) do lines[i] = string.format("%-28s %s", pr.item.name, pr.name) end if ui.confirm("Rename " .. #proposals .. " picture(s)?\n\n" .. table.concat(lines, "\n"), "Name pictures") then for _, pr in ipairs(proposals) do file_ops.rename(pr.item, pr.name) end ui.status(#proposals .. " picture(s) renamed; Ctrl+Z undoes each") end