<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
	<key>bundleid</key>
	<string>switchhosts.oldj.net</string>
	<key>category</key>
	<string>Tools</string>
	<key>connections</key>
	<dict>
		<key>E4D66445-FD72-47A2-9EE6-7232A2BADE29</key>
		<array>
			<dict>
				<key>destinationuid</key>
				<string>78D17FD5-9628-4901-A01A-511528D5FC14</string>
				<key>modifiers</key>
				<integer>0</integer>
				<key>modifiersubtext</key>
				<string></string>
				<key>vitoclose</key>
				<false/>
			</dict>
		</array>
	</dict>
	<key>createdby</key>
	<string>oldj</string>
	<key>description</key>
	<string>Switch hosts quickly!</string>
	<key>disabled</key>
	<false/>
	<key>name</key>
	<string>SwitchHosts</string>
	<key>objects</key>
	<array>
		<dict>
			<key>config</key>
			<dict>
				<key>concurrently</key>
				<false/>
				<key>escaping</key>
				<integer>102</integer>
				<key>script</key>
				<string>curl 'http://127.0.0.1:50761/api/toggle?id={query}'</string>
				<key>scriptargtype</key>
				<integer>0</integer>
				<key>scriptfile</key>
				<string></string>
				<key>type</key>
				<integer>0</integer>
			</dict>
			<key>type</key>
			<string>alfred.workflow.action.script</string>
			<key>uid</key>
			<string>78D17FD5-9628-4901-A01A-511528D5FC14</string>
			<key>version</key>
			<integer>2</integer>
		</dict>
		<dict>
			<key>config</key>
			<dict>
				<key>alfredfiltersresults</key>
				<false/>
				<key>alfredfiltersresultsmatchmode</key>
				<integer>0</integer>
				<key>argumenttreatemptyqueryasnil</key>
				<false/>
				<key>argumenttrimmode</key>
				<integer>0</integer>
				<key>argumenttype</key>
				<integer>1</integer>
				<key>escaping</key>
				<integer>68</integer>
				<key>keyword</key>
				<string>swh</string>
				<key>queuedelaycustom</key>
				<integer>3</integer>
				<key>queuedelayimmediatelyinitially</key>
				<true/>
				<key>queuedelaymode</key>
				<integer>0</integer>
				<key>queuemode</key>
				<integer>1</integer>
				<key>runningsubtext</key>
				<string>loading...</string>
				<key>script</key>
				<string>function makeItems(items) {
  return items.map(item =&gt; {
    return {
      uid: item.id,
      title: item.title,
      arg: item.id,
      icon: {path: item.on ? 'on.png' : 'off.png'},
      subtitle: (item.content || '').split('\n')[0],
    }
  })
}

function run(argv) {
  const server = 'http://127.0.0.1:50761'
  // console.log(argv)
  const queryURL = $.NSURL.URLWithString(`${server}/api/list`)
  const requestData = $.NSData.dataWithContentsOfURL(queryURL)
  const requestString = $.NSString.alloc.initWithDataEncoding(requestData, $.NSUTF8StringEncoding).js

  let result
  try {
    result = JSON.parse(requestString)
    result.data = result.data.filter((item)=&gt;item.title.includes("{query}"))
  } catch (e) {
    console.log(e)
    return JSON.stringify({
      items: [{
        uid: '0',
        title: `API Error: ${server}`,
        subtitle: 'Make sure SwitchHosts is running and the HTTP API interface is enabled.',
        valid: false,
      }]
    })
  }

  if (result.success) {
    return JSON.stringify({
      items: makeItems(result.data)
    })
  }

  return JSON.stringify({
    items: [{
      uid: '0',
      title: `Error: ${result.message || result.code || 'Unknown'}`,
      valid: false,
    }]
  })
}</string>
				<key>scriptargtype</key>
				<integer>0</integer>
				<key>scriptfile</key>
				<string></string>
				<key>subtext</key>
				<string>Switch hosts quickly!</string>
				<key>title</key>
				<string>Show hosts..</string>
				<key>type</key>
				<integer>7</integer>
				<key>withspace</key>
				<true/>
			</dict>
			<key>type</key>
			<string>alfred.workflow.input.scriptfilter</string>
			<key>uid</key>
			<string>E4D66445-FD72-47A2-9EE6-7232A2BADE29</string>
			<key>version</key>
			<integer>3</integer>
		</dict>
	</array>
	<key>readme</key>
	<string>This workflow is for the SwitchHosts App.

https://switchhosts.vercel.app</string>
	<key>uidata</key>
	<dict>
		<key>78D17FD5-9628-4901-A01A-511528D5FC14</key>
		<dict>
			<key>xpos</key>
			<integer>340</integer>
			<key>ypos</key>
			<integer>30</integer>
		</dict>
		<key>E4D66445-FD72-47A2-9EE6-7232A2BADE29</key>
		<dict>
			<key>xpos</key>
			<integer>120</integer>
			<key>ypos</key>
			<integer>30</integer>
		</dict>
	</dict>
	<key>variablesdontexport</key>
	<array/>
	<key>version</key>
	<string>1.3.0</string>
	<key>webaddress</key>
	<string>https://switchhosts.vercel.app</string>
</dict>
</plist>
