如何在 AppleScript 中 trim 字符串?

How to trim strings in AppleScript?

我正在尝试 trim AppleScript 中的返回字符串。这是我打包的应用程序中脚本的目录。我的想法是让这个应用程序移动,因为旧应用程序依赖静态目录 运行 另一个应用程序 Spore_origin.app 中的脚本,包含在它的应用程序包中。这是代码:

to run
    set current_path to POSIX path of (path to me) as string    
    tell application "Terminal"
        do script current_path & "/Spore_origin.app/Contents/MacOS/cider_noui"
    end tell    
end run

我想做的是trim最后:

/Users/mypetnamedsteve/Stuff/Spore.app/Contents/Resources/Scripts/main.scpt

突出显示的位需要 trimming,但我不知道如何。帮助!!!!

只需获取父文件夹即可。无需使用字符串操作

set current_path to (POSIX path of (container of (path to me))) as string

取决于路径的类型 - 如果您获得 hfs 路径,这也将有效

set current_path to POSIX path of ((path to me as text) & "::")
set myPath to POSIX path of (path to me)
tell application "Terminal"
    do script ("`dirname " & quoted form of myPath & "`/Spore_origin.app/Contents/MacOS/cider_noui")
end tell    

实际上trimming string (因此适用于需要trim字符串的其他情况, 问题的标题)

用于 trim 具有 ~/ 前缀或 /~ 后缀的字符串的代码:

set example_string to "~/Wow/~"
-- prefix
if example_string starts with "~/" then
    set music_folder to characters (count of "~/") thru -1 of example_string as string
    -- instead of (count of "~/") you could just use 2
end if
-- suffix
if example_string ends with "/" then
    set example_string to characters -1 - (count of "/~") thru -1 of example_string as string
    -- instead of -1 - (count of "~/") you could just use -3
end if