2011-03-24 39 views
1

我正在使用此AppleScript編輯我的剪貼板數據。但也有一些事情我無法弄清楚如何做到:編輯剪貼板文本而不會丟失格式樣式並連續運行腳本

  1. 此腳本將刪除剪貼板中的文本格式整體風格。有沒有辦法保存所有格式?

  2. 我試圖運行這個腳本作爲應用程序(保持打開檢查保存),但它只是一次新的開始後,不編輯任何新的複製文本。我怎樣才能讓這個腳本持續運行?

這裏是我的腳本:

on idle 

    get the clipboard 
    replacement of "SqlConnection" by "OleDbConnection" for the result 
    replacement of "SqlDataAdapter" by "OleDbDataAdapter" for the result 
    set the clipboard to (replacement of "SqlCommand" by "OleDbCommand" for the result) 

end idle 

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 

回答

2

首先,AppleScript的只適用於文本,而不是格式化的文本。所以一旦你把剪貼板放入applescript中,你就失去了所有的格式。你無能爲力。其次,爲了使「空閒」處理程序正常工作,您需要返回時間值,該時間值是處理程序將再次運行的時間。所以就在「結束空閒」語句之前添加「return 10」,這意味着每10秒運行腳本。

2

維護格式的最簡單方法是坦率地使用文本編輯或頁面等樣式的文本編輯器作爲中間階段並在此處進行操作。即在Pages中打開一個新文檔,粘貼文本,執行查找和替換以修改文本,使用GUI腳本選擇全部文件,然後將其複製回剪貼板。

您也可以使用此技術來設置樣式化模板,填寫數據庫信息,然後將其打印或放在剪貼板上。我用了很多。我只是希望你能用數字來做。 (數的查找和替換沒有鍵盤選項)

編輯:這裏有一個快速和骯髒的示例腳本使用網頁作爲中介的地方,查找和替換文本將保持格式化。

tell application "Pages" 
    activate 
    make new document 
end tell 

tell application "System Events" 
    tell process "Pages" 
     -- paste clipboard 
     keystroke "v" using (command down) 
     -- go to top of document 
     key code 126 using (command down) 
     -- open find window 
     keystroke "f" using (command down) 
     -- set word to replace 
     keystroke "original" 
     -- tab to replace field 
     keystroke tab 
     -- set word to replace with 
     keystroke "newword" 
     -- press replace all button 
     click button "Replace All" of tab group 1 of window "Find & Replace" 
     -- close find window 
     keystroke "w" using (command down) 
     -- select all text 
     keystroke "a" using (command down) 
     -- copy back to clipboard 
     keystroke "c" using (command down) 
    end tell 
end tell 
+0

如果我使用的頁面或類似的東西,然後我回到開頭,這意味着用手代替一切(當然我用查找/替換,但我有我自己做的,這也正是爲什麼我開始使用applescript - 來防止這種情況發生。當在拍攝的文本中一次又一次地替換相同的3-4個單詞時,有點奇怪) – TabulaRasa 2011-03-24 23:06:19

+0

不,這正是您不需要做的。您可以編程方式進行查找和替換。只需使用GUI腳本來做到這一點 - 我會看看我今晚晚些時候是否無法展示腳本。 – Clark 2011-03-26 18:14:23

相關問題