Beginner's Guide to Siri Shortcuts - Part 4: Scripting

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
welcome everybody to tech craft this is rob and today's video is part 4 of my step by step beginner's guide to iOS shortcuts in today's video we're going to learn all about scripting let's go you haven't seen the first three parts of this series yet make sure you check those out and I'll link them up above we're gonna dive straight in today to talk about scripting and about user input I'm going to work through three different shortcuts which I think will highlight all the cool scripting and user input features let's go so we'll start with something reasonably simple which is this shortcut here the favorite location shortcut actually let me show you how this looks if we run it from the home screen so here it is in my widget and I'm just gonna press favorite location say and it's popped up three locations that I have decided on my favorite ones my work my five guys for a burger and Liverpool Street Station which I travel into quite a bit so if I click on this it will bring up this map of driving instructions for me to get to Liverpool Street and obviously you can add any address you want to this list let's see how this is built okay so I'm back here in shortcuts and I've deleted everything from this shortcut apart from the actual input and what I've got here is a dictionary where the keys are my location names and the values are the actual post codes and you can use full street addresses here if that makes more sense where you are the first thing I want to do is prompt for which of these we want to travel to and that's really easy we can use the choose from list now there is a choose from menu action here I don't find that particularly useful let me drag that in and just explain why so all the options here are hard-coded it's not taking the options from the dictionary and this block as a whole doesn't have any return value it doesn't say return which which item you chose you have to put the individual items of code inside each block so let me show you what I mean by that if I just bring in a show alert option here and drop that into one so if I choose one this alert will fire if I choose two nothing will happen let's press play and choose two okay it returns the input which is our dictionary if I press play again and choose one this time I get the alert this is kind of hokey because I don't want to have to every time I add a new location add a new item to this menu and add a new block so I'm gonna scratch that the only options and we're gonna use to choose from list which is a little bit easier to run with when you have this kind of data-driven shortcut and I can go straight with choose from dictionary and get quite a nice user interface if I press play when I run it here I get these labels and I get they're kind of like the value which is the corresponding thing here which gives a nice interface and if I run this in the widget then what I get is just the keys themselves so that you don't have to mess around this is quite nice and this is one of the powerful things I think of shortcuts is that you get quite a nice default user interface just straight out of the box for most of the data types so now that we if we run this let me just show you what it actually returns so what it returns is the actual postcode so we don't even need to mess around trying to get into the dictionary to get the postal code we can straight away use the show directions action drag that in show driving directions to chosen item brilliant if I press play now and choose the upholstery station there we go it's giving me the directions to Liverpool Street Station I'm amazing I mean three actions and we have a really nice shortcut that we can use and I really am kind of enamored by how the user interface actions work I'd like to say that there's more to say about them but there really is and there are other things you can do like prompts boxes which we've seen already and you can send alerts and so forth but these kind of action menus for me are what I think are the most common user interface elements in shortcuts so what I want to do now is switch over and take a look at scripting and we'll see some more user interface elements as we do this what I really want to do is build on the shortcut that we saw from part 3 where we were showing the location of a particular photograph let's go okay so we're back in shortcuts and we have a an empty shortcut here and what we're going to do is build a shortcut that will prompt us to select some photographs find which of those photographs have a location attached to them and then just then ask us which one we'd like to see on a map okay so let's get started I want to do is bring in a find photos action here and I have a particular album that I'd like this to run from which is the shortcut Sundays test album and I'm not limiting this so this is gonna show me all photos in the album we can just press play to see what that looks like so quite a few images in here come now not always images have a location attached to them so what we need to do is run through this list and filter out the ones that have no location and for that we're going to use two of the most important scripting actions will use a repeat to do the looping part and we'll use an F to do the decision part you can find all of your kind of control flow things or peer and what we want to do is bring in a repeat action and actually there are two types of repeat repeat we'll just repeat a fixed number of times and repeat with each will repeat with each item in a list far far more useful let's bring that in and what this is happening now is it's just gonna loop over every photograph in this list and execute whatever code whatever scripting actions we put in the middle here so what I want to do first is maybe just show you what happens if we try to get the location for each of the photographs in that list if I get details of image which is this one here and the detail I want is location and okay press play so we've got six items returned here and if I just now take that out and just see how many images we had we've got about nine or ten images there so what's happening isn't because not all of these have a location the list when it has an empty value just ignores that I'm going to take advantage of that filtering capability to get rid of the photographs so let's bring an if statement here and we want to say if the location of the repeat item has some value then keep it otherwise get rid of it and notice that already we've got this special variable here repeat item and if you remember back to part two I said that there are some variables we hadn't covered yet and it's these particular repeat and repeat index variables which we'll see shortly and they the value of this will change every loop every time we go around this loop this is the photograph or the whatever list item for that iteration of the loop okay what I want though is the location so I can click on this it's audio photo and I'm going to just pick location so remember this is how we can get properties of an item directly we don't have to throw action for this and I'm just gonna say has any value so if the location has any value this block of code will execute otherwise this block will execute and I can get rid of the otherwise block if I want but what I actually quite like is just to be precise it's to just drag in an action called nothing put that here and make it clear to myself later on that I'm actually doing nothing in this block and then I'm going to we want to keep the photograph here and there's no handy way of just saying I want the result of this block to be whatever repeat item we had here so we're gonna use repeat index so I'm gonna say get item from list so I want to get a very specific item from the list and the list is this list of photos here so let's change that first of all to the photos and it's not the first item it's the item that a particular index and the index we want is our repeat index so it's worth revisiting what we're doing here we're going to loop over every photograph in the list in our album we're gonna say if the location of that photograph has any value then get that photograph out of the list otherwise do nothing and what I'm expecting to see now is a list of photographs that all have a location let's press play great so we now only have our photographs with a location attached to them the next thing to do is show these in a list and pick the one we want to show on the map so I'm going to use choose from list and press play to show you how that looks and we get a nice user interface here this again is just the power of how the user interface components work in shortcuts is that it shows the actual image I can pick this one say okay fine now we just need to show in oops let's just drag it on here so that would show the chosen item in maps and just notice that all these things are sticking together and as a reminder from previous times if you long press and click select magic variable you can see where all the different magic things come from let's press play and see how this works pick that one okay great that's in the Seychelles let me just show you a small bug though if I go back and press play and pick this photograph here nothing happens this was a photograph I took in Shenzhen in China and Maps does not have particularly good data coverage in China so what I'd like to do is filter out those images that I think that maps can't actually render and then we actually get a nicer experience so we can use another repeat and another if block before we do that though I want to just tidy this up a little bit because this is quite messy so the first thing I want to do is bring in a set variable here drop this in here I almost set this to the photos and I'm going to change this to be the photos and this to be the photos okay so now we know we're using this variable and what I really want to do is get rid of them now get rid of that for now is update the variable after that step okay so now I know that my photos are have been filtered out by those that have a location let's repeat this process and filter it out again finding the ones that have a location in China so we'll bring in a repeat with each action again over the photos which is great and now to filter out by location it's kind of difficult it's hard to know which location field the word China will appear in you'd think it would appear in country it actually doesn't so one thing we can do if you remember in part 3 is there's quite powerful type coercion we can move between locations and text for example is we can just get the whole location object as if it was text and just look for the word China inside it let's do that so I'm going to bring in a much text action here I want to match not in repeat item but in the location of repeat item but don't and I'm going to just look for the word China so I can literally just type so I can literally just type China in here and that's great so what wants to do is if we match here then this is a photograph that we're discarding if it would don't match here this is a photograph that we're keeping so we'll bring another if statement in this down here so if matches has any value then that is a photograph that we are discarding so we will do nothing so we will do nothing if matches doesn't have a value then this is a photograph that we're keeping and we're gonna use the same thing that we did up here get item our index get item from list drag that into the otherwise block from the photos we want item we want item at index and the index is the repeat index for this loop now we can press play and see if this works I'm expecting this to have filtered out quite a few images now okay cool so we're down to four images now this one this one oops that one interestingly this one is also taken in shame gem but it just has the word Asia in the list and that can be displayed on the map which I thought was quite interesting okay so let's just finish this off now so we need to bring in a choose from list again from the repeat results let's press play see what happens with that okay great so we can now see just the four items pick that one there and now the final thing is to show in maps and we want to shoot the choke show the chosen item in the map let's press play I'm gonna pick the one here in the seychelles and there we go we brought all the seychelles again so I find that repeat and if together a great this kind of filtering workflow it's very important if you're trying to build a good shortcut that you take out most of the invalid data if not all of the invalid data before you present it to your user so this is very important part of the workflow what we can also use repeat for though is to do bulk actions and I want to now show you a shortcut I really like which is a bulk image resizer let's see how that works so we're back here now with Blanka shortcut and the first thing I want to do is bring in a select photos because I want to be really good about this from the beginning I'm gonna put set variable in here I'm gonna call that the photos and you'll see why I do this very shortly I mean to expand show more here and I want to make sure that select multiple is enabled this is a bulk resizes so we want to be able to select multiple photographs okay cool the next thing is I want to prompt the user for how big the longest side of the image should be so we're gonna use another user interface component called ask for import we've seen this one a few times so far I'm just say how big and if I expand show more what we've seen so far in parts 1 2 & 3 is just text import but I'm gonna use numeric input here and the default value I want to be at 1024 always if you can provide a default it makes not just your debugging life easier it make sure users life easier as well beryllium if I just show you what the user interface looks like so far I'm asked for some photographs so if I scroll down to my test album here and maybe just select a few these and notice there's a different orientation and that is part of what I want to handle it in the shortcut click done how big fine so we go out how big and I'm gonna set that as a variable let's call that size so the next thing I want to do start looping over these photographs and actually resizing them and obviously for a loop we know we need a repeat because we're looping over a list it's a repeat with each let's drop that in there and what I want to do is not with each item in size is each item in the photos and I want to resize along the longest edge so I need to find out which edge is longest and if we want to make a decision we just use the if so I'm gonna drop that if in here so I was get the width in the height of the photograph so I'm gonna click on here and try to pick up that property now what happens is it detected the type of repeat item as a number never quite figure out why this is we know that the photos is a list of photos so we know that the repeat item should be a photo or an image so I can click on the type here and I can actually tell shortcuts that this should be an in edge and then I can get the width from it click done and now it's with this is numeric so we get a lot more conditions this time in our if statement we're not just checking if it has or does not have a value we can do number type things so this is the power of the conditional and the power of the datatypes working together this is a number so we can do number of things and I won't say if the width is greater than the height and we have to repeat this whole process again I've setting the type to be image and choosing height click done I'm going to resize along the width so we get a resize image option this is great so the image resizing is the repeat item this is what we want to resize it's the width and we want that to be size and we just going to leave the height as auto and then we'll repeat the same thing in the other wise block but we'll resize the height instead and leave the width to be automatic so get rid of that go into auto height and choose size so this is basically what I want to happen I want these to be resized this is looking quite nice in a press play choose Mauri photographs 1024 is fine and there we go we have a resized images now you can't see that and I'm going to show you a little bit bit more clearly in a second by making this very small but there are a few more things I want to do to this first when I'm debugging this constantly selecting the photographs is a real pain so because we've made a variable called the photos and the rest of the shortcut is using that variable everywhere not the magic variable we can change what the value is so I'm going to drop a fine photos in here get rid of that set that variable to be just what we come from find and choose my album here I don't want to limit I want them all this means that now when I press ruin I don't have to go through that whole selection process and it will resize every image in that album this is a very useful pattern when you are debugging and working on a short core and the later on I can just go back and switch this back to select before I do that though I have a few more things that I want to do to this shortcut and one of those he's actually set up somewhere to save these images to so here I am in the Photos app and I've created this album called resize and it's currently empty and what I want to do is every time I run a bulk resize I want to save the photos to this album so back in shortcuts now what we gonna do is Chuck a save to photo album action at the bottom here we want to save the repeat results cuz we know that's a list of images I don't want say this to my recent album I want to save this to my resized album that I'd selected earlier so if I press play now remember we've still got the fine photos action so we don't need to actually select any I'm gonna save these as 128 so we can see how small they go there we go that's the output there but if I switch to photos you can see I've got a lot of very small images but they've all been resized along their longest edge really zoomed in there okay cool one more thing I want to do is actually make it so that if I'm inside the Photos app I can just click on one of these and then in the share sheet also resize it so let's add that to our shortcut as well so I'm back in shortcuts and ivory added our select photos action here and what I'm going to do is click Next to save this and then we're gonna make this shortcut appear in the share sheet so I'm just gonna call this B R for bulk resize done now bring up the three dots three dots again and we're into the shortcut properties this shortcut will already show in the widget and when I run it from the widget I want that select photo UI to show up and I also want girls running from the share sheet passing in a specific image it's already got the share sheet types as images but normally what you'll see is that it has all of them selected if I press D selects all and then press images that's the type that I want okay cool I think he's fine so now that the shortcut is appearing in the share sheet we have this accepts and that tells that we accept an image and you remember from part two that this means there's now a special variable called shortcuts input and if we have that we want to use that otherwise you want to do the select let's bring an if-statement him already picks up shortcut input as the value we're testing so if that has any value then we will set a variable which is the photos to be that otherwise I just want to do these actions here so I'm going to drag them into here so this means if we don't have a shortcut input will prompt for the photographs otherwise we'll use the shortcut input so I can press dumb let's just run this from the Photos app so I've cleared out my resized album and I'm going to just go and pick a photograph from here and I'll choose this one to resize it and click on the share sheet I'll scroll down until we see our BR shortcut here Press that how big we want it to be we'll say 64 very small okay and that's done so if we go back to our resize album now we can see there's our resize photograph so as you've seen from this video there scripting features and the user input features are actually fairly simple to use inside of shortcuts but they are really powerful I think it's really the scripting and the user input that brings together everything we've seen so far and makes it usable in real world kind of useful workflows and useful shortcuts without being ultimate decisions without being able to change what you do based on the size of your input lists or the size of your input dictionaries you can't really do anything powerful but having those extra features really unlocks the power of shortcuts I really hope that you found this video useful and I hope you found it entertaining if so please hit like please hit subscribe and don't just hit subscribe but hit the bell as well so you don't miss out on any future content thanks for watching and I'll see you in the next video
Info
Channel: Tech Craft
Views: 125,071
Rating: undefined out of 5
Keywords: siri shortcuts for beginners, siri shortcuts, siri shortcuts tutorial, shortcuts walkthrough, how to use siri shortcuts, getting started with siri shortcuts, shortcuts app for beginners, siri shortcuts ios 13 tutorial, how to use shortcuts, shortcuts app, shortcuts tutorial, shortcuts, siri, apple shortcuts, iphone 11, ios13 shortcuts
Id: LCiahAWsJRY
Channel Id: undefined
Length: 21min 53sec (1313 seconds)
Published: Sun Dec 22 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.