Last active
October 29, 2020 01:47
-
-
Save jdtsmith/84a89d41a1a4802ca3e2117f5249ddd7 to your computer and use it in GitHub Desktop.
Simple example of coroutine based recursive searchPath
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
ax=hs.axuielement | |
dockAX = ax.applicationElement(hs.application("Dock")) | |
ids={"mc","mc.display","mc.spaces"} | |
scf={} | |
for i,id in ipairs(ids) do | |
table.insert(scf,ax.searchCriteriaFunction( | |
{{attribute="AXRole", value="AXGroup"}, | |
{attribute="AXIdentifier",value=id}})) | |
end | |
function searchPath(start,searchCriteriaFunctions,callback) | |
local co=coroutine.create(function (myco) | |
local searchResume = function (msg,results,cnt) | |
coroutine.resume(myco,results,cnt) | |
end | |
local elem={start} | |
for _,search in ipairs(searchCriteriaFunctions) do | |
for _,el in ipairs(elem) do -- search all in parallel | |
el:elementSearch(searchResume, search, {depth=1}) | |
end | |
local num=#elem | |
elem={} | |
if num==0 then break end | |
for _=1,num do -- wait for all searches to return | |
res,cnt=coroutine.yield() | |
for _,r in ipairs(res) do | |
table.insert(elem,r) | |
end | |
end | |
end | |
callback(elem) | |
end) | |
coroutine.resume(co,co) | |
end | |
-- Invoke Mission Control before running the Search | |
hs.timer.doAfter(2,function() | |
searchPath(dockAX,scf, | |
function (els) | |
print('Results: ') | |
for i,v in ipairs(els) do | |
print(v,v.AXIdentifier) | |
end | |
end) | |
end) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment