如何删除最后一个空格文本剪贴板applescript?

如何删除最后一个空格文本剪贴板applescript?,applescript,Applescript,它是: “你好 你好 你好 " 需要 “你好” 如果最后一个字符是要删除的空格 谢谢大家 get the clipboard set the clipboard to (replacement of "1" by "2" for the result) on replacement of oldDelim by newDelim for sourceString set oldTIDs to text item delimiters of AppleScript set tex

它是:

“你好 你好 你好 "

需要

“你好”

如果最后一个字符是要删除的空格 谢谢大家

get the clipboard set the clipboard to (replacement of "1" by "2" for the result) on replacement of oldDelim by newDelim for sourceString set oldTIDs to text item delimiters of AppleScript set text item delimiters of AppleScript to oldDelim set strtoks to text items of sourceString set text item delimiters of AppleScript to newDelim set joinedString to strtoks as string set text item delimiters of AppleScript to oldTIDs joinedString end replacement 获取剪贴板 将剪贴板设置为(将结果的“1”替换为“2”) 关于用newDelim替换sourceString的oldDelim 将oldTIDs设置为AppleScript的文本项分隔符 将AppleScript的文本项分隔符设置为oldDelim 将strtoks设置为sourceString的文本项 将AppleScript的文本项分隔符设置为newDelim 将joinedString设置为strtoks作为字符串 将AppleScript的文本项分隔符设置为oldtid 连接字符串 末端置换
这里有一个简洁的方法,可以将最后一个字符与空白列表进行比较。比一系列“如果”或“如果”更干净

-- set clipboard to "hello hello hello       "
set theString to the clipboard
repeat 99 times
    set c to last character of theString
    if c is in {tab, space, return} then
        set theString to text 1 thru -2 of theString
    else
        exit repeat
    end if
end repeat
return theString

(从技术上讲,抓取“字符串的文本-2到-1”而不是“最后一个字符”会更快,但是这会忽略尾随的返回字符,因此对您不起作用)

这里有一个简洁的方法,可以将最后一个字符与空白列表进行比较。比一系列“如果”或“如果”更干净

-- set clipboard to "hello hello hello       "
set theString to the clipboard
repeat 99 times
    set c to last character of theString
    if c is in {tab, space, return} then
        set theString to text 1 thru -2 of theString
    else
        exit repeat
    end if
end repeat
return theString
(从技术上讲,抓取“字符串的文本-2到-1”而不是“最后一个字符”会更快,但这会忽略尾随的返回字符,所以对您不起作用)

返回:

“你好”--带引号


如果你不想要报价

set the clipboard to "hello hello hello "
set theString to get the clipboard
set theWords to text from first word to last word of theString
set the clipboard to theWords 
返回:

你好,没有引号

返回:

“你好”--带引号


如果你不想要报价

set the clipboard to "hello hello hello "
set theString to get the clipboard
set theWords to text from first word to last word of theString
set the clipboard to theWords 
返回:


hello hello--不带引号假定剪贴板只包含一行文本,如您的问题中的“hello hello”,如果没有引号,则下面的一行将删除尾随空格

set the clipboard to text from first word to last word of (the clipboard as text)

注意:这也会删除任何前导和尾随空格,并且剪贴板上单行文本包含的文本字数不受限制。

假设剪贴板只包含您问题中的单行文本,例如“hello”,不带引号,下面的一行将删除尾随空间

set the clipboard to text from first word to last word of (the clipboard as text)

注意:这也会删除任何前导和尾随空格,并且剪贴板上单行文本包含的文本字数不受限制。

@alexander.polomodov,我相信您的编辑更改了最初的问题,即如何从“hello”中删除尾随空格,不是如何从“hello\nhello\nhello”中获取
hello
@alexander.polomodov,我相信您的编辑更改了最初的问题,即如何从“hello\nhello”中删除尾随空格,而不是如何从“hello\nhello\nhello”中获取
hello\nhello!该死我想我的答案是非常棒的LOL+1Damn。。。我觉得我的答案很精彩,哈哈,因为我在读了你的答案后确实有了这个想法,所以我加上你也是公平的虽然您所做的编辑显然是有效的,并且允许使用OP示例中的三个词以上的内容,但在我看来,您添加了一行不必要的代码。我看不必先计算单词数,只需将字符串中的单词从单词1到单词3设置为文本
将字符串中的单词从第一个单词到最后一个单词设置为文本
。这样一来,就不需要对单词进行编码,而后者在没有添加编码的情况下也会做同样的事情。因为我在阅读了你的答案后才明白了这一点,所以我加1你也是公平的。:)虽然您所做的编辑显然是有效的,并且允许使用OP示例中的三个词以上的内容,但在我看来,您添加了一行不必要的代码。我看不必先计算单词数,只需将字符串中的单词从单词1到单词3设置为文本
将字符串中的单词从第一个单词到最后一个单词设置为文本
。这样,就不需要对单词进行编码,后者也可以在不增加编码的情况下完成同样的工作。