3

I am trying to modify an applescript that takes a text file, and creates a new todo with each line of the text file:

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

I would like instead, to be able to simply use the clipboard as the data source instead so I don't have to create and save a text file each time, is there any way to load the clipboard, and for each newline, perform the newToDo function?

This is my attempt so far but it doesn't seem to work and puts the entire clipboard into one line, I can't seem to find the proper delimiter.

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: With the help of the answer below, the code is incredibly simple!

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
waffl
  • 5,179
  • 10
  • 73
  • 123

1 Answers1

6

Applescript has a command called "paragraphs" which is very good at figuring out what the line delimiter is. As such give this a try. Note that you won't need the "text item delimiters" stuff with this approach.

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

Note that if you want to use your code, this is the proper way to get a line feed character...

set LF to character id 10
regulus6633
  • 18,848
  • 5
  • 41
  • 49