Created
December 18, 2021 13:46
-
-
Save Technifocal/6284a6d2bfc76fa1fac18c1e9121c3bd 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
function reloadConfig(files) | |
doReload = false | |
for _,file in pairs(files) do | |
if file:sub(-4) == ".lua" then | |
doReload = true | |
end | |
end | |
if doReload then | |
hs.reload() | |
end | |
end | |
log = hs.logger.new('init', 5) | |
suisApp = hs.application.find('Control Centre') | |
suisAX = hs.axuielement.applicationElement(suisApp) | |
controlCentreMenuSearch = hs.axuielement.searchCriteriaFunction({ | |
{attribute = 'AXRole', value = 'AXMenuBarItem'}, | |
{attribute = 'AXSubrole', value = 'AXMenuExtra'}, | |
{attribute = 'AXIdentifier', value = 'com.apple.menuextra.controlcenter'} | |
}); | |
sideCarMenuSearch = hs.axuielement.searchCriteriaFunction({ | |
{attribute = 'AXRole', value = 'AXCheckBox'}, | |
{attribute = 'AXSubrole', value = 'AXToggle'}, | |
{attribute = 'AXIdentifier', value = 'controlcenter-sidecar'} | |
}); | |
iPadConnectionButton = hs.axuielement.searchCriteriaFunction({ | |
{attribute = 'AXRole', value = 'AXCheckBox'}, | |
{attribute = 'AXSubrole', value = 'AXToggle'}, | |
{attribute = 'AXIdentifier', value = 'sidecar-device-iPad'} | |
}); | |
connectToSidecarItemSearchTimer = nil; | |
sidecarItemFound = false; | |
function stopConnectToSidecarItemSearchTimer() | |
print("stopConnectToSidecarItemSearchTimer") | |
if connectToSidecarItemSearchTimer then | |
connectToSidecarItemSearchTimer:stop() | |
connectToSidecarItemSearchTimer = nil | |
end | |
end | |
function iPadConnectionButtonHandler(msg, results, count) | |
print("iPadConnectionButtonHandler"); | |
if count == 0 then | |
suisAX:buildTree(function(treeMsg, msgResults) | |
print(hs.inspect(msgResults)); | |
end) | |
return log:d("Can't get ipad menu entry:", msg) | |
end | |
sidecarItemFound = true; | |
results[1]:doAXPress() | |
end | |
function sideCarMenuSearchHandler(msg, results, count) | |
print("sideCarMenuSearchHandler"); | |
if count == 0 then | |
return log:d("Can't get side car menu:", msg) | |
end | |
results[1]:doAXPress() | |
hs.timer.doAfter(5, function() stopConnectToSidecarItemSearchTimer() end) | |
connectToSidecarItemSearchTimer = hs.timer.doUntil( | |
function() | |
return sidecarItemFound | |
end, | |
function() | |
suisAX:elementSearch(iPadConnectionButtonHandler, iPadConnectionButton, {count = 1, depth = 3}) | |
end, | |
0.5 | |
) | |
end | |
function contrlCentreMenuSearchHandler(msg, results, count) | |
print("contrlCentreMenuSearchHandler"); | |
if count == 0 then | |
return log:d("Can't get control centre menu:", msg) | |
end | |
results[1]:doAXPress() | |
suisAX:elementSearch(sideCarMenuSearchHandler, sideCarMenuSearch, {count = 1, depth = 2}) | |
end | |
function isIpadConnected() | |
for index,value in ipairs(hs.screen.allScreens()) do | |
if(value:getUUID() == "225CA0A0-84E0-4C6D-B129-2C2964F1BE17") then | |
return true; | |
end | |
end | |
return false; | |
end | |
function connectIpad() | |
if(isIpadConnected()) then | |
return; | |
end | |
suisAX:elementSearch(contrlCentreMenuSearchHandler, controlCentreMenuSearch, {count = 1, depth = 2}) | |
end | |
myWatcher = hs.pathwatcher.new(os.getenv("HOME") .. "/.hammerspoon/", reloadConfig):start() | |
hs.hotkey.bind({"ctrl", "alt"}, "left", function() | |
connectIpad(); | |
hs.timer.waitUntil(isIpadConnected, function() | |
for index,value in ipairs(hs.screen.allScreens()) do | |
if(value:getUUID() == "37D8832A-2D66-02CA-B9F7-8F30A301B230") then | |
value:setOrigin(0, 0); | |
elseif(value:getUUID() == "225CA0A0-84E0-4C6D-B129-2C2964F1BE17") then | |
value:setOrigin(-1366, 93) | |
end | |
end | |
end) | |
end) | |
hs.hotkey.bind({"ctrl", "alt"}, "right", function() | |
connectIpad(); | |
hs.timer.waitUntil(isIpadConnected, function() | |
for index,value in ipairs(hs.screen.allScreens()) do | |
if(value:getUUID() == "37D8832A-2D66-02CA-B9F7-8F30A301B230") then | |
value:setOrigin(0, 0); | |
elseif(value:getUUID() == "225CA0A0-84E0-4C6D-B129-2C2964F1BE17") then | |
value:setOrigin(1728, 93) | |
end | |
end | |
end) | |
end) | |
hs.console.clearConsole(); | |
hs.alert.show("Config loaded") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment