如何在AppleScript中修剪字符串?

时间:2017-07-18 21:03:42

标签: string applescript subdirectory

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

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

我想要做的就是最后修剪:

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

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

3 个答案:

答案 0 :(得分:1)

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

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) & "::")

答案 1 :(得分:0)

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    

答案 2 :(得分:0)

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

用于修剪前缀为~/或后缀为/~的字符串的代码:

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