Action

Location

Posted by taurean, Last update 6 days ago

Appends an Apple Maps URL and long/lat of your current location to the end of a note, then moves your cursor above the added text. Also adds Apple map URL to clipboard

Steps

  • script

    // Location Saver - Gets current GPS location and adds to note
    // Copies Apple Maps URL to clipboard using first line as location name
    
    function getLocationWithRetry(maxAttempts = 5, delayMs = 2000) {
        let attempt = 0;
        
        while (attempt < maxAttempts) {
            attempt++;
            
            let currentLat = draft.processTemplate("[[latitude]]");
            let currentLon = draft.processTemplate("[[longitude]]");
            
            if (currentLat && currentLon && 
                currentLat !== "0.0" && currentLon !== "0.0" && 
                currentLat.trim() !== "" && currentLon.trim() !== "" &&
                parseFloat(currentLat) !== 0 && parseFloat(currentLon) !== 0) {
                
                return { lat: currentLat, lon: currentLon, success: true };
            }
            
            if (attempt < maxAttempts) {
                alert(`Getting GPS location... Attempt ${attempt}/${maxAttempts}\n\nMake sure you're near a window or outside for better signal.`);
                
                let start = Date.now();
                while (Date.now() - start < delayMs) {
                    // Delay between attempts
                }
            }
        }
        
        return { success: false };
    }
    
    let locationResult = getLocationWithRetry(3, 3000);
    
    if (!locationResult.success) {
        alert("Could not get GPS location after multiple attempts.\n\nTry:\n• Going outside or near a window\n• Checking location services are enabled");
        context.fail();
    }
    
    // Get name from first line, remove heading markup
    let locationName = "Marked%20Location";
    if (draft.title && draft.title.trim() !== "") {
        let cleanTitle = draft.title.trim().replace(/^#+\s*/, "");
        locationName = encodeURIComponent(cleanTitle);
    }
    
    // Create Apple Maps URL and copy to clipboard
    let mapsURL = `https://maps.apple.com/place?coordinate=${locationResult.lat},${locationResult.lon}&name=${locationName}&map=explore`;
    app.setClipboard(mapsURL);
    
    // Create location block
    let locationBlock = `
    
    
    📍
    ${mapsURL}
    
    Long: ${locationResult.lon}
    Lat: ${locationResult.lat}`;
    
    // Insert location block and position cursor above it
    let currentPosition = editor.getSelectedRange()[0];
    
    if (currentPosition === draft.content.length && draft.content.length > 0) {
        editor.setSelectedText("\n");
        currentPosition = editor.getSelectedRange()[0];
    }
    
    editor.setSelectedText(locationBlock);
    editor.setSelectedRange(currentPosition, 0);
    
    draft.addTag("location");
    draft.update();

Options

  • After Success Default
    Notification Info
    Log Level Info
Items available in the Drafts Directory are uploaded by community members. Use appropriate caution reviewing downloaded items before use.