Remove preceding and trailing white spaces in string: AppleScript

ぐ巨炮叔叔 提交于 2019-12-13 14:22:34

问题


I'm trying to remove preceding and trailing white spaces in a string but the code I'm using isn't working... It still only works if I select a directory path without white spaces at the beginning or the end. What am I doing wrong?

on run {input, parameters}
    set filePath to input

    set ASTID to AppleScript's AppleScript's text item delimiters
    set AppleScript's text item delimiters to space
    set unwantedSpaces to filePath's text items

    set a to 1
    set b to (count unwantedSpaces)

    repeat while (a < b) and ((count item a of unwantedSpaces) is 0)
        set a to a + 1
    end repeat

    repeat while (b > a) and ((count item b of unwantedSpaces) is 0)
        set b to b - 1
    end repeat

    set strippedText to text from text item a to text item b of filePath
    set AppleScript's AppleScript's text item delimiters to ASTID

    set validFilePaths to {}

    repeat with aLine in strippedText
        try
            set targetAlias to POSIX file aLine as alias
            tell application "Finder" to reveal targetAlias
            set end of validFilePaths to {}

        end try
    end repeat
    return validFilePaths
end run

回答1:


Similar to double_j's answer, but for others who land here...

I often use a simple sub-routine, with echo " str1 " | xargs shell script:

on trim(theText)
  return (do shell script "echo \"" & theText & "\" | xargs")
end trim

OR because JavaScript is my typical language I sometimes think in JS & do this (even though it's terribly inefficient, so I wouldn't use this for trim but it can be a quick win for more complex things when being efficient isn't critical):

on trim(theText)
  return (do shell script "osascript -l JavaScript -e '\"" & theText & "\".trim()'")
end trim

(Perhaps there's a real way to execute inline JS in AppleScript, but I'm not sure......yet)




回答2:


Here's another simpler approach:

on run {input, parameters}
    set filePath to input as text
    set Trimmed_filePath to do shell script "echo " & quoted form of filePath & " | sed -e 's/^[ ]*//' | sed -e 's/[ ]*$//'"
end run



回答3:


If you are looking to trim spaces from a string, the script below should do it. However, your variable name filePath leads me to believe that your input is a file path ;) . If this is the case, the end of the path won't be trimmed because of the name extension and the beginning of the path won't be trimmed unless your drive's name starts with a space. If you are looking to trim spaces in a file name, you will have to adjust the script.

on run {input, parameters}
    set filePath to (input as text)
    return my trim(filePath, true)
end run

-- From Nigel Garvey's CSV-to-list converter
-- http://macscripter.net/viewtopic.php?pid=125444#p125444
on trim(txt, trimming)
    if (trimming) then
        repeat with i from 1 to (count txt) - 1
            if (txt begins with space) then
                set txt to text 2 thru -1 of txt
            else
                exit repeat
            end if
        end repeat
        repeat with i from 1 to (count txt) - 1
            if (txt ends with space) then
                set txt to text 1 thru -2 of txt
            else
                exit repeat
            end if
        end repeat
        if (txt is space) then set txt to ""
    end if

    return txt
end trim


来源:https://stackoverflow.com/questions/27304792/remove-preceding-and-trailing-white-spaces-in-string-applescript

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!