"on run argv
set theQuery to argv as text
set workflowFolder to do shell script "pwd"
set wlib to load script POSIX file (workflowFolder & "/q_workflow.scpt")
set wf to wlib'snew_workflow()
if (count of characters of theQuery) is not 8 then
set sTitle to "Enter a CEP"
add_result of wf without isvalid given theUID:"fastcep", theArg:theQuery, theTitle:sTitle, theAutocomplete:"", theSubtitle:"", theIcon:"icon.png", theType:""
else
set json to (do shell script "curl http://www.cepaberto.com/api/v2/ceps.json?cep=" & theQuery & " -H 'Authorization: Token token="9d36c686872348871a056df779e9ca"'")
if json is "{}" then
set sTitle to "CEP invalid"
add_result of wf without isvalid given theUID:"fastcep", theArg:sTitle, theTitle:sTitle, theAutocomplete:"", theSubtitle:"", theIcon:"icon.png", theType:""
else
set text item delimiters to {","bairro":"", ","bairro":", "","cep":"", ","cep":"", ","logradouro":"", "","cidade":"", "","ddd", ","estado":"", ""}"}
set sTitle to text item 4 of json & ", " & text item 2 of json & ", " & text item 5 of json & "/" & text item 7 of json
add_result of wf with isvalid given theUID:"cepaberto", theArg:sTitle, theTitle:sTitle, theAutocomplete:"", theSubtitle:"Copy to clipboard", theIcon:"icon.png", theType:""
end if
end if
return wf's to_xml("")
end run"