2017-10-07 46 views
0

它是:如何刪除最後一個空格的文本剪貼板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 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 
+0

@ alexander.polomodov,我相信你的編輯改變了最初的要求,正在_how去掉尾隨空格from_「hello hello hello」,** not **如何從hello \ nhello得到'hello hello hello' \ nhello「! – user3439894

回答

-1

下面是一個比較最後一個字符和空格列表的簡潔方法。比一系列if或者更清潔。

-- 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直通theString -1」,而不是「最後一個字符」,然而,這將忽略尾隨返回字符因此不會爲你工作)

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 quote & theWords & quote 

返回值:

「你好你好你好」 - 用引號


如果你不想報價

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 

返回值:

你好你好你好 - 不帶引號

+1

因爲我在閱讀你的答案後確實得到了這個想法,所以我也只能給你+1。 :) – user3439894

+1

雖然編輯你已經做出了明顯的工作,並允許在OP的例子中有更多的三個字,但IMO已經添加了不必要的代碼行。我不需要首先得到單詞的數量,所有必須改變的地方是'將words從theString的第一個字到第三個字設置爲'將字設置爲從字的第一個字到最後一個字的文本「 。因此,不需要對單詞進行計數編碼,後者在沒有附加編碼的情況下執行相同的操作。 – user3439894

1

假設剪貼板只包含文本一行在你的問題例如「你好,你好,你好」,沒有引號,下面的一個班輪刪除尾隨空間。

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

注:這也刪除任何金額開頭和結尾的空白,是文字文本的剪貼板上的一行中包含的單詞的數量是無限的。

+0

該死的......而我認爲我的回答很棒LOL +1 – wch1zpink

相關問題