2011-12-15 42 views
2

私は、テキストファイルを取るAppleScriptを変更しようとすると、テキストファイルの各行に新しいTODOを作成しています:Applescript - 改行で区切られたクリップボードの各行を取得し、各行でアクションを実行する方法?

set myFile to (choose file with prompt "Select a file to read:") 
open for access myFile 

set fileContents to read myFile using delimiter {linefeed} 
close access myFile 

tell application "Things" 

    repeat with currentLine in reverse of fileContents 

     set newToDo to make new to do ¬ 
      with properties {name:currentLine} ¬ 
      at beginning of list "Next" 
     -- perform some other operations using newToDo 

    end repeat 

end tell 

私が代わりたい、単にクリップボードを使用できるようにします代わりにデータソースを作成して、毎回テキストファイルを作成して保存する必要はありません。クリップボードをロードする方法はありますか?また、改行ごとにnewToDo関数を実行しますか?

これはこれまでの試みですが、動作していないようでクリップボード全体を1行にまとめるため、適切な区切り文字が見つからないようです。

try 
    set oldDelims to AppleScript's text item delimiters -- save their current state 
    set AppleScript's text item delimiters to {linefeed} -- declare new delimiters 

    set listContents to get the clipboard 
    set delimitedList to every text item of listContents 

    tell application "Things" 
     repeat with currentTodo in delimitedList 
      set newToDo to make new to do ¬ 
       with properties {name:currentTodo} ¬ 
       at beginning of list "Next" 
      -- perform some other operations using newToDo 
     end repeat 
    end tell 

    set AppleScript's text item delimiters to oldDelims -- restore them 
on error 
    set AppleScript's text item delimiters to oldDelims -- restore them in case something went wrong 
end try 

EDIT:以下の答えの助けを借りて 、コードは非常に簡単です!

set listContents to get the clipboard 
set delimitedList to paragraphs of listContents 

tell application "Things" 
    repeat with currentTodo in delimitedList 
     set newToDo to make new to do ¬ 
      with properties {name:currentTodo} ¬ 
      at beginning of list "Next" 
     -- perform some other operations using newToDo 
    end repeat 
end tell 

答えて

3

AppleScriptは行区切り文字が何であるかを考え出すに非常に優れている「段落」と呼ばれるコマンドがあります。このように試してみてください。このアプローチでは、「テキスト項目デリミタ」の必要はありません。

set listContents to get the clipboard 
set delimitedList to paragraphs of listContents 

あなたのコードを使用したい場合は、これは改行文字を取得するための適切な方法であることに注意してください...

set LF to character id 10 
関連する問題