You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

AppleScript实现选择GPX文件后生成指定格式输出文件名

解决GPX转换脚本的文件名命名问题

要实现originalfilename.2.gpx的输出格式,核心是从选中的文件中提取原始文件名(不含扩展名),再拼接后缀.2.gpx。

关键修改步骤

  1. 提取选中文件的原始文件名:通过AppleScript的Finder应用获取文件的显示名称,拆分掉扩展名部分。
  2. 替换原脚本中固定的outputFileName赋值逻辑,用提取到的原始文件名构造目标文件名。

修改后的完整脚本

-- 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

-- This is set up for gpsbabel installed using DarwinPorts. See gpsbabel docs for details
-- This script is not being actively supported, but you may get some help from the gpsbabel discussion group
--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)

-- 提取原始文件名(不含扩展名)
tell application "Finder"
    set inputFileNameFull to displayed name of inputPATHalias
    -- 拆分文件名和扩展名
    set AppleScript's text item delimiters to "."
    set inputFileNameParts to text items of inputFileNameFull
    -- 取除最后一个部分(扩展名)之外的所有部分,再拼接回文件名
    set inputFileNameBase to ""
    repeat with i from 1 to (count of inputFileNameParts) - 1
        if inputFileNameBase is not "" then set inputFileNameBase to inputFileNameBase & "."
        set inputFileNameBase to inputFileNameBase & item i of inputFileNameParts
    end repeat
    set AppleScript's text item delimiters to ""
end tell

set inputPATH to POSIX path of inputPATHalias
set inputPATH to quoted form of inputPATH -- needed to pass to shell script

-- 构造目标输出文件名:原文件名 + .2.gpx
set outputFileName to inputFileNameBase & ".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.
-- 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

修改说明

  • 新增了通过Finder提取原始文件名的逻辑,支持处理文件名中包含多个.的情况(比如my.trip.gpx会提取为my.trip)
  • 将原固定的outputFileName赋值替换为inputFileNameBase & ".2." & outputTYPE,确保输出格式完全符合要求

内容的提问来源于stack exchange,提问作者johansson

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.11 04:00:29