Script modification to parse Fantastical inputs into Title and Notes?

Scripters,

Script A below results in Fantastical (http://flexibits.com/fantastical) creating a Calendar.app entry with notes (the url).

Script B below will have Fantastical batch create Calendar.app entries but it does not include script logic and parsing to allow entry of notes. i.e. it could not add the url to the notes section of the calendar entry as Script A does.

Could any of you modify Script B so that it would break each line/paragraph it is fed into two parts: the part before the string “Notes:” and the part after, so the modified Script B could process entries with notes just like Script A?

For example, if Script B would pull this string from a file: “Softball game #1 at Martinez East on 5/15 at 8pm for 60 minutes Notes: http://tinyurl.com/bpc62mh” then it would process it along the lines of Script A with the appended ‘notes’ addition.

Thank you,
Erik


Script A

tell application “Fantastical”
parse sentence “Softball game #1 at Martinez East on 5/15 at 8pm for 60 minutes” notes “http://tinyurl.com/bpc62mh
end tell


Script B

– Created based on code found in these sources:
https://discussions.apple.com/thread/3956557?start=0&tstart=0
https://discussions.apple.com/thread/3678943?start=0&tstart=0

– ask the user for a file:
set theFile to (choose file)
– read the file contents:
set theFileContents to (read theFile)
– break out the lines/paragraphs of text:
set theLines to paragraphs of theFileContents

– now iterate through those lines, one by one:
repeat with eachLine in theLines
tell application “Fantastical”
activate
parse sentence eachLine with add immediately
– delay 1
– tell application “System Events”
– keystroke return using command down --CMD+Enter
– keystroke return using command down --CMD+Enter
delay 2
– end tell
end tell
end repeat


Try this.

Set AppleScript's text item delimiters to "Notes: "
tell application "Fantastical"
parse sentence (text item 1 of eachLine) notes (text item 2 of eachline) with add immediately
end tell
set applescript's text item delimiters to ""

Also, you shouldn’t need the “delay 2” in there. Fantastical should keep up without trouble.

Nik,
Thank you very much! I incorporated that code with a slight modification and it works great. The full new script is below.
All the best,
Erik



-- Created based on code found in these sources:
-- https://discussions.apple.com/thread/3956557?start=0&tstart=0
-- https://discussions.apple.com/thread/3678943?start=0&tstart=0

-- ask the user for a file:
set theFile to (choose file)
-- read the file contents:
set theFileContents to (read theFile)
-- break out the lines/paragraphs of text:
set theLines to paragraphs of theFileContents

set AppleScript's text item delimiters to "Notes:"

-- now iterate through those lines, one by one:
repeat with eachLine in theLines
	
	tell application "Fantastical"
		parse sentence (text item 1 of eachLine) notes (text item 2 of eachLine) with add immediately
	end tell
	-- set AppleScript's text item delimiters to ""
end repeat



could you please explain this line?

end tell
– set AppleScript’s text item delimiters to “”
end repeat

TIA

amelchi

Borrowed from AppleScript Users Guide :

text item delimiters
AppleScript provides the text item delimiters property for use in processing text. This property consists of a list of strings used as delimiters by AppleScript when it coerces a list to text or gets text items from text strings. When getting text items of text, all of the strings are used as separators. When coercing a list to text, the first item is used as a separator.

Note: Prior to OSX Snow Leopard v10.6, AppleScript only used the first delimiter in the list when getting text items.

Because text item delimiters respect considering and ignoring attributes in AppleScript 2.0, delimiters are case-insensitive by default. Formerly, they were always case-sensitive. To enforce the previous behavior, add an explicit considering case statement.

You can get and set the current value of the text item delimiters property. Normally, AppleScript doesn’t use any delimiters. For example, if the text delimiters have not been explicitly changed, the statement

 {"bread", "milk", "butter", 10.45}  as string

returns the following:
“breadmilkbutter10.45”

For printing or display purposes, it is usually preferable to set text item delimiters to something that’s easier to read. For example, the script

 set AppleScript's text item delimiters to {", "}
{"bread", "milk", "butter", 10.45}  as string

returns this result:
“bread, milk, butter, 10.45”

The text item delimiters property can be used to extract individual names from a pathname. For example, the script

set AppleScript's text item delimiters to {":"}
get last text item of "Hard Disk:CD Contents:Release Notes"

returns the result “Release Notes”.

If you change the text item delimiters property in Script Editor, it remains changed until you restore its previous value or until you quit Script Editor and launch it again. If you change text item delimiters in a script application, it remains changed in that application until you restore its previous value or until the script application quits; however, the delimiters are not changed in Script Editor or in other script applications you run.
Scripts commonly use an error handler to reset the text item delimiters property to its former value if an error occurs (for more on dealing with errors, see “AppleScript Error Handling” (page 40)):

set AppleScript's text item delimiters to {":"}
get last text item of "Hard Disk:CD Contents:Release Notes"
set savedDelimiters to AppleScript's text item delimiters
try
    set AppleScript's text item delimiters to {"**"}
    --other script statements...
    --now reset the text item delimiters:
    set AppleScript's text item delimiters to savedDelimiters
on error m number n
    --also reset text item delimiters in case of an error:
    set AppleScript's text item delimiters to savedDelimiters
    --and resignal the error:
    error m number n
end try

Yvan KOENIG running El Capitan 10.11.2 in French (VALLAURIS, France) dimanche 27 décembre 2015 22:21:07

Hi.

Yvan’s given the low-down on what text item delimiters are and what you can do with them.

His assertion that AppleScript doesn’t normally use any delimiters isn’t quite correct, technically. The delimiters are alway set to something. Their default value is a list containing one empty text ” {“”} ” which produces the effect of there being “no delimiter” in situations where delimiters are relevant.

It’s good practice for scripts which change AppleScript’s text item delimiters to restore the previous value after use. There are two schools of thought about how to do this. Some scripters (such as myself) store the delimiters’ current value in a variable before changing them, then restore them from the variable afterwards. Others explicitly set the delimiters to {“”} or “” after use. (A third class of scripter may argue that since they always explicitly set the delimiters before using them, there’s no need to reset them afterwards. But it’s better to script “courteously” in order to avoid causing problems for users who depend on scripts written by several different people.)

In emiskysa’s script in post #3, the delimiter-restoring line .

set AppleScript's text item delimiters to ""

. has been commented out. This is no doubt because it had erroneously been written inside the repeat and was causing problems by resetting the delimiters after the first iteration instead of the last. The correct fix, of course, would have been to move it to after the repeat:

set AppleScript's text item delimiters to "Notes:"

-- now iterate through those lines, one by one:
¨repeat with eachLine in theLines
   
   tell application "Fantastical"
       parse sentence (text item 1 of eachLine) notes (text item 2 of eachLine) with add immediately
   end tell
end repeat

set AppleScript's text item delimiters to ""

¨

That’s not quite the case any more. Each document has its own text item delimiters, and they are reset every time you recompile.

The worst places people get caught out is in things like script panels or menus in scriptable apps, like in InDesign or FastScripts. In such cases, all scripts share the same text item delimiters.