2010-09-21 56 views
0

我是AppleScript noob,我真的很想用它做一些很好的事情。我怎樣才能使AppleScript在任何時候運行檢查剪貼板更改?我想要做的是檢查剪貼板,看看它是否是一個特定的值,並使用該剪貼板值進行網絡請求。如何檢查用AppleScript粘貼到剪貼板的值

這是我現在有,它只是獲取在當前剪貼板

get the clipboard 
set the clipboard to "my text" 

任何幫助,將不勝感激的價值。提前致謝。

回答

3

AppleScript沒有辦法「等待剪貼板更改」,因此您必須定期「輪詢」剪貼板。

repeat環與暫停

set oldvalue to missing value 
repeat 
    set newValue to the clipboard 
    if oldvalue is not equal to newValue then 
     try 

      if newValue starts with "http://" then 
       tell application "Safari" to make new document with properties {URL:newValue} 
      end if 

     end try 
     set oldvalue to newValue 
    end if 

    delay 5 

end repeat 

一些可能使用的do shell script "sleep 5"代替delay 5;我從來沒有遇到過delay的問題,但是我從來沒有在像這樣的長時間運行的程序中使用它。

根據用於運行此程序的啓動程序,這樣的腳本可能會「束縛」應用程序並阻止它啓動其他程序(某些啓動程序一次只能運行一個AppleScript程序)。

「保持打開」應用程序與idle處理器

一個更好的選擇是你的程序保存爲「保持打開」應用程序(在另存爲...對話框),並使用idle handler的週期性工作。

property oldvalue : missing value 

on idle 
    local newValue 
    set newValue to the clipboard 
    if oldvalue is not equal to newValue then 
     try 

      if newValue starts with "http://" then 
       tell application "Safari" to make new document with properties {URL:newValue} 
      end if 

     end try 
     set oldvalue to newValue 
    end if 

    return 5 -- run the idle handler again in 5 seconds 

end idle