Created
January 23, 2018 08:21
-
-
Save latenitefilms/816c2cd286f3e4f146aa19003b7d0498 to your computer and use it in GitHub Desktop.
Keywords Plugin Temp Fix
This file contains hidden or 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
-------------------------------------------------------------------------------- | |
-------------------------------------------------------------------------------- | |
-- K E Y W O R D S P L U G I N -- | |
-------------------------------------------------------------------------------- | |
-------------------------------------------------------------------------------- | |
--- === plugins.finalcutpro.browser.keywords === | |
--- | |
--- Browser Keywords | |
-------------------------------------------------------------------------------- | |
-- | |
-- EXTENSIONS: | |
-- | |
-------------------------------------------------------------------------------- | |
local log = require("hs.logger").new("addnote") | |
local ax = require("hs._asm.axuielement") | |
local eventtap = require("hs.eventtap") | |
local dialog = require("cp.dialog") | |
local fcp = require("cp.apple.finalcutpro") | |
local config = require("cp.config") | |
local tools = require("cp.tools") | |
-------------------------------------------------------------------------------- | |
-- | |
-- THE MODULE: | |
-- | |
-------------------------------------------------------------------------------- | |
local mod = {} | |
function mod.save(whichButton) | |
-------------------------------------------------------------------------------- | |
-- Check to see if the Keyword Editor is already open: | |
-------------------------------------------------------------------------------- | |
local fcpx = fcp:application() | |
local fcpxElements = ax.applicationElement(fcpx) | |
local whichWindow = nil | |
for i=1, fcpxElements:attributeValueCount("AXChildren") do | |
if fcpxElements[i]:attributeValue("AXRole") == "AXWindow" then | |
if fcpxElements[i]:attributeValue("AXIdentifier") == "_NS:247" then --"_NS:264" then | |
whichWindow = i | |
end | |
end | |
end | |
if whichWindow == nil then | |
dialog.displayMessage(i18n("keywordEditorAlreadyOpen")) | |
return | |
end | |
fcpxElements = fcpxElements[whichWindow] | |
-------------------------------------------------------------------------------- | |
-- Get Starting Textfield: | |
-------------------------------------------------------------------------------- | |
local startTextField = nil | |
for i=1, fcpxElements:attributeValueCount("AXChildren") do | |
if startTextField == nil then | |
if fcpxElements[i]:attributeValue("AXIdentifier") == "_NS:176" then --"_NS:102" then | |
startTextField = i | |
goto startTextFieldDone | |
end | |
end | |
end | |
::startTextFieldDone:: | |
if startTextField == nil then | |
-------------------------------------------------------------------------------- | |
-- Keyword Shortcuts Buttons isn't down: | |
-------------------------------------------------------------------------------- | |
fcpxElements = ax.applicationElement(fcpx)[1] -- Refresh | |
for i=1, fcpxElements:attributeValueCount("AXChildren") do | |
if fcpxElements[i]:attributeValue("AXIdentifier") == "_NS:264" then --"_NS:276" then | |
keywordDisclosureTriangle = i | |
goto keywordDisclosureTriangleDone | |
end | |
end | |
::keywordDisclosureTriangleDone:: | |
if fcpxElements[keywordDisclosureTriangle] == nil then | |
dialog.displayMessage(i18n("keywordShortcutsVisibleError")) | |
return "Failed" | |
else | |
local keywordDisclosureTriangleResult = fcpxElements[keywordDisclosureTriangle]:performAction("AXPress") | |
if keywordDisclosureTriangleResult == nil then | |
dialog.displayMessage(i18n("keywordShortcutsVisibleError")) | |
return "Failed" | |
end | |
end | |
end | |
-------------------------------------------------------------------------------- | |
-- Get Values from the Keyword Editor: | |
-------------------------------------------------------------------------------- | |
local savedKeywordValues = {} | |
local favoriteCount = 1 | |
local skipFirst = true | |
for i=1, fcpxElements:attributeValueCount("AXChildren") do | |
if fcpxElements[i]:attributeValue("AXRole") == "AXTextField" then | |
if skipFirst then | |
skipFirst = false | |
else | |
savedKeywordValues[favoriteCount] = fcpxElements[i]:attributeValue("AXHelp") | |
favoriteCount = favoriteCount + 1 | |
end | |
end | |
end | |
-------------------------------------------------------------------------------- | |
-- Save Values to Settings: | |
-------------------------------------------------------------------------------- | |
local savedKeywords = config.get("savedKeywords") | |
if savedKeywords == nil then savedKeywords = {} end | |
for i=1, 9 do | |
if savedKeywords['Preset ' .. tostring(whichButton)] == nil then | |
savedKeywords['Preset ' .. tostring(whichButton)] = {} | |
end | |
savedKeywords['Preset ' .. tostring(whichButton)]['Item ' .. tostring(i)] = savedKeywordValues[i] | |
end | |
config.set("savedKeywords", savedKeywords) | |
-------------------------------------------------------------------------------- | |
-- Saved: | |
-------------------------------------------------------------------------------- | |
dialog.displayNotification(i18n("keywordPresetsSaved") .. " " .. tostring(whichButton)) | |
end | |
function mod.restore(whichButton) | |
-------------------------------------------------------------------------------- | |
-- Get Values from Settings: | |
-------------------------------------------------------------------------------- | |
local savedKeywords = config.get("savedKeywords") | |
local restoredKeywordValues = {} | |
if savedKeywords == nil then | |
dialog.displayMessage(i18n("noKeywordPresetsError")) | |
return "Fail" | |
end | |
if savedKeywords['Preset ' .. tostring(whichButton)] == nil then | |
dialog.displayMessage(i18n("noKeywordPresetError")) | |
return "Fail" | |
end | |
for i=1, 9 do | |
restoredKeywordValues[i] = savedKeywords['Preset ' .. tostring(whichButton)]['Item ' .. tostring(i)] | |
end | |
-------------------------------------------------------------------------------- | |
-- Check to see if the Keyword Editor is already open: | |
-------------------------------------------------------------------------------- | |
local fcpx = fcp:application() | |
local fcpxElements = ax.applicationElement(fcpx) | |
local whichWindow = nil | |
for i=1, fcpxElements:attributeValueCount("AXChildren") do | |
if fcpxElements[i]:attributeValue("AXRole") == "AXWindow" then | |
if fcpxElements[i]:attributeValue("AXIdentifier") == "_NS:247" then --"_NS:264" then | |
whichWindow = i | |
end | |
end | |
end | |
if whichWindow == nil then | |
dialog.displayMessage(i18n("keywordEditorAlreadyOpen")) | |
return | |
end | |
fcpxElements = fcpxElements[whichWindow] | |
-------------------------------------------------------------------------------- | |
-- Get Starting Textfield: | |
-------------------------------------------------------------------------------- | |
local startTextField = nil | |
for i=1, fcpxElements:attributeValueCount("AXChildren") do | |
if startTextField == nil then | |
if fcpxElements[i]:attributeValue("AXIdentifier") == "_NS:176" then --"_NS:102" then | |
startTextField = i | |
goto startTextFieldDone | |
end | |
end | |
end | |
::startTextFieldDone:: | |
if startTextField == nil then | |
-------------------------------------------------------------------------------- | |
-- Keyword Shortcuts Buttons isn't down: | |
-------------------------------------------------------------------------------- | |
local keywordDisclosureTriangle = nil | |
for i=1, fcpxElements:attributeValueCount("AXChildren") do | |
if fcpxElements[i]:attributeValue("AXIdentifier") == "_NS:264" then --"_NS:276" then | |
keywordDisclosureTriangle = i | |
goto keywordDisclosureTriangleDone | |
end | |
end | |
::keywordDisclosureTriangleDone:: | |
if fcpxElements[keywordDisclosureTriangle] ~= nil then | |
local keywordDisclosureTriangleResult = fcpxElements[keywordDisclosureTriangle]:performAction("AXPress") | |
if keywordDisclosureTriangleResult == nil then | |
dialog.displayMessage(i18n("keywordShortcutsVisibleError")) | |
return "Failed" | |
end | |
else | |
dialog.displayErrorMessage("Could not find keyword disclosure triangle.\n\nError occurred in restoreKeywordSearches().") | |
return "Failed" | |
end | |
end | |
-------------------------------------------------------------------------------- | |
-- Restore Values to Keyword Editor: | |
-------------------------------------------------------------------------------- | |
local favoriteCount = 1 | |
local skipFirst = true | |
for i=1, fcpxElements:attributeValueCount("AXChildren") do | |
if fcpxElements[i]:attributeValue("AXRole") == "AXTextField" then | |
if skipFirst then | |
skipFirst = false | |
else | |
currentKeywordSelection = fcpxElements[i] | |
setKeywordResult = currentKeywordSelection:setAttributeValue("AXValue", restoredKeywordValues[favoriteCount]) | |
keywordActionResult = currentKeywordSelection:setAttributeValue("AXFocused", true) | |
eventtap.keyStroke({""}, "return") | |
-- For some reason you have to do this twice. Probably a timing issue? | |
setKeywordResult = currentKeywordSelection:setAttributeValue("AXValue", restoredKeywordValues[favoriteCount]) | |
keywordActionResult = currentKeywordSelection:setAttributeValue("AXFocused", true) | |
eventtap.keyStroke({""}, "return") | |
favoriteCount = favoriteCount + 1 | |
end | |
end | |
end | |
-------------------------------------------------------------------------------- | |
-- Successfully Restored: | |
-------------------------------------------------------------------------------- | |
dialog.displayNotification(i18n("keywordPresetsRestored") .. " " .. tostring(whichButton)) | |
end | |
-------------------------------------------------------------------------------- | |
-- | |
-- THE PLUGIN: | |
-- | |
-------------------------------------------------------------------------------- | |
local plugin = { | |
id = "finalcutpro.browser.keywords", | |
group = "finalcutpro", | |
dependencies = { | |
["finalcutpro.commands"] = "fcpxCmds", | |
} | |
} | |
-------------------------------------------------------------------------------- | |
-- INITIALISE PLUGIN: | |
-------------------------------------------------------------------------------- | |
function plugin.init(deps) | |
for i=1, 9 do | |
deps.fcpxCmds:add("cpRestoreKeywordPreset" .. tools.numberToWord(i)) | |
:activatedBy():ctrl():option():cmd(tostring(i)) | |
:titled(i18n("cpRestoreKeywordPreset_customTitle", {count = i})) | |
:whenActivated(function() mod.restore(i) end) | |
deps.fcpxCmds:add("cpSaveKeywordPreset" .. tools.numberToWord(i)) | |
:activatedBy():ctrl():option():shift():cmd(tostring(i)) | |
:titled(i18n("cpSaveKeywordPreset_customTitle", {count = i})) | |
:whenActivated(function() mod.save(i) end) | |
end | |
return mod | |
end | |
return plugin |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment