Applescript:Go to iCal and copy first word of Notefield of active ToDo

Hi - first of all a happy new year to all readers here on the board,

just trying to get the following applescript.

I want to activate out of a todo in iCal that I can copy the first or last word of the note-field.

found the following script for copying…now how can I guide to the noefield of the active todo in iCal (can be different calendars) and copy only the first or last word - in my case it would be a number / does it has to be the same digits like 001 or 145 or does 1 and 145 also work?

tell application “iCal” to activate
delay 3
tell application “System Events”
tell process “iCal”
keystroke return
keystroke “c” with command down
keystroke return
end tell
end tell

Best regards -Christoph

Hi Christoph,

iCal can do this directly without UI scripting.
This copies e.g. the last word of the description of first todo of calendar 1

tell application "iCal"
	tell calendar 1
		set a to last word of (get description of todo 1)
	end tell
end tell

Hi Stefan,

thank you very much - your script does work perfect - I only want to have “the last word” now which I copied with your script in the clipboard of the finder that I can transfer it to another application… what do I have to do for that?

Christoph

I got it myself… proud… :wink:

tell application “iCal”
tell calendar 1
set a to last word of (get description of todo 1)
end tell
end tell
tell application “Finder”
set the clipboard to the result as text
end tell

grrr…don´t know why buy after that this script was working at first I now get:

žiCal" hat einen Fehler erhalten: NSReceiverEvaluationScriptError: 4

Which means iCal had received an error: NSReceiverEvaluationScriptError: 4

and the word “discription” in the script is selected!?

Any ideas what that could mean?

Best regards - Christoph

I found this at apple.com:

“There has been a change in Cocoa Scripting such that when you incorrectly specify an object in your script, an error is now returned, whereas before it simply failed silently. An example of how this error can occur would be title of button “foo” of window “main” when there isn’t a button named “foo” in the window. The error is reported as “NSReceiverEvaluationScriptError: 4”.
The object or objects specified by the direct parameter to a command could not be found.”

But it was working for the first time and is “description” wrong? I think the field exist and it is also called like that…

Christoph

Hallo Christoph,

the error message means, that this todo doesn’t exist.
You can avoid it either with a try block or with retrieving the existence of this todo.
btw: the clipboard doesn’t belong to the finder, it’s part of the Standard Scripting Additions

tell application "iCal"
	tell calendar 1
		if exists todo 1 then set the clipboard to last word of (get description of todo 1)
	end tell
end tell

Hi Stefan,

thanks again for your help… when I activate your new script and then trying to paste in order to see whats in the clipboard it paste the hole script.

Before activating the script I selected a specific todo

Christoph

Edit - that was not right, I thought it would give me the script - but it gives me nothing, the whole script was already in the clipboard… that was the reason - but the script gives me nothing right now.

Don´t understand that because at first it was working for a few times. And now either nothing or with the old script the Error: 4

to select a specified todo in the todo window is much more complicated,
because iCal has no selection property like iTunes or iPhoto has.
But you can simulate it with UI scripting, it works only properly, if each todo has a unique summary
and the todo window is active

tell application "iCal" to activate
tell application "System Events"
	tell process "iCal"
		tell window 1
			try
				set a to some row of outline 1 of scroll area 1 whose selected is true
				set selectedToDo to value of text field 1 of a
			on error
				display dialog "No todo is selected" buttons {"OK"} default button 1 with icon stop
				return
			end try
		end tell
	end tell
end tell
set flag to false
tell application "iCal"
	repeat with c in calendars
		if (count todos of c) > 0 then
			repeat with t in todos of c
				if summary of t is selectedToDo then
					set the clipboard to last word of (get description of t)
					set flag to true
					exit repeat
				end if
			end repeat
		end if
		if flag then exit repeat
	end repeat
end tell

Hi Stefan,

thank you so much for helping me out - your script works great! I do only have one problem that it takes the word into the clipboard, but then, when I want to go to Filemaker to enter this word into a searchfield I can´t get something into it. Somehow it work in other application to paste the clipboard - but in Filemaker I only get a errorsound. Don´t know why that is.

Right now I tell in the script that it should go to filemaker and run a filemaker script where I tell it do go to a specific field and do a search afer pasting

But there is nothing to paste right now…

Any ideas? Thanks in advance - Christoph

One more thing… when I copy a word before activating the script, then run the script and go to a application like textedit - it takes 2or 3 tries before I get the word that was filtered by the script - before that I still get the word that I copied before activating the script.

probably it’s not necessary to copy & paste via the clipboard.
Both TextEdit and FileMaker are also scriptable.
Assign the result to a variable and pass it to the application.
I don’t use FileMaker but may be it helps to read the dictionary

Yes - maybe something like this?

tell application “iCal” to activate
tell application “System Events”
tell process “iCal”
tell window 1
try
set a to some row of outline 1 of scroll area 1 whose selected is true
set selectedToDo to value of text field 1 of a
on error
display dialog “No todo is selected” buttons {“OK”} default button 1 with icon stop
return
end try
end tell
end tell
end tell
set flag to false
tell application “iCal”
repeat with c in calendars
if (count todos of c) > 0 then
repeat with t in todos of c
if summary of t is selectedToDo then
set the clipboard to last word of (get description of t)
set flag to true
exit repeat
end if
end repeat
end if
if flag then exit repeat
end repeat
end tell
tell application “FileMaker Pro Advanced”
activate
tell database “Cvkaiser”
tell table “cvkaiser”
create request
set cell “ID” of request 1 to (clipboard)
find
end tell
end tell
end tell

I get the message with this one selected that the object doesn´t exist in Filemaker - but I have a field called ID and it is in the layout:

set cell “ID” of request 1 to (clipboard)

When I change it to:

tell application “FileMaker Pro Advanced”
activate
create request
set cell “ID” of request 1 to (clipboard)
find
end tell

I´m getting the message that (with highlighted) set cell “ID” of request 1 to (clipboard) - žFileMaker Pro Advanced" hat einen Fehler erhalten: Objekt oder Eigenschaft hat den falschen Typ. The object or quality has the wrong typ.

try it without clipboard

.... set theSelection to last word of (get description of t) .... set cell "ID" of request 1 to theSelection
the correct syntax to get the content of the clipboard is

the clipboard

one of the rare cases, which the keyword “the” must be used in

Perfect! Thank you very much Stefan - can´t thank you enough - couldn´t have done that on my own because I actually don´t know much about Applescript.
Wonderful - Best regards and have a great day! Christoph