Using Location Helper App with AppleScript and KM

I'm trying hard though no success "I'm not no programmer"

Trying to make a applescript loading variables from KM into script then set KM variables.
I'm sure for beginners setvariable can be shorten.

I'm Having a error on line 19 to end see "Result" below.


Result:
error "Keyboard Maestro Engine got an error: Can’t make {45.444771337026, -73.649429224338} into type text." number -1700 from {45.444771337026, -73.649429224338} to text

tell application "Keyboard Maestro Engine"
   set geolocation to getvariable "geolocation"
   set a_coords to getvariable "a_coords"
   set b_location to getvariable "b_location"
   set a_record to getvariable "a_record"
   set c_coords to getvariable "c_coords"
end tell

tell application "Location Helper"
   -- Fetch the Google Map API information for the current location
   set a_coords to get location coordinates {geolocation}
   set b_location to reverse geocode location
   set a_record to get location record
   set c_coords to get location altitude
end tell

tell application "Keyboard Maestro Engine"
   setvariable "geolocation" to geolocation
   **setvariable "a_coords" to a_coords**
   **setvariable "b_location" to b_location**
   **setvariable "a_record" to a_record**
   **setvariable "c_coords" to c_coords**
end tell

I'm sorry for my troubles, just don't get this...

Best Regards,
William 

I don’t have Location Helper, so I can’t test your script for myself, but I can suggest changing

set a_coords to get location coordinates {geolocation}

to

set a_coords to get location coordinates {geolocation} as text

and seeing if that makes a difference. If that doesn’t work, try

set a_coords to (get location coordinates {geolocation} as text)

If either of those work, I’m guessing you may also need to make similar changes to the other lines in the Location Helper block.

By the way, I’m not sure you need the first Keyboard Maestro block, since you’re replacing all the variable values with ones from Location Helper anyway. I’d suggest commenting that entire block out too and see if it makes any difference to your script. As an AppleScript beginner myself: good luck!

Hey Bill,

This is an invalid statement:

set a_coords to get location coordinates {geolocation}

You can’t input {geolocation}

This is correct:

set a_coords to get location coordinates

The AppleScript error tells you all you need to know — you’re trying to assign an AppleScript list-object of real numbers to a Keyboard Maestro variable that only accepts text.

You can’t enter that into a Keyboard Maestro variable.

Here’s an example of breaking up a list into discrete Keyboard Maestro variables:

--------------------------------------------

tell application "Location Helper"
   set a_coords to get location coordinates
end tell

--------------------------------------------

set {theLongitude, theLatitude} to a_coords

# OR

set theLongitude to item 1 of a_coords
set theLatitude to item 2 of a_coords

--------------------------------------------

tell application "Keyboard Maestro Engine"
   setvariable "long" to theLongitude
   setvariable "lat" to theLatitude
end tell

--------------------------------------------

-Chris

Bill,

First, I hope you don't mind that I've retitled your topic to better reflect your question/issue.

Second, Chris (@ccstone) has already shown you the basic issue with your AppleScript script.

But, if you don't mind, I'd like to back up here and ask, what is your objective with the KM macro and script?

I don't think you need all of the Location Helper commands.
It largely depends on whether you want location information about the location of the Mac running the script, OR, of some other location.
Which do you want?

Please review this page about Location Helper:
Using Location Helper

The data that Location Helper provides is complex involving objects, records, lists, and text. Of course, KM can only store text, so you will have to convert the data in your AppleScript to text before you set the KM variables

Here's a revised script that will log results for the location of your Mac.
It should help you better understand the data from Location Helper.

Take a look at all of this, and let us know if you have more questions.

##example Script Using Location Helper

tell application "Location Helper"
  -- Fetch the Google Map API information for the current location
  set a_coords to get location coordinates ##{geolocation}
  set locationList to results of (reverse geocode location)
  set a_record to get location record
  set c_coords to get location altitude ## you don't need this
  
end tell

--- THESE COMMANDS DO NOT NEED Location Helper App ---
--    (best to put them outside of the tell block)

set AppleScript's text item delimiters to ","
set latLongStr to a_coords as text

set primeAddrStr to formatted_address of item 1 of locationList

set addrList to {}
repeat with oLoc in locationList
  set end of addrList to formatted_address of oLoc
end repeat

set mylatutude to lat of a_record
set mylongitude to lng of a_record
set myaltitude to altitude of a_record

set AppleScript's text item delimiters to linefeed

log ("latLongStr: " & latLongStr)
log ("primeAddrStr: " & primeAddrStr)
log ("addrList: " & addrList as text)
log ("mylatutude: " & mylatutude as text)
log ("mylongitude: " & mylongitude as text)
log ("myaltitude: " & myaltitude as text)

