Skip to content

Instantly share code, notes, and snippets.

@latenitefilms
Created May 1, 2019 10:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save latenitefilms/d3fff0e6a67e533a5785d2e113133afe to your computer and use it in GitHub Desktop.
Save latenitefilms/d3fff0e6a67e533a5785d2e113133afe to your computer and use it in GitHub Desktop.
A plugin that creates new FCPXML's based off JSON data
--- === plugins.finalcutpro.automate ===
---
--- A plugin that creates new FCPXML's based off JSON data.
local log = require("hs.logger").new("automate")
local json = require("hs.json")
local tools = require("cp.tools")
local fcp = require("cp.apple.finalcutpro")
local dialog = require("cp.dialog")
--------------------------------------------------------------------------------
--
-- THE MODULE:
--
--------------------------------------------------------------------------------
local mod = {}
local function writeToFile(path, data)
file = io.open(path, "w")
file:write(data)
file:close()
end
local function readFromFile(path)
local file = io.open(path, "r")
local output = file:read("*a")
file:close()
return output
end
local function literalize(str)
return str:gsub("[%(%)%.%%%+%-%*%?%[%]%^%$]", function(c) return "%" .. c end)
end
function createTemplates(templatePath, jsonPath, outputPath)
local template = readFromFile(templatePath)
local jsonEncoded = readFromFile(jsonPath)
local jsonDecoded = json.decode(jsonEncoded)
for project, metadata in pairs(jsonDecoded) do
local result = template
-- Update filename:
result = string.gsub(result, [[<project name="Panel Intro Master Template XML"]], [[<project name="]] .. project .. [["]])
-- Update data:
for source, destination in pairs(metadata) do
-- PHOTOPATH:
local filename = project
filename = "file:///Volumes/JellyfishNFS/PHOTOPATH/" .. filename .. ".jpg"
result = string.gsub(result, "PHOTOPATH", filename)
if string.find(destination, "%&") then
destination = string.gsub(destination, "%&", "&amp;")
end
result = string.gsub(result, source, destination)
end
writeToFile(outputPath .. project .. ".fcpxml", result)
end
end
--------------------------------------------------------------------------------
--
-- THE PLUGIN:
--
--------------------------------------------------------------------------------
local plugin = {
id = "finalcutpro.automate",
group = "finalcutpro",
dependencies = {
["core.commands.global"] = "global",
}
}
--------------------------------------------------------------------------------
-- INITIALISE PLUGIN:
--------------------------------------------------------------------------------
function plugin.init(deps)
--------------------------------------------------------------------------------
-- Add a new Global Command:
--------------------------------------------------------------------------------
deps.global
:add("automate")
:whenPressed(function()
local templatePath = dialog.displayChooseFile("Select the FCPXML Template:", "fcpxml")
if not templatePath then
return
end
local jsonPath = dialog.displayChooseFile("Select the JSON File:")
if not jsonPath then
return
end
local outputPath = dialog.displayChooseFolder("Select the output directory:")
if not outputPath then
return
end
createTemplates(templatePath, jsonPath, outputPath)
dialog.displayMessage("Victory!")
end)
:titled("Create new FCPXML from JSON Template")
return mod
end
return plugin
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment