3

テキスト ファイルを受け取り、テキスト ファイルの各行で新しい 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

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

これはこれまでの私の試みですが、うまくいかないようで、クリップボード全体を 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 

編集:以下の回答の助けを借りて、コードは信じられないほど簡単です!

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
4

1 に答える 1

6

Applescriptには、「段落」と呼ばれるコマンドがあります。これは、行区切り文字が何であるかを理解するのに非常に優れています。そのため、これを試してみてください。このアプローチでは、「テキストアイテムの区切り文字」は必要ないことに注意してください。

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

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

set LF to character id 10
于 2011-12-15T22:42:29.507 に答える