Bill, it is very hard to get a detailed description of the data Location Helper returns. Here is a screen shot of the Location Record (what LH calls MAP API info). HTH:

Also, there are two commands to get location info on locations other than your Mac:

--- FROM LAT/LONG COORDINATES ---
  set locFromLatLongRec to results of (reverse geocode location using coordinates {31, -94})
  
--- FROM PHYSICAL ADDRESS ---
  set locFromAddressRec to ¬
    results of (geocode address "1 Infinity Loop, Cupertino, California, USA")
  

Here's another more complete view, since I just learned how to "pretty print" in Script Debugger:

{
  {
    formatted_address:"Infinite Loop, Cupertino, CA 95014, USA", 
    partial_match:true, 
    address_components:{
      {
        short_name:"Infinite Loop", 
        long_name:"Infinite Loop", 
        |types|:{
          "route"
        }
      }, 
      {
        short_name:"Cupertino", 
        long_name:"Cupertino", 
        |types|:{
          "locality", 
          "political"
        }
      }, 
      {
        short_name:"Santa Clara County", 
        long_name:"Santa Clara County", 
        |types|:{
          "administrative_area_level_2", 
          "political"
        }
      }, 
      {
        short_name:"CA", 
        long_name:"California", 
        |types|:{
          "administrative_area_level_1", 
          "political"
        }
      }, 
      {
        short_name:"US", 
        long_name:"United States", 
        |types|:{
          "country", 
          "political"
        }
      }, 
      {
        short_name:"95014", 
        long_name:"95014", 
        |types|:{
          "postal_code"
        }
      }
    }, 
    geometry:{
      location_type:"GEOMETRIC_CENTER", 
      viewport:{
        northeast:{
          lat:37.3335129, 
          lng:-122.028577
        }, 
        southwest:{
          lat:37.3305164, 
          lng:-122.0321699
        }
      }, 
      |bounds|:{
        northeast:{
          lat:37.3335129, 
          lng:-122.028577
        }, 
        southwest:{
          lat:37.3305164, 
          lng:-122.0321699
        }
      }, 
      location:{
        lat:37.3322109, 
        lng:-122.0307778
      }
    }, 
    place_id:"ChIJ-7m057a1j4AR2VBPVzJDemk", 
    |types|:{
      "route"
    }
  }
}

I am looking to find altitude from a location “Full Address location” from Longitude and Altitude.
It seems these give me current location… ?

1 Like

I don't understand your request.

You have two choices:

  1. Get location info from latitude and longitude
  2. Get location info from physical address.

Which do you want?

In either case, I have already given you the answer:

  --- FROM LAT/LONG COORDINATES ---
  
  set locFromLatLongRec to results of (reverse geocode location using coordinates {31, -94})
  
  --- FROM PHYSICAL ADDRESS ---
  
  set locFromAddressRec to ¬
    results of (geocode address "1 Infinity Loop, Cupertino, California, USA")

Both return the location record as described above.
Unfortunately, this location record does NOT include altitude.
So it appears that the only altitude you can get is for your own location.

Here's a complete macro to get address from lat/long:

##Macro Library   @Location -- Get Address from Lat/Long @Example


####DOWNLOAD:
<a class="attachment" href="/uploads/default/original/2X/2/261c5a9f679edcf5f6a19b9aad52859d0817b366.kmmacros">@Location -- Get Address from Lat-Long @Example.kmmacros</a> (3.8 KB)
**Note: This Macro was uploaded in a DISABLED state. You must enable before it can be triggered.**

---

###Example Results

<img src="/uploads/default/original/2X/6/6d72438c52b5fcee6b361588e51bdf3dad3473e9.png" width="406" height="184">

---

###ReleaseNotes

Set the Action in magenta to the lat/long you want to use.

---

<img src="/uploads/default/original/2X/e/e1f313743617b0c8f4b8b7074a8d98695a50d0be.png" width="459" height="989">

---

###AppleScript 
```applescript
--- GET LAT/LONG STRING FROM KM ---
tell application "Keyboard Maestro Engine" to set kmLatLngStr to getvariable "GAC__LatLng"

--- CONVERT STRING TO LIST OF NUMBERS ---
set latLngToGetList to run script ("{" & kmLatLngStr & "}")

tell application "Location Helper"
  
  --- GET LOCATION RECORD FROM LAT/LONG COORDINATES ---
  set locFromLatLongRec to results of (reverse geocode location using coordinates latLngToGetList)
  
end tell

set addrFromLatLongStr to (formatted_address of item 1 of locFromLatLongRec)

return addrFromLatLongStr

--- IF YOU WANT TO SET KM VAR, USE THIS ---
-- tell application "Keyboard Maestro Engine" to setvariable "GAC__FullAddress" to addrFromLatLongStr
```
1 Like