Currently Being Moderated

Script: Extract the Numerical GPS Values Using an Apple Script

Photos for Mac will display photos on a map, if the photos have been tagged with GPS data, but it does not show the numerical values for the GPS coordinates.

 

This little script will display latitude, longitude, altitude for the currently selected photo in a panel and copy the coordinates to the clipboard:

It works best, when the photo has been selected in the "All Photos" album.  Other albums and views may produce errors.

 

GPS tags are read only in the Apple Script dictionary for Photos, so it is not possible to use this kind of scripts to edit the GPS tags. We can only read and display them.

 

-- This script will display the GPS values and the altitude for a selected photo in a dialog panel.

-- The coordinates will be copied to the clipboard

 

on ensure_val(the_value, default_value)

  -- if the_value is defined return the value, otherwise the default value

  if the_value is equal to "missing value" then

  return default_value

  else

  return the_value

  end if

end ensure_val

 

-- on run {}

tell application "Photos"

  activate

 

  set imageSel to (get selection) -- get the selected image

 

  if imageSel is {} then

  error "Please select an image."

  end if

  tell the first item of imageSel

  set loc to get the location --retrieve longitude and latitude as list

  set lati to (the first item of loc) as string

  set longi to (the second item of loc) as string

  set alti to get the altitude as string -- retrieve the altitude

  end tell

 

end tell

 

-- check for undefined values

set lati to ensure_val(lati, "latitude empty")

set longi to ensure_val(longi, "longitude empty")

set alti to ensure_val(alti, "altitude empty")

 

-- display the result in a dialog panel

set panel_message to "Latitude: " & lati & "

Longitude: " & longi & "

Altitude: " & alti & "

 

Copied to the clipboard"

display dialog panel_message buttons ¬

  "OK" with icon caution default button "OK"

 

-- copy the GPS data to the clipboard

set clipboard_message to " " & lati & " " & longi & " " & alti

set the clipboard to clipboard_message

 

return clipboard_message

 

-- end run

 

Here is a second version, that you can try, if your library is a large iCloud Photo Library.  Then the script may give errors, if the photos have not been selected in the "All Photos" album.

There is a work-around for this problem proposed by NicFletcher:  Don't ask for the current selection, but add the photos you want to process to an album with a fixed name.

  • Create an album with a fixed name - mycurrentselection or similar - and add all photos you want to process to this album.
  • Instead of the "get current selection" process the photos in this album.

 

Here is a sample Apple Script by NicFletcher that uses this method:

https://discussions.apple.com/message/28087786#28087786

 

 

I modified my script a bit to include Nic's fix. Also I added some code to catch errors and to try again on error after a short delay.

 

-- This script will display the GPS values and the altitude for a selected photo in a dialog panel.

-- The coordinates will be copied to the clipboard

 

-- to pass the mage to be processed create a  top level album named PhotoDropBox

-- Add the photo as the first item to this album.

-- If you want to ccall this album differently, set the variable "theAlbumName" to this name.

 

on ensure_val(the_value, default_value)

  -- check if a value is defined and set it to the default, if it is undefined

  -- if the_value is defined return the value, otherwise the default value

  if the_value is equal to "missing value" then

  return default_value

  else

  return the_value

  end if

end ensure_val

 

--on run()

set ReadFromAlbum to true -- set this to true, if you want to pass the photos in a toplevel album

set theAlbumName to "PhotoDropBox" -- Enter the name of your album here.

 

 

set imageSel to {}

 

tell application "Photos"

  activate

  if (ReadFromAlbum) then -- the photos will be passed in a toplevel album named "PhotoDropBox"

 

  try

 

  if exists container theAlbumName then

 

  set thePhotosBuffer to container theAlbumName

  set imageSel to every media item of thePhotosBuffer

  else

  error "Album " & theAlbumName & "does not exist"

  end if

 

  on error errTexttwo number errNumtwo

  display dialog "Cannot open album: " & errNumtwo & return & errTexttwo

  end try

 

  else -- process the selected photos from the All Photos album

  try

  set imageSel to (get selection)

  on error errTexttwo number errNumtwo

  display dialog "Cannot get the selection: " & errNumtwo & return & errTexttwo

  end try

 

  end if

 

  -- check, if the album or the selected photos do contain images

  if imageSel is {} then

  error "Please select an image."

  else

 

  --  repeat with im in imageSel

  try

 

  tell the first item of imageSel

  set loc to get the location --retrieve longitude and latitude as list

  set lati to (the first item of loc) as string

  set longi to (the second item of loc) as string

  set alti to get the altitude as string -- retrieve the altitude

  end tell

  on error errText number errNum

  display dialog "Error: " & errNum & return & errText & "Trying again"

  try

  delay 2

  tell the first item of imageSel

  set loc to get the location --retrieve longitude and latitude as list

  set lati to (the first item of loc) as string

  set longi to (the second item of loc) as string

  set alti to get the altitude as string -- retrieve the altitude

  end tell

 

  on error errTexttwo number errNumtwo

  display dialog "Skipping image due to repeated error: " & errNumtwo & return & errTexttwo

  error "giving up"

  end try

  end try

  --  end repeat

  end if

end tell

 

-- check for undefined values

set lati to ensure_val(lati, "latitude empty")

set longi to ensure_val(longi, "longitude empty")

set alti to ensure_val(alti, "altitude empty")

 

-- display the result in a dialog panel

set panel_message to "Latitude: " & lati & "

Longitude: " & longi & "

Altitude: " & alti & "

 

Copied to the clipboard"

display dialog panel_message buttons ¬

  "OK" with icon caution default button "OK"

 

-- copy the GPS data to the clipboard

set clipboard_message to " " & lati & " " & longi & " " & alti

set the clipboard to clipboard_message

 

return clipboard_message

 

-- end run

Replies

Delete User Tip

Are you sure you want to delete this user tip?