Subway status in Buenos Aires

{
  "always_run_in_app" : false,
  "icon" : {
    "color" : "purple",
    "glyph" : "subway"
  },
  "name" : "Status of all Lines",
  "script" : "let url = \"http:\/\/www.infosubte.com.ar\/medios\/listinfosubte.asp\"\nlet req = new Request(url)\nlet json = await req.loadString()\n\n\n\/\/ table\nlet table = new UITable()\ntable.showSeparators = true\n\n\/\/ header\nlet row = new UITableRow()\nlet titleCell = row.addText(\"Subtes Buenos Aires\")\ntitleCell.widthWeight = 80\nrow.height = 60\nrow.cellSpacing = 10\nrow.isHeader = true\ntable.addRow(row)\n\nlet lines = json.split(\"<Linea>\")\nfor (line of lines) {\n  if (line.includes(\"<nombre>\")) {\n    \/\/ lines\n    let row = new UITableRow()\n    let titleCell = row.addText(name(line),\n                                state(line))\n    titleCell.widthWeight = 80\n    row.height = 60\n    row.cellSpacing = 10\n    table.addRow(row)\n  }\n}\n\nQuickLook.present(table)\n\n\/\/ concat status'\nlet stats = \"\"\nfor (line of lines) {\n  if (line.includes(\"<nombre>\")) {\n    if ((name(line).includes(\"B\") &&\n         !state(line).includes(\"Ok\")) ||\n        (name(line).includes(\"D\") &&\n         !state(line).includes(\"Ok\"))  ||\n        (name(line).includes(\"H\") &&\n         !state(line).includes(\"Ok\")) )\n        stats += name(line) + \": \" +\n                 state(line) + '.\\n'\n  }\n}\n\n\/\/ are they all ok?\nif (!stats.includes(\".\"))\n  stats = \"All your lines are ok!\"\n\nif (config.runsWithSiri) {\n  Speech.speak(stats)\n}\n\n\n\nfunction freq(item) {\n  let regex = \/<frecuencia>(.*)<\\\/frecuencia>\/\n  let matches = item.match(regex)\n  if (matches && matches.length >= 2) {\n    return matches[1]\n  } else {\n    return \"\"\n  }\n}\n\nfunction state(item) {\n  let regex = \/<estado>(.*)<\\\/estado>\/\n  let matches = item.match(regex)\n  if (matches && matches.length >= 2) {\n    return matches[1]\n  } else {\n    return \"Ok\"\n  }\n}\n\nfunction name(item) {\n  let regex = \/<nombre>(.*)<\\\/nombre>\/\n  let matches = item.match(regex)\n  if (matches && matches.length >= 2) {\n    return matches[1]\n  } else {\n    return null\n  }\n}",
  "share_sheet_inputs" : [

  ]
}

Is that how a Scriptable script is built? Looks nice and easy to confect.

How does one get a piece of JSON like that into Scriptable as a script?

Scriptable scripts are plain JavaScript files. There’s nothing magical about that. I do prepend a comment in the top of the JavaScript file that specifies script settings. If you have iCloud Drive enabled, you can find the scripts in the Scriptable folder in iCloud.

When sharing a script in Scriptable you get te option to share it as a .scriptable file. That’s what @Ioni_Nielavitzky have done. It’s a JSON file containing the script and some settings. However, you can also share the script as a plain JS file.

2 Likes