Created
March 26, 2016 03:25
-
-
Save Adidea/5885bcff5b1d8485c09e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--[[ Debug ]] | |
local Require = require 'Require'.path ("../debugscript.lrdevplugin") | |
local Debug = require 'Debug'.init () | |
require 'strict' | |
local json = require("json") | |
local FileUtil = import("LrFileUtils") --for readFile | |
local Dialogs = import("LrDialogs") --for runOpenPanel to select tags file | |
local Application = import("LrApplication") --for activeCatalog | |
local Tasks = import("LrTasks") | |
local Catalog = Application.activeCatalog() | |
function tableLength(tab) | |
local i = 0 | |
for _ in pairs(tab) do | |
i = i + 1 | |
end | |
return i | |
end | |
function writeTagsToCatalog(data) | |
local progress = LrDialogs.showModelProgressDialog({ | |
title = "" | |
}) | |
local photos = Catalog:getMultipleSelectedOrAllPhotos() | |
local nPhotos = #photos | |
local importKeyword = nil --A container for our imported keywords so as not to make mess. | |
local photoMatches = {} --array - {LrPhoto, {LrKeywords}} | |
Catalog:withWriteAccessDo("createImport", function() | |
importKeyword = Catalog:createKeyword("Imported", {}, true, nil, true) | |
end) | |
--create keywords | |
Catalog:withWriteAccessDo("createKeywords", function() | |
for i, photo in pairs(photos) do | |
local name = photo:getFormattedMetadata("fileName") | |
if data[name] then | |
-- create keywords | |
local photoKeyPair = {photo = photo, keywords = {}} | |
for _, tag in pairs(data[name].tags) do | |
table.insert(photoKeyPair.keywords, tag) | |
end | |
table.insert(photoMatches, photoKeyPair) | |
--write some other information to the photo | |
--data[name].artist | |
--data[name].category (parent to a category keyword) | |
--data[name].submission (rename to source, should be custom field) | |
--data[name].title (also custom field) | |
--if custom fields don't work keyword groups should work well enough. | |
end | |
Tasks.yield(); | |
end) | |
Catalog:withWriteAccessDo("writeKeywords", function() | |
for i,v in pairs(photoMatches) do | |
for _, tag in pairs(v.keywords) do | |
v.photo:addKeyword(tag) | |
end | |
end | |
end) | |
end | |
end | |
function activate() | |
local tagData = {} | |
local tagFilePath = Dialogs.runOpenPanel({ --file prompt | |
title = "Select a tag file", | |
canChooseFiles = true, | |
canChooseDirectories = true, | |
allowMultipleSelection = false, | |
fileTypes = "json", | |
}) | |
if tagFilePath then --A file was selected now decode it | |
Debug.logn("decoding json file") | |
local tagFile = FileUtil.readFile(tagFilePath[1]) | |
tagData = json.decode(tagFile) | |
Tasks.startAsyncTask(function() | |
writeTagsToCatalog(tagData, progress) | |
end) | |
end | |
end | |
activate() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment