删除尾随空格时Applescript代码中断

时间:2019-07-09 19:15:16

标签: string applescript

应该是简单的代码来删除字符串中的最后一个字符,但似乎没有明显的原因(无论如何对我来说)。

我正在尝试创建一个简单的脚本来解决同事在文件名和扩展名之间留有多余空格的OneDrive文件同步问题。

很长一段时间后回到AppleScript,似乎已经忘记了一切,只需要了解为什么简单的事情似乎会让我感到困惑……对我温柔。

on stripSpaces(thisText)
    local newText
    local pos
    local tempText

    set newText to thisText

    set tempText to ""

    repeat while tempText ≠ newText
        set tempText to newText

        --remove spaces before extension name
        set pos to the offset of ". " in newText

        if pos > 0 then
            set newText to ((characters 1 thru pos of newText) as text) & (characters (pos + 2) thru end of newText) as text
        end if

        --remove spaces before extension
        set pos to the offset of " ." in newText

        if pos > 0 then
            set newText to ((characters 1 thru (pos - 1) of newText) as text) & (characters (pos + 1) thru end of newText) as text
        end if

        --remove leading spaces
        if character 1 of newText = " " then
            set newText to characters 2 thru end of newText as text
        end if

        ---BROKEN SECTION

        --remove trailing spaces
        if (last character of newText) = " " then
            set newText to (characters 1 thru (end of newText) - 1) as text
        end if
    end repeat

    return newText
end stripSpaces

log stripSpaces("   spa     .          nish  . txt  ")

错误“无法结束\“ spa。 nish。文本文件 \”。”从“ spa.nish.txt”的最后插入点开始的编号-1728

3 个答案:

答案 0 :(得分:1)

我来晚了一点。 Ted Wrigley盯着球,很正确地注意到,最text item delimiters是最干净,最有效的解决方案。在我看来,我感谢他提供了迄今为止提供的最合适的答案,从本质上讲,这只是后续操作,以说明一个人可以使用更紧凑的两行处理程序来达到相同的结果: / p>

to removeAllWhitespace from input as text
    set text item delimiters to {pi, space, tab, linefeed, return}
    return the input's text items as text
end removeAllWhitespace

我也很自由地清除了space之外的所有(传统)空格字符,即tab字符和换行符。如果您愿意,您可能可以推断出如何删除这些选项。

调用处理程序:

removeAllWhitespace from "   spa     .          nish  . txt  "

其输出: "spa.nish.txt"


嗯?皮...?

简而言之::随机选择不是文本且不太可能出现在文件名中的任何内容。如有疑问,请使用pi ^ pi

详细信息: 不要被pi所困扰:上面定界符列表中使用pi就数量本身而言,没有任何意义,它唯一的功能是作为 非字符串 实体,该实体允许从列表中删除所有定界符文件名,它将包括任何以字符串形式出现的自身,即"3.14159265359"。如果此字符序列未出现在文件名中,则pi可能是充当此“虚拟分隔符”的一个不错的选择。否则,关键是将pi换成任何其他 非字符串 值。说... pi + 1?我经常使用nullmissing value或随机生成的数字。但是,如果您发现自己不介意生成数字或自己想一个,只需使用pi ^ pi

答案 1 :(得分:0)

使用s.replaceAll( '[^a-zA-Z]+', ' ' ).trim().split( ' ' ) 获取文本范围要容易得多,其中-1是最后一个字符。这也避免了所有text 1 thru -1 of newText的强制。

as text

在AppleScriptObjC的帮助下,整个处理程序可以减少到两行

on stripSpaces(thisText)
    local newText
    local pos
    local tempText

    set newText to thisText

    set tempText to ""

    repeat while tempText ≠ newText
        set tempText to newText

        --remove spaces before extension name
        set pos to the offset of ". " in newText

        if pos > 0 then
            set newText to text 1 thru pos of newText & text (pos + 2) thru -1 of newText
        end if

        --remove spaces before extension
        set pos to the offset of " ." in newText

        if pos > 0 then
            set newText to text 1 thru (pos - 1) of newText & text (pos + 1) thru -1 of newText
        end if

        --remove leading spaces
        if first character of newText = space then
            set newText to text 2 thru -1 of newText
        end if

        --remove trailing spaces
        if last character of newText = space then
            set newText to text 1 thru -2 of newText
        end if
    end repeat

    return newText
end stripSpaces

log stripSpaces("   spa     .          nish  . txt  ")

答案 2 :(得分:0)

您问题的直接答案是,在这种情况下,您不应使用“ end”,而应使用“ -1”指示最后一个字符,使用“ -2”指示倒数第二个字符,等等。结果最终看起来像这样:

from google.cloud import talent_v4beta1

client = talent_v4beta1.JobServiceClient()
# initialize jobs and parent variables
operation = client.batch_update_jobs(parent, jobs) # this line is failing

但是,如果您的目标只是删除空格,则可以使用文本项定界符来更有效地做到这一点:

on stripSpaces(thisText)
    local newText
    local pos
    local tempText

    set newText to thisText

    set tempText to ""

    repeat while tempText ≠ newText
        set tempText to newText

        --remove spaces before extension name
        set pos to the offset of ". " in newText

        if pos > 0 then
            set newText to ((characters 1 thru pos of newText) as text) & (characters (pos + 2) thru -1 of newText) as text
        end if

        --remove spaces before extension
        set pos to the offset of " ." in newText

        if pos > 0 then
            set newText to ((characters 1 thru (pos - 1) of newText) as text) & (characters (pos + 1) thru -1 of newText) as text
        end if

        --remove leading spaces
        if character 1 of newText = " " then
            set newText to characters 2 thru -1 of newText as text
        end if

        ---BROKEN SECTION

        --remove trailing spaces
        if (last character of newText) = " " then
            set newText to (characters 1 thru -2 of newText) as text
        end if
    end repeat

    return newText
end stripSpaces

log stripSpaces("   spa     .          nish  . txt  ")