Programmer’s Guides?

Complete newbie/neophyte here.

I would like to use Scriptable on my new iPhone, to see what’s possible, and maybe to help control things in my home.

Seems to me there are two parts to this:
Knowing enough about what iOS provides (in order to know where to start and what is possible)
Building literacy in the Javascript-ified interfaces to the iOS services.

There seems to be lots of Scriptable documentation at:
but in order for that to make sense, I need to be literate in the ways of iOS.

So, where should I start so that I can build my iOS “programmer literacy?”

Thanks in advance for your patience with a newcomer; I realize this is a very general question!

I would suggest that you start with the tutorials on MDN. Yes, they are about JavaScript in browsers, but it will help you nonetheless when you want to automate websites with Scriptable. And don’t freak out when you don’t understand what certain words mean. Just keep reading the tutorials. But not only read them, also try them for yourself. You’ll learn most when you try it yourself.

When you reached a level where you can read code without looking everything up and understand what most bits are doing (especially how Promises and async/await (aka. asynchronous Code) works and what classes are and how to use them (you don’t have to build classes yourself)) then you can start reading scripts that people made for Scriptable. Have a look in the gallery tab of Scriptable. There you’ll find simple scripts like Is Slack down?, What is my Elevation (widget), Reminders due today, Todays xkcd Comic. Generally, shorter scripts are easier to understand. Try them and see what they do. Try to read their code and from that you should slowly begin to understand how to use the functions that Scriptable provides.

Just one heads up: When you need to assign an object to a property of a Scriptable class (like request.body or request.header) then build the complete object beforehand and assign it as the last step. Otherwise if you try something like request.header["content-type"] = "application/json" then it won’t assign that. If you didn’t understand what I meant with that, then ignore it and come back when you have read and tried some Scriptable scripts. It will then (hopefully) make sense!

1 Like

@schl3ck thanks so much for taking the time to reply; sounds like the best way to learn is to:

  • build basic javascript literacy (paying particular attention to asynchronous programming)

  • Learn from examples in the gallery, i. e., look for something simple, to start with.

I’ll try to look for some simpler starter examples, then.

I’m still learning and will be dividing into asynchronous stuff soon. Here’s a script I wrote to remind myself of the basics. Copy and paste into Scriptable to see each part in action.

// To simply print a text on the screen do this. 
await QuickLook.present("hello world");

// "QuickLook.present" is versitile. It can load just about anything, from text to web pages, to  images and PDFs. 

await QuickLook.present("")

// Varables store chunks of data. 
// "var" variables can store anything. they don't complain much. 
// "const" are a kind of variable that isn't changed after being created. it is "read only". 
// "let" variables are manipulated and the values can change. 
// try changing the "let" to a "const" and a "var" to see what happens. 
let theAnswerIs = "The answer is:\n\n"
const five = 5
const six = 6
// notice addition here
const result = five + six
// here the "+" assembles the statements. 
theAnswerIs = theAnswerIs + result

await QuickLook.present(theAnswerIs)

// Interacting with the user can be done with alerts. 
// Creating alerts needs more steps. Similar to above, the following builds an alert from its components.  

// first, create a "let" variable. this is where the alert is assembled. 
let thisAlert = new Alert()
// Next, add in the properties.
// looking at the docs, Alert has two: title and message. they are added in like this. 
  thisAlert.title = "Result"
  thisAlert.message = theAnswerIs
// at the end is a "cancel" action. The text in quotes can be any text you want.
// The alert is triggered with the "await" statement
  await thisAlert.present()
// Alerts can be used to present options:

let optionAlert = new Alert()
  optionAlert.title = "Choose one:"
  optionAlert.message = "The user decides what happens next."
// alerts can have actions to choose from
  optionAlert.addAction("4 + 9")  
  optionAlert.addAction("5 + 6")
// The cancel action returns to the script without doing anything.  
// User's choice is saved in a variable  
  let usersChoice = await optionAlert.present()  
// the if-else block defines what happens with each option
  if (usersChoice == 0) {
    await QuickLook.present(4+9)  
  } else if (usersChoice == 1) {
    await QuickLook.present(result)

// I like to play with text, so RegEx is my jam
const cleanText = "hello world"
const name = "Mr. May"
// RegEx goes between /'s
const regex = /^.*lo/gm
const processed = regex.exec(cleanText)
await QuickLook.present(cleanText+"\n\n"+processed+", "+ name)