iPhone Automation with Scriptable
What is Scriptable?
Scriptable is a free iPhone app that lets you write small programs (called scripts) using JavaScript to automate tasks on your phone. Think of it as giving your iPhone superpowers - you can create custom widgets, automate repetitive tasks, and build mini-apps tailored to your specific needs.
What makes Scriptable special?
- Completely Free: No subscription fees or in-app purchases
- Powerful Access: Can access your location, contacts, calendar, photos, and more
- Widget Support: Create custom home screen widgets
- Beginner Friendly: Copy-paste scripts work perfectly
Getting Started
Download Scriptable
First, download the Scriptable app from the App Store. It's completely free and developed by Simon Støvring.
Search for "Scriptable" in the App Store
Open Scriptable
Once installed, open the app. You'll see a clean interface with a "+" button at the top right. This is where you'll create your first script.
Creating Your Location Saver App
Create a New Script
Tap the "+" button in Scriptable to create a new script. You'll see a blank editor where you can write or paste code.
Name Your Script
Tap on "Untitled Script" at the top and rename it to something meaningful like "Location Saver" or "My Places".
Add the Code
Here's where the magic happens! Copy and paste the following code into your script. Don't worry about understanding every line - I'll explain the important parts below.
// Siri-Enhanced Location Saver Script for Scriptable
// Supports parameters from Shortcuts app for better Siri integration
const LOCATIONS_KEY = "saved_locations";
// Get parameter from Shortcuts app if available
const action = args.shortcutParameter || "menu";
// Main function with parameter support
async function main() {
switch (action.toLowerCase()) {
case "save":
await saveCurrentLocation();
break;
case "list":
case "show":
case "view":
await viewSavedLocations();
break;
case "quick-save":
await quickSaveLocation();
break;
default:
await showMenu();
}
}
// Show main menu
async function showMenu() {
const alert = new Alert();
alert.title = "Location Saver";
alert.message = "What would you like to do?";
alert.addAction("Save Current Location");
alert.addAction("Quick Save (Auto-named)");
alert.addAction("View Saved Locations");
alert.addAction("Cancel");
const choice = await alert.presentAlert();
if (choice === 0) {
await saveCurrentLocation();
} else if (choice === 1) {
await quickSaveLocation();
} else if (choice === 2) {
await viewSavedLocations();
}
}
// Quick save with automatic naming
async function quickSaveLocation() {
try {
Location.setAccuracyToKilometer();
const location = await Location.current();
if (!location) {
showError("Unable to get current location. Please check location permissions.");
return;
}
// Auto-generate name with timestamp
const now = new Date();
const locationName = `Location ${now.toLocaleDateString()} ${now.toLocaleTimeString([], {hour: '2-digit', minute:'2-digit'})}`;
const savedLocation = {
name: locationName,
latitude: location.latitude,
longitude: location.longitude,
timestamp: now.toISOString(),
address: await getAddressFromCoordinates(location.latitude, location.longitude)
};
const existingLocations = getSavedLocations();
existingLocations.push(savedLocation);
Keychain.set(LOCATIONS_KEY, JSON.stringify(existingLocations));
// Show notification instead of alert for quicker Siri experience
const notification = new Notification();
notification.title = "Location Saved";
notification.body = `Saved as "${locationName}"`;
notification.schedule();
console.log(`Location saved: ${locationName}`);
} catch (error) {
showError(`Error saving location: ${error.message}`);
}
}
// Save current location with custom name
async function saveCurrentLocation() {
try {
Location.setAccuracyToKilometer();
const location = await Location.current();
if (!location) {
showError("Unable to get current location. Please check location permissions.");
return;
}
const nameAlert = new Alert();
nameAlert.title = "Save Location";
nameAlert.message = "Enter a name for this location:";
nameAlert.addTextField("Location name", "");
nameAlert.addAction("Save");
nameAlert.addAction("Cancel");
const nameChoice = await nameAlert.presentAlert();
if (nameChoice === 0) {
const locationName = nameAlert.textFieldValue(0);
if (!locationName || locationName.trim() === "") {
showError("Please enter a valid location name.");
return;
}
const savedLocation = {
name: locationName.trim(),
latitude: location.latitude,
longitude: location.longitude,
timestamp: new Date().toISOString(),
address: await getAddressFromCoordinates(location.latitude, location.longitude)
};
const existingLocations = getSavedLocations();
existingLocations.push(savedLocation);
Keychain.set(LOCATIONS_KEY, JSON.stringify(existingLocations));
showSuccess(`Location "${locationName}" saved successfully!`);
}
} catch (error) {
showError(`Error saving location: ${error.message}`);
}
}
// View saved locations
async function viewSavedLocations() {
const locations = getSavedLocations();
if (locations.length === 0) {
showError("No saved locations found.");
return;
}
const listAlert = new Alert();
listAlert.title = "Saved Locations";
listAlert.message = "Select a location to open in Google Maps:";
locations.forEach(location => {
const displayText = `${location.name}\n${location.address || 'Unknown address'}`;
listAlert.addAction(displayText);
});
listAlert.addAction("Delete Location");
listAlert.addAction("Cancel");
const choice = await listAlert.presentAlert();
if (choice < locations.length) {
await openInGoogleMaps(locations[choice]);
} else if (choice === locations.length) {
await deleteLocation();
}
}
// Delete a saved location
async function deleteLocation() {
const locations = getSavedLocations();
if (locations.length === 0) {
showError("No saved locations to delete.");
return;
}
const deleteAlert = new Alert();
deleteAlert.title = "Delete Location";
deleteAlert.message = "Select a location to delete:";
locations.forEach(location => {
deleteAlert.addAction(location.name);
});
deleteAlert.addAction("Cancel");
const choice = await deleteAlert.presentAlert();
if (choice < locations.length) {
const confirmAlert = new Alert();
confirmAlert.title = "Confirm Delete";
confirmAlert.message = `Are you sure you want to delete "${locations[choice].name}"?`;
confirmAlert.addAction("Delete");
confirmAlert.addAction("Cancel");
const confirmChoice = await confirmAlert.presentAlert();
if (confirmChoice === 0) {
locations.splice(choice, 1);
Keychain.set(LOCATIONS_KEY, JSON.stringify(locations));
showSuccess("Location deleted successfully!");
}
}
}
// Open location in Google Maps
async function openInGoogleMaps(location) {
const googleMapsUrl = `https://maps.google.com/?q=${location.latitude},${location.longitude}`;
try {
await Safari.open(googleMapsUrl);
} catch (error) {
const appleMapsUrl = `http://maps.apple.com/?ll=${location.latitude},${location.longitude}&q=${encodeURIComponent(location.name)}`;
await Safari.open(appleMapsUrl);
}
}
// Get saved locations from storage
function getSavedLocations() {
try {
const stored = Keychain.get(LOCATIONS_KEY);
return stored ? JSON.parse(stored) : [];
} catch (error) {
console.error("Error reading saved locations:", error);
return [];
}
}
// Get address from coordinates
async function getAddressFromCoordinates(latitude, longitude) {
try {
const location = await Location.reverseGeocode(latitude, longitude);
if (location && location.length > 0) {
const addr = location[0];
return `${addr.thoroughfare || ''} ${addr.locality || ''} ${addr.administrativeArea || ''}`.trim();
}
} catch (error) {
console.log("Could not get address:", error);
}
return `${latitude.toFixed(6)}, ${longitude.toFixed(6)}`;
}
// Show success message
function showSuccess(message) {
const alert = new Alert();
alert.title = "Success";
alert.message = message;
alert.addAction("OK");
alert.presentAlert();
}
// Show error message
function showError(message) {
const alert = new Alert();
alert.title = "Error";
alert.message = message;
alert.addAction("OK");
alert.presentAlert();
}
// Run the main function
await main();
How Your Location Saver Works
Let me break down what this script does in simple terms:
Saving Locations
When you choose "Save Current Location," the script asks your iPhone for your current GPS coordinates, lets you give it a custom name, and stores it securely on your device.
Managing Your List
All your saved locations are stored in a list that you can view anytime. Each location includes the name you gave it, the exact coordinates, and a readable address.
Opening in Maps
Tap any saved location to open it directly in Google Maps (or Apple Maps as a backup). Perfect for navigation or sharing with friends!
Using Your New Location Saver
Run Your Script
Once you've pasted the complete code, tap the play button (▶️) at the bottom of the screen. Your script will run and show you a menu with options.
Grant Permissions
The first time you run the script, iOS will ask for location permission. Make sure to tap "Allow" so the app can access your current location.
Save Your First Location
Try saving your current location! Tap "Save Current Location," give it a name like "Home" or "Office," and watch as your script saves it with the exact coordinates and address.
- Save parking spots in busy areas
- Remember great photo locations
- Keep track of new restaurants you want to try
- Save meeting points for group activities
- Create a personal map of your favorite places
Making It Even More Convenient
Add to Home Screen
Want quick access to your Location Saver? You can add it directly to your iPhone's home screen:
- In Scriptable, tap the settings icon (⚙️) next to your script
- Tap "Add to Home Screen"
- Choose an icon and name
- Tap "Add" in the top right
Now you'll have a custom app icon on your home screen that runs your Location Saver directly!
Create a Widget
Scriptable also supports widgets! You could modify the script to show your most recent saved locations right on your home screen. This is more advanced, but it's a great next step once you're comfortable with the basics.
Common Issues and Solutions
❌ "Unable to get current location"
Solution: Go to Settings → Privacy & Security → Location Services → Scriptable, and make sure it's set to "While Using App" or "Always."
❌ Script shows errors when running
Solution: Make sure you copied the complete script. Missing parts of the code will cause errors. Double-check that you have all the functions included.
❌ Locations not saving
Solution: This usually means there's a small typo in the code. Try deleting the script and creating it again with a fresh copy of the code.
What's Next?
Congratulations! You've just created your first iPhone automation. This Location Saver is just the beginning - Scriptable can do so much more:
Calendar Automations
Create scripts that automatically add events, send meeting reminders, or show your schedule in custom widgets.
Photo Management
Build tools to organize your photos, create albums automatically, or even add watermarks to images.
Data Processing
Parse CSV files, connect to web APIs, or create custom dashboards with your personal data.
Home Screen Widgets
Create beautiful, functional widgets that show weather, stock prices, to-do lists, or anything else you can imagine.
Learning Resources
Ready to dive deeper into Scriptable? Here are some great resources:
- Scriptable Documentation: The official docs have examples and API references
- Reddit r/Scriptable: A community of users sharing scripts and helping each other
- GitHub Scriptable Scripts: Hundreds of ready-to-use scripts you can copy and modify
- YouTube Tutorials: Visual learners can find step-by-step video guides
Final Thoughts
Creating your first Scriptable automation might seem intimidating at first, but as you can see, it's really just about copying, pasting, and running code that others have written. The Location Saver we built today is a practical tool you can use every day, and it's given you the foundation to explore much more complex automations.
The best part about Scriptable is that you don't need to be a programmer to benefit from it. There's a whole community of users sharing scripts, and most of the time, you can just copy what someone else has built and adapt it to your needs.
Remember: Every expert was once a beginner. Start with simple scripts like this Location Saver, experiment with modifications, and gradually work your way up to more complex automations. Before you know it, you'll be creating custom iPhone automations that make your life easier and more efficient!
🎉 You Did It!
You've successfully created your first iPhone automation with Scriptable. Share your success and inspire others to start their automation journey!