AppleScripts for use with DarwinPorts Installation

The following three AppleScripts will get you started with your DarwinPorts installation of GPSBabel. They can be used with the gpsbabel file that’s part of the MacGPSBabel installation with minor changes.

These three scripts are written for uploading and downloading GPX formatted files, and for converting (the downloaded) GPX files to KML for use with GoogleEarth. But they can be modified for other file formats with study of the scripts and GPSBabel documentation, For help try AppleScript forums or GPSBabel discussion groups.

The scripts are shown below or the three scripts can be downloaded here.

The Script Editor application is in your Applications folder in the AppleScript folder or some similar location depending on your OS X version.

Open Script Editor and put the lines between "--begin script" and "--end script" for each script in a new script file. Save each script to a convenient location such as ~/Library/Scripts/. If you don’t have the Script Menu activated launch Applications > AppleScript > AppleScript Utility and click on "Show Script Menu in menu bar" which will put a script symbol in the Menu Bar allowing quick access to the scripts. The following works for Tiger. Earlier versions of Mac OS X used a different procedure for activating the Script Menu; I don't know when this utility was first included.

Since the two scripts that create files have automatic file naming, they check for the existence of a file with that name. A message pops up telling the user that. Two options are offered: 1) OK (go ahead and overwrite), and 2) Cancel. Rather than implement options in the script for this assumed to be rare situation, you either need to select another folder for the file or rename the file with the same name.

GPS Downloading Script

The first script is for downloading from your Garmin GPS hooked up via a serial cable. It would need to be modified to use with a modern USB native GPS.


--begin script
--This script downloads from your Garmin using the DarwinPorts installed version of gpsbabel. 
-- Options can be used by uncommenting the needed options or changing as needed.
-- Also a line for using the MacGPSBabel version--but you'd probably be using MacGPSBabel for this unless you need some special filter that isn't implemented
--Based on the script by Robert Nixon, http://www.gpsbabel.org/os/OSX_oneclick.html

-- This is set up for gpsbabel installed using DarwinPorts. See <http://www.gpsbabel.org/osnotes.html> for details
-- This script is not being actively supported, but you may get some help from the gpsbabel discussion group: <http://www.gpsbabel.org/lists.html>

set gpsBabelLocation to "/opt/local/var/db/dports/software/gpsbabel/1.2.7_0/opt/local/bin/" -- DarwinPorts Default for Tiger, Feb. 2006
--set gpsBabelLocation to "/Applications/MacGPSBabel/MacGPSBabel/Contents/MacOS/MacGPSBabel" -- Normal MacGPSBabel location
--set gpsBabelLocation to ""

set inputTYPE to " -t -w -i garmin" -- for downloading from Garmin

set outputTYPE to "gpx" --set the output file type to whatever you want


set inputPATH to " /dev/cu.USA19H531P1.1 " -- Recent Keyspan. See Documentation for variations

-- from iView MediaPro
set theDate to (current date)
set theYear to ((year of theDate) as string)
set theMonth to my getPaddedString(my getMonthNumber(theDate))
set theDay to my getPaddedString(day of theDate)
set downloadDate to theYear & "-" & theMonth & "-" & theDay

-- assumes same folder for all files created
set outputFolderalias to choose folder with prompt "Select a folder for downloaded GPS data:"

--file name archive (everything downloaded with no processing)
--set outputFileName to "Garmin." & downloadDate & ".z." & outputTYPE
--set outputFileName to downloadDate & ".GPSBabel." & outputTYPE -- UUUU User default
set outputFileName to downloadDate & ".All.z." & outputTYPE -- DDDD Developer default
set outputPATHArchive to fileCheckName(outputFolderalias, outputFileName)

(*
Filters, etc.
 Position.  See the Documentation for normal usage. http://www.gpsbabel.org/readme.html#d0e1388
Using the Position filter to correct a Garmin error. When the Garmin loses sight of the sattiletes, when it reconects, it puts in poitns with a lat-long equal to the last good point, but current time and elevation (mine has a barameter). Garmin: remove extraneous points recorded when out of range that have same lat and long as last good point (altitude is ignored in this filter)

	 Some formats, like 'garmin' require the 
	 -t flag to work with tracks and 
	 -r to work with routes. 
	 -w is for waypoints, and is the default.
	 *)
-- leave all but one commented
set filterSetting to "" -- use this one if you don't want any Position filtering. You can leave in uncommented and any below will override it.
-- set filterSetting to " -x position " -- Default, distance=0
-- set filterSetting to " -x position,distance=1f " -- 1 foot and leave one
--set filterSetting to " -x position,distance=1m " -- 1 meter and leave one
--set filterSetting to " -x position,distance=1m -x  track,pack,title="ACTIVE LOG" " -- don't know how to pass the double quotes
--set filterSetting to " -x position,distance=1m -x track,pack" -- this probably doesn't work well with saved tracks as they are combined
--set filterSetting to " -x track,pack -x position,distance=1m" -- GIVES A GPSBABEL ERROR--AWAITING FIXES. Pack first for better removal of duplicates. this probably doesn't work well with saved tracks as they are combined.
-- set filterSetting to " -x position,all"  -- remove all duplicates
-- set filterSetting to " -x simplify,count=1000"
-- set filterSetting to " -x simplify,error=1k" -- doesnt' work, but then it's not in the manual
-- set filterSetting to ""

tell application "System Events"
	activate
	do shell script gpsBabelLocation & "gpsbabel " & inputTYPE & " -f " & inputPATH & filterSetting & " -o " & outputTYPE & " -F " & outputPATHArchive
end tell

--Handlers. Some only used once, but I've used them elsewhere
on fileCheckName(outputFolderalias, outputFileName)
	set outputFolder to POSIX path of outputFolderalias
	set outputPATHnoQuote to outputFolder & "/" & outputFileName -- may need to change this if the extension isn't the same as the outputType, The Desktop is a common location
	set outputPATH to quoted form of outputPATHnoQuote -- needed to pass to shell script
	
	--check if file already exist and if we want to overwrite it
	set outputFolderaliasText to outputFolderalias as text
	set outputPATHcolon to outputFolderaliasText & outputFileName
	tell application "Finder" -- trying to get handler to work
		if file outputPATHcolon exists then display dialog "The file " & outputFileName & " already exists. Do you want to overwrite it?"
	end tell
	return outputPATH
end fileCheckName

on getPaddedString(aNumber)
	if aNumber < 10 then
		return "0" & (aNumber as string)
	else
		return (aNumber as string)
	end if
end getPaddedString

on getMonthNumber(aDate)
	if month of aDate is January then
		return 1
	else if month of aDate is February then
		return 2
	else if month of aDate is March then
		return 3
	else if month of aDate is April then
		return 4
	else if month of aDate is May then
		return 5
	else if month of aDate is June then
		return 6
	else if month of aDate is July then
		return 7
	else if month of aDate is August then
		return 8
	else if month of aDate is September then
		return 9
	else if month of aDate is October then
		return 10
	else if month of aDate is November then
		return 11
	else if month of aDate is December then
		return 12
	end if
end getMonthNumber
--end script

GPS Uploading Script

The second script is for uploading waypoints and routes to your Garmin GPS hooked up via a serial cable. It would need to be modified to use with a modern USB native GPS.


--begin script
-- A basic script to upload GPX to Garmin GPS. Read the documentation for your version
-- Limited testing

--Based on the script by Robert Nixon, http://www.gpsbabel.org/os/OSX_oneclick.html

-- This is set up for gpsbabel installed using DarwinPorts. See <http://www.gpsbabel.org/osnotes.html> for details
-- This script is not being actively supported, but you may get some help from the gpsbabel discussion group: <http://www.gpsbabel.org/lists.html>


--start script
tell application "System Events"
	activate
	-- HINT a '~/' is your home folder and of course the double hyphen is a comment. Remove to make active.
	
	-- setting path for gpsbabel. Reset as needed
	set gpsBabelLocation to "/opt/local/var/db/dports/software/gpsbabel/1.2.7_0/opt/local/bin/" -- DarwinPorts Default for Tiger, Feb. 2006
	--set gpsBabelLocation to "/Applications/MacGPSBabel/MacGPSBabel/Contents/MacOS/MacGPSBabel" -- Normal MacGPSBabel location
	--set gpsBabelLocation to ""
	
	set outputTYPE to "garmin" -- for downloading from Garmin
	set outputPATH to " /dev/cu.USA19H531P1.1" -- Recent Keyspan. See Documentation for variations
		
	set inputTYPE to "gpx" -- set the input file type to whatever you want	
	
	--set inputPATH to "~/Desktop/yourfile.gpx" --set the path to where your input file is
	set inputPATH to "~/Desktop/yourfile.gpx" --set the path to where your input file is, Need to put in a display dialog for this ****
	-- comment out the following line if you want to use the line above
	set inputPATHalias to choose file with prompt "Select a GPX file to upload to GPS:"
	-- set inputPATH to quoted form of POSIX path of inputPATHalias  -- got error, but two step below works.
	set inputPATH to POSIX path of inputPATHalias
	set inputPATH to quoted form of inputPATH -- needed to pass to shell script
	
	
	set filterSetting to "" -- use this one if you don't want any Position filtering. You can leave in uncommented and any below will override it.
	-- set filterSetting to " -x position " -- Default, distance=0
	-- set filterSetting to " -x position,distance=1f " -- 1 foot and leave one
	-- set filterSetting to " -x position,distance=1m " -- 1 meter and leave one
	-- set filterSetting to " -x position,all"  -- remove all duplicates
	-- set filterSetting to " -x simplify,count=1000"
	-- set filterSetting to " -x simplify,error=1k" -- doesnt' work, but then it's not in the manual
	-- set filterSetting to ""
	
	--the meat and potatoes, aka that part that gets it done
	-- do shell script gpsBabelLocation & "gpsbabel " & inputTYPE & " -f " & inputPATH & " " & filterSetting & " -o " & outputTYPE & " -F " & outputPATH -- works
	do shell script gpsBabelLocation & "gpsbabel " & filterSetting & " -i " & inputTYPE & " -f " & inputPATH & " -o " & outputTYPE & " -F " & outputPATH
	
end tell
--end script

Format Conversion Script

The third script is For converting from one format to another. Preset to convert from GPX to KML. But it can be modified for other file formats with study of the scripts and GPSBabel documentation


-- begin script
-- For converting from one format to another. Preset to convert from GPX to KML. Tailor as needed. Read the documentation for your version

--Based on the script by Robert Nixon, http://www.gpsbabel.org/os/OSX_oneclick.html

-- This is set up for gpsbabel installed using DarwinPorts. See <http://www.gpsbabel.org/osnotes.html> for details
-- This script is not being actively supported, but you may get some help from the gpsbabel discussion group: <http://www.gpsbabel.org/lists.html>
--start script

-- HINT a '~/' is your home folder and of course the double hyphen is a comment. Remove to make active.

-- setting path for gpsbabel. Reset as needed
set gpsBabelLocation to "/opt/local/var/db/dports/software/gpsbabel/1.2.7_0/opt/local/bin/" -- DarwinPorts Default for Tiger, Feb. 2006
--set gpsBabelLocation to "/Applications/MacGPSBabel/MacGPSBabel/Contents/MacOS/MacGPSBabel" -- Normal MacGPSBabel location
--set gpsBabelLocation to ""

set inputTYPE to "gpx" -- set the input file type to whatever you want	
set outputTYPE to "kml" --set the output file type to whatever you want 

set inputPATH to "~/Desktop/yourfile.gpx" --set the path to where your input file is
-- comment out the following line if you want to use the line above
set inputPATHalias to choose file with prompt "Select a GPX file to convert to KML:"
-- set inputPATH to quoted form of POSIX path of inputPATHalias  -- got error, but two step below works.
set inputPATH to POSIX path of inputPATHalias
set inputPATH to quoted form of inputPATH -- needed to pass to shell script

--set outputPATH to "~/Desktop/yournewfile.wpt" --set the path to where your output file will be
set outputFileName to "Converted." & inputTYPE & ".2." & outputTYPE

set outputFolderalias to choose folder with prompt "Select a folder for converted file:"
set outputPATH to fileCheckName(outputFolderalias, outputFileName)

-- See the Documentation for normal usage. http://www.gpsbabel.org/readme.html#d0e1388
-- Using the Position filter to correct a Garmin error. When the Garmin loses sight of the sattiletes, when it reconects, it puts in poitns with a lat-long equal to the last good point, but current time and elevation (mine has a barometer). Garmin: remove extraneous points recorded when out of range that have same lat and long as last good point (altitude is ignored in this filter). 
set filterSetting to "" -- use this one if you don't want any Position filtering. You can leave in uncommented and any below will override it.
-- set filterSetting to " -x position " -- Default, distance=0
-- set filterSetting to " -x position,distance=1f " -- 1 foot and leave one
-- set filterSetting to " -x position,distance=1m " -- 1 meter and leave one
-- set filterSetting to " -x position,all"  -- remove all duplicates
-- set filterSetting to " -x simplify,count=1000"
-- set filterSetting to " -x simplify,error=1k" -- doesnt' work, but then it's not in the manual
-- set filterSetting to ""

tell application "System Events"
	activate
	do shell script gpsBabelLocation & "gpsbabel " & filterSetting & "-i " & inputTYPE & " -f " & inputPATH & " -o " & outputTYPE & " -F " & outputPATH
end tell

--Handlers. Only used once, but I've used them elsewhere
on fileCheckName(outputFolderalias, outputFileName)
	set outputFolder to POSIX path of outputFolderalias
	set outputPATHnoQuote to outputFolder & "/" & outputFileName -- may need to change this if the extension isn't the same as the outputType, The Desktop is a common location
	set outputPATH to quoted form of outputPATHnoQuote -- needed to pass to shell script
	
	--check if file already exist and if we want to overwrite it
	set outputFolderaliasText to outputFolderalias as text
	set outputPATHcolon to outputFolderaliasText & outputFileName
	tell application "Finder" -- trying to get handler to work
		if file outputPATHcolon exists then display dialog "The file " & outputFileName & " already exists. Do you want to overwrite it?"
	end tell
	return outputPATH
end fileCheckName
--end script