local LrApplication = import 'LrApplication'
local LrDialogs = import 'LrDialogs'
local LrTasks = import 'LrTasks'
local LrView = import 'LrView'
local LrLogger = import 'LrLogger'
-- Initialize logger with default Lightroom log location
local logger = LrLogger('ImageRaterLogger')
logger:enable("logfile")
logger:info("Logger initialized.")
LrDialogs.message("Logger initialized.") -- Debug message
-- Function to set a rating
local function setRating(rating)
LrDialogs.message("setRating function called with rating: " .. (rating or "nil")) -- Debug message
local catalog = LrApplication.activeCatalog()
local photo = catalog:getTargetPhoto()
if photo then
LrDialogs.message("Photo selected. Attempting to set rating: " .. (rating or "nil")) -- Debug message
LrFunctionContext.callWithContext("Set Rating Context", function(context)
local success, err = pcall(function()
catalog:withWriteAccessDo("Set Rating", function()
photo:setRawMetadata('rating', rating)
end, {
timeout = 1, -- Wait for up to 1 second for write access
callback = function()
LrDialogs.message("Write access timeout. Could not set rating.") -- Debug message
end
})
end)
if success then
LrDialogs.message("Rating set successfully.") -- Debug message
else
LrDialogs.message("Failed to set rating: " .. (err or "Unknown error")) -- Debug message
end
local message = rating and ("Rated " .. rating .. " stars") or "Rating cleared"
LrDialogs.showBezel(message)
end)
else
LrDialogs.message("No photo selected. Cannot set rating.") -- Debug message
end
end
-- Show the rating dialog
local function showRater()
local catalog = LrApplication.activeCatalog()
if not catalog then
logger:info("No active catalog found.")
LrDialogs.message("No active catalog found.") -- Debug message
return
end
local photo = catalog:getTargetPhoto()
if not photo then
logger:info("No photo selected in catalog.")
LrDialogs.message("No photo selected in catalog.") -- Debug message
LrDialogs.message("Please select a photo", "No photo selected", "info")
return
end
logger:info("Photo selected. Showing rating dialog.")
LrDialogs.message("Photo selected. Showing rating dialog.") -- Debug message
local f = LrView.osFactory()
local contents = f:column {
spacing = f:control_spacing(),
f:row {
f:push_button {
title = "Rate 1",
action = function()
LrDialogs.message("Rate 1 button clicked") -- Debug message
setRating(1)
LrDialogs.stopModalWithResult(contents, "ok")
end,
},
f:push_button {
title = "Rate 2",
action = function()
LrDialogs.message("Rate 2 button clicked") -- Debug message
setRating(2)
LrDialogs.stopModalWithResult(contents, "ok")
end,
},
f:push_button {
title = "Rate 3",
action = function()
LrDialogs.message("Rate 3 button clicked") -- Debug message
setRating(3)
LrDialogs.stopModalWithResult(contents, "ok")
end,
},
},
f:row {
f:push_button {
title = "Rate 4",
action = function()
LrDialogs.message("Rate 4 button clicked") -- Debug message
setRating(4)
LrDialogs.stopModalWithResult(contents, "ok")
end,
},
f:push_button {
title = "Rate 5",
action = function()
LrDialogs.message("Rate 5 button clicked") -- Debug message
setRating(5)
LrDialogs.stopModalWithResult(contents, "ok")
end,
},
f:push_button {
title = "Clear Rating",
action = function()
LrDialogs.message("Clear Rating button clicked") -- Debug message
setRating(nil)
LrDialogs.stopModalWithResult(contents, "ok")
end,
},
},
}
LrDialogs.presentModalDialog({
title = "Rate Image",
contents = contents,
})
end
-- Start the task
LrTasks.startAsyncTask(showRater)