Last active
December 20, 2015 16:18
-
-
Save juandesant/6160117 to your computer and use it in GitHub Desktop.
Drag-and-drop script to download standalone KML files that do not depend of a network connection, and that can be used with things like online converters, or iOS apps. See article describing the script (in Spanish) here:
http://www.entremaqueros.com/bitacoras/memoria/?p=2455
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
on downloadUrl(theUrl, fileName) | |
set curlCmd to "curl -s -L -o " & fileName & " '" & theUrl & "' " | |
--display dialog curlCmd | |
do shell script curlCmd | |
end downloadUrl | |
on run {input, parameters} | |
set theUrls to {} | |
repeat with kmlFile in input | |
set kmlFileString to kmlFile as string | |
set theUrl to "" | |
tell application "System Events" | |
try | |
set kmlDataContent to the contents of XML file kmlFileString | |
try | |
tell kmlDataContent | |
tell XML element "kml" -- the klm level | |
tell XML element "Document" | |
tell XML element "NetworkLink" | |
try | |
tell XML element "Link" | |
set theUrl to (value of XML element "href") | |
end tell | |
on error | |
-- On ocassions, it can have a Url insted of a Link | |
try | |
tell XML element "Url" | |
set theUrl to (value of XML element "href") | |
end tell | |
end try | |
end try | |
end tell | |
end tell | |
end tell | |
end tell | |
on error | |
display dialog "The file is not a well-formed XML KML file with NetworkLink tags." | |
end try | |
on error | |
display dialog "The selected file is not an XML file." | |
end try | |
end tell | |
if theUrl > "" then -- we only intend to save if there is a URL from which we can retrieve something | |
set kmlFileName to the name of the (info for kmlFile) | |
--Extension with the starting dot | |
set kmlFileExt to ("." & the name extension of the (info for kmlFile)) | |
set kmlFilePath to (the POSIX path of kmlFile) as string | |
set extensionPosition to the offset of kmlFileExt in kmlFileName | |
-- Minus 1, so that we don't get the dot | |
set kmlBaseFileName to ((characters 1 thru (extensionPosition - 1) of kmlFileName) as string) | |
set newFileName to kmlBaseFileName & "-complete" & kmlFileExt | |
set outKmlFile to quoted form of POSIX path of ¬ | |
(choose file name with prompt ¬ | |
"Select where to save:" default name newFileName ¬ | |
default location (path to downloads folder)) | |
try | |
my downloadUrl(theUrl, outKmlFile) | |
-- Only update theUrls if we can download; | |
-- if we reach here, there was no exception raised | |
set theUrls to theUrls & theUrl | |
on error | |
display alert "Could not download " & theUrl | |
end try | |
end if | |
end repeat | |
return theUrls | |
end run |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
on downloadUrl(theUrl, fileName) | |
set curlCmd to "curl -s -L -o " & fileName & " '" & theUrl & "' " | |
do shell script curlCmd | |
end downloadUrl | |
on open finderItems | |
set theUrls to {} | |
repeat with kmlFile in finderItems | |
set kmlFileString to kmlFile as string | |
set theUrl to "" | |
tell application "System Events" | |
try | |
set kmlDataContent to the contents of XML file kmlFileString | |
try | |
tell kmlDataContent | |
tell XML element "kml" -- the klm level | |
tell XML element "Document" | |
tell XML element "NetworkLink" | |
try | |
tell XML element "Link" | |
set theUrl to (value of XML element "href") | |
end tell | |
on error | |
-- On ocassions, it can have a Url insted of a Link | |
try | |
tell XML element "Url" | |
set theUrl to (value of XML element "href") | |
end tell | |
end try | |
end try | |
end tell | |
end tell | |
end tell | |
end tell | |
on error | |
display dialog "The file is not a well-formed XML KML file with NetworkLink tags." | |
end try | |
on error | |
display dialog "The selected file is not an XML file." | |
end try | |
end tell | |
if theUrl > "" then -- we only intend to save if there is something to save | |
set kmlFileName to the name of the (info for kmlFile) | |
--Extension with the starting dot | |
set kmlFileExt to ("." & the name extension of the (info for kmlFile)) | |
set kmlFilePath to (the POSIX path of kmlFile) as string | |
set p to the offset of kmlFileExt in kmlFileName | |
-- Minus 1, so that we don't get the dot | |
set kmlBaseFileName to ((characters 1 thru (p - 1) of kmlFileName) as string) | |
set newFileName to kmlBaseFileName & "-complete" & kmlFileExt | |
set outKmlFile to quoted form of POSIX path of ¬ | |
(choose file name with prompt ¬ | |
"Select where to save:" default name newFileName ¬ | |
default location (path to downloads folder)) | |
try | |
my downloadUrl(theUrl, outKmlFile) | |
-- Only update theUrls if we can download; | |
-- if we reach here, there was no exception raised | |
set theUrls to theUrls & theUrl | |
on error | |
display alert "Could not download " & theUrl | |
end try | |
end if | |
end repeat | |
return theUrls | |
display dialog (theUrls as string) | |
end open |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The Download proper KML file.scpt can be exported as an Application from Mac OS X's Script Editor, for drag and drop conversion. The lower file (Download referenced KML.scpt) is for use in an AppleScript box inside an Automator workflow.