删除preceding和尾随字符串空格:AppleScript的 [英] Remove preceding and trailing white spaces in string: AppleScript

查看:501
本文介绍了删除preceding和尾随字符串空格:AppleScript的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图删除preceding和绳子拖着空格但code我使用的是不工作...如果我选择的目录路径不能包含空格它仍然只适用在开始或结束。我在做什么错了?

 上运行{输入参数}
    设置文件路径输入    设置ASTID到的AppleScript的AppleScript的文本项分隔符
    设置的AppleScript的文字分隔符项空间
    设置unwantedSpaces到文件路径的文本项    设置为1
    集合B为(计数unwantedSpaces)    重复而(A< b)和((数项unwantedSpaces的)为0)
        设置为+1
    重复结束    重复而(B> a)及((计数b unwantedSpaces项目)为0)
        设置的B to B - 1
    重复结束    设置strippedText从文本项的文本文字b项文件路径的
    设置的AppleScript的AppleScript的文本项分隔符ASTID    集validFilePaths到{}    与艾琳重复strippedText
        尝试
            设置targetAlias​​为POSIX文件艾琳作为别名
            告诉应用程序发现者透露targetAlias
            validFilePaths到结束设置{}        年底试
    重复结束
    返回validFilePaths
运行结束


解决方案

下面是另一个更简单的方法:

 上运行{输入参数}
    设置文件路径输入文本
    设置Trimmed_filePath做shell脚本回声&放大器;引用文件路径和放大器的形式; | sed的-e'S / ^ [] * //'| sed的-e'S / [] * $ //'
运行结束

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

解决方案

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

这篇关于删除preceding和尾随字符串空格:AppleScript的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