Search code examples
macosapplescript

Get filename without extension after choose file with prompt


I'm trying to write a small gpx file conversion script. The conversion part is done but I can't fix the naming of the converted file. I want the filename of the converted file like this:

originalfilename.2.gpx

This is the code I have so far:

-- 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/GPSBabelFE.app/Contents/MacOS/" -- Normal MacGPSBabel location
--set gpsBabelLocation to ""

set inputTYPE to "gpx" -- set the input file type to whatever you want  
set outputTYPE to "gpx" --set the output file type to whatever you want
set strPath to POSIX file "/Users/someusername/downloads/" --set the default path for file input dialog

--set inputPATH to "~/Desktop/route.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 " & outputTYPE & ":" default location strPath)

-- set inputPATH to quoted form of POSIX path of inputPATHalias  -- got error, but two step below works.

set inputfilename to (inputPATHalias as text)


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 outputFileName to outputFileName & ".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 & " -x transform,trk=rte " & " -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


I'm unable to find Applescript code that translates the filename from a prompt into a variable.


Solution

  • I recommend to not use the Finder.app at all for this task. Especially, checking file existence with the Posix file coercion inside the tell block is a big mistake. The Finder is also not needed to get the file basename. For this task, with the help of other experienced users, I developed a high-speed handler, provided here.

    In general, I recommend using the Finder only when absolutely necessary, because it is often busy in the background with other time-consuming tasks.

    set gpsBabelLocation to "/Applications/GPSBabelFE.app/Contents/MacOS/" -- Normal MacGPSBabel location
    
    set inputType to "gpx" -- the input file type extension  
    set outputType to "gpx" -- the output file type extension
    set defaultPath to (path to downloads folder)
    set inputPath to (choose file of type inputType with prompt "Select a " & inputType & " file to convert to " & outputType & ":" default location defaultPath) -- remove 'of type inputType' to select any file
    set outputFileName to getBaseName(POSIX path of inputPath) & ".2." & outputType -- the converted name
    
    set outputFolder to choose folder with prompt "Select a folder for the converted file:"
    set outputPath to fileCheckName(outputFolder, outputFileName)
    
    set filterSetting to ""
    set inputPath to quoted form of (POSIX path of inputPath)
    set command to gpsBabelLocation & "gpsbabel " & filterSetting & "-i " & inputType & " -f " & inputPath & " -x transform,trk=rte " & " -o " & outputType & " -F " & outputPath
    log command
    if gpsBabelLocation is not "" then
        do shell script command
        log result
    end if
    
    
    on getBaseName(posixPath)
        set {ATID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, "/"}
        if (posixPath ends with "/") then
            set baseName to text item -2 of posixPath
        else
            set baseName to text item -1 of posixPath
        end if
        if (baseName contains ".") then
            set AppleScript's text item delimiters to "."
            set baseName to text 1 thru text item -2 of baseName
        end if
        set AppleScript's text item delimiters to ATID
        return baseName
    end getBaseName
    
    
    on fileCheckName(outputFolder, outputFileName) -- check if file exists
        try
            alias (outputFolder & outputFileName)
            display alert quoted form of outputFileName & " already exists. Do you want to replace it?" message "A file or folder with the same name already exists at " & quoted form of POSIX path of outputFolder & ". Replacing it will overwrite its current contents." buttons {"OK", "Cancel"}
            if button returned of the result is "Cancel" then error number -128 -- alert doesn't automatically cancel
        end try
        return quoted form of ((POSIX path of outputFolder) & outputFileName) -- quote for the shell script
    end fileCheckName