selected text to Palm Desktop memo

Caution: newbie to this forum and to AS.

I am a perl programmer, so my mindset definitely makes it difficult to understand AS. Nonetheless, yesterday I decided to write a small AS to emulate the various text grab services – you know, grab the selection and stick it in TextEdit or grab a selection and stick in Mail, whatever. Except, I want to do this with Palm Desktop, which, being a Carbon app, doesn’t understand services.

I wrote the following script, and the line marked with comment doesnot work.

– grab the current text selection in Safari
tell application “Safari” to set contentsTxt to (do JavaScript “getSelection()” in document 1) as string

– the following line does not work
set titleTxt to text 1 thru 20 of contentsTxt

– interestingly, the above line works if contentsTxt is set to
– something static such as
– set contentsTxt to “some very long string that is going to make the contents of my memo”

– the following works if titleTxt is set to something static such as with
– set titleTxt to “Some title”
– or if contentsTxt is set to something static and then
– set titleTxt to text 1 thru 20 of contentsTxt
– however, since the line above trying to set titleTxt to the
– first 20 chars of contentsTxt fails, the following also fails
tell application “Palm Desktop”
activate
make new memo with properties {title:titleTxt, contents:contentsTxt}
end tell

What am I doing wrong? Perhaps there is a better way to do this, and I would like to know that. However, I would also like to know what I am doing wrong above.

Tia.

What is the error you receive? Your code works for me if there is something selected, and its length is greater than 20 characters… (no valid for form fields)

I really don’t know. I don’t know how to trap the error and where to see it. Just that Safari window blinks and an empty item gets inserted in the PD memo list.

I know the error is because of my attempt to set titleTxt to a truncated version of contentsTxt because if I change either the contentsTxt to a static string, or titleTxt to a static string then the code works fine. In other words –

the following works – use do Javascript to grab the selection and stick it into contentsTxt, but set titleTxt to “Some title”

the following works – set contentsTxt to “Some very long string with lots of words in it” and then set titleTxt to text 1 thru 20 of contentsTxt

however, the following does not work – do Javascript to grab the selection and stick it into contentsTxt and then set titleTxt to text 1 thru 20 of contentsTxt. An empty memo item is inserted which, upon opening, is promptly deleted because memos without any content and without any title are not allowed.

In the Script Editor, switch from the “Result” tab to “Event Log”, and use log statements:

tell application "Safari" to set contentsTxt to (do JavaScript "getSelection()" in document 1) as string

log contentsTxt

This is more or less “debugging”. To trap errors, use this:

try
	set titleTxt to text 1 thru 20 of contentsTxt
on error msg --> will trap error if contentsTxt's length < 20
	--> do whatever, for example:
	display dialog msg
end try

thanks for the tip. I log-ged contentsTxt and got the following in the log window (I had selected your message in Safari) –

tell application “Safari”
do JavaScript “getSelection()” in document 1
"In the Script Editor, switch from the “Result” tab to “Event Log”, and use log statements:

Code:

tell application “Safari” to set contentsTxt to (do JavaScript “getSelection()” in document 1) as string

log contentsTxt

This is more or less “debugging”. To trap errors, use this:

Code:

try
set titleTxt to text 1 thru 20 of contentsTxt
on error msg → will trap error if contentsTxt’s length < 20
→ do whatever, for example:
display dialog msg
end try"
(*In the Script Editor, switch from the “Result” tab to “Event Log”, and use log statements:

Code:

tell application “Safari” to set contentsTxt to (do JavaScript “getSelection()” in document 1) as string

log contentsTxt

This is more or less “debugging”. To trap errors, use this:

Code:

try
set titleTxt to text 1 thru 20 of contentsTxt
on error msg → will trap error if contentsTxt’s length < 20
→ do whatever, for example:
display dialog msg
end try*)
end tell
tell application “Palm Desktop”
activate
make new memo with properties {title:“In the Script Editor”, contents:"In the Script Editor, switch from the “Result” tab to “Event Log”, and use log statements:

Code:

tell application “Safari” to set contentsTxt to (do JavaScript “getSelection()” in document 1) as string

log contentsTxt

This is more or less “debugging”. To trap errors, use this:

Code:

try
set titleTxt to text 1 thru 20 of contentsTxt
on error msg → will trap error if contentsTxt’s length < 20
→ do whatever, for example:
display dialog msg
end try"}
“Palm Desktop got an error: Can’t make some data into the expected type.”


interesting. Seems set-ting titleTxt to text 1 thru 20 of contentsTxt does work as I can see titleTxt is “In the Script Editor”, however, PD is croaking – it says “Palm Desktop got an error: Can’t make some data into the expected type.” Interestingly, PD doesn’t mind if titleTxt is not set dynamically. What gives?

I selected the same text as before, but this time set titleTxt to a static string. Works… no error. For some reason, even though titleTxt is set dynamically, PD croaks.

So, with the code you posted in your first message, it doesn’t work?
Maybe PD doesn’t accept “Unicode text” as input :?:
If so, what if you use the following?

tell application "Safari" to do JavaScript "getSelection()" in document 1
--> quick'n dirty hack to extract plain string from Unicode text
set contentsTxt to «class ktxt» of (result as record)

I selected the following in Safari –

and ran the following script –


tell application "Safari" to set contentsTxt to (do JavaScript "getSelection()" in document 1) as string

set titleTxt to text 1 thru 20 of contentsTxt

tell application "Palm Desktop"
	activate
	
	try
		make new memo with properties {title:titleTxt, contents:contentsTxt}
	on error msg
		display dialog msg
	end try
	
end tell

The following was printed in my log window, and I got the displayed error, and an empty memo was created which got deleted automatically on opening it.

Then, with the same text selected in Safari as above, I modified the script to –


tell application "Safari" to set contentsTxt to (do JavaScript "getSelection()" in document 1) as string

set titleTxt to "junk"

tell application "Palm Desktop"
	activate
	
	try
		make new memo with properties {title:titleTxt, contents:contentsTxt}
	on error msg
		display dialog msg
	end try
	
end tell

I got the following printed in my log window –

I got no errors at all, and a new memo was created successfully.

It doesn’t seem to me that it has anything to do with unicode. Just the combination of selecting text and grabbing it AND creating the title by truncating the selection doesn’t seem to work. This is really perplexing because there is no explanation that I can think of.

I don’t have a copy of PD to play with, but this is a very strange behaviour… Does accept the memo a title with 20 characters? Does accept the memo a title with a dot in a random place? (as “interesting. Seems s”)
The substring does work:

--> my selection: "MacScripter BBS | Applescript Forums"
tell application "Safari" to do JavaScript "getSelection()" in document 1
set x to text 1 thru 20 of result --> "MacScripter BBS | Ap"

So, where do you get the error here, if you run this code from your Script Editor?

tell application "Palm Desktop" 
	make new memo with properties {title:"junk" as unicode text, contents:"contents" as unicode text} 
	make new memo with properties {title:"some text longer than 20 characters", contents:"contents"} 
	make new memo with properties {title:"text.withdots.and no know extensions", contents:"contents"} 
end tell

the following fails with the same message as above


make new memo with properties {title:"junk" as unicode text, contents:"contents" as unicode text} 

the following work fine


   make new memo with properties {title:"some text longer than 20 characters", contents:"contents"} 
   make new memo with properties {title:"text.withdots.and no know extensions", contents:"contents"}

however, look at my code… I am already selecting stuff from Safari “as string”. I tried to coerce any Unicode to as string once again, but failure. Fwiw, PD is available free from Palmone’s website.

Cool. I d/l and installed PD and, as thought, the title doesn’t accept Unicode text as input. This should fix the problem (simply extracting the string info from the Unicode data that Safari passes by default):

-- grab the current text selection in Safari
tell application "Safari" to do JavaScript "getSelection()" in document 1
--> quick'n dirty hack to extract plain string from Unicode text 
set contentsTxt to «class ktxt» of (result as record)

set titleTxt to text 1 thru 20 of contentsTxt
tell application "Palm Desktop" to make new memo with properties {title:titleTxt, contents:contentsTxt}

yes, your suggestion works. Interestingly, wasn’t “as string” supposed to do the same thing? Also, interestingly, why did contentsTxt works (without conversion) as long as titleTxt was not set to text 1 thru 20? Or, why did titleTxt work set to text 1 thru 20 when contentsTxt was hard-coded? It just seems that setting both dynamically was not working. Also, coercing “as string” or “as international text” (that is how it is written up in PD dictionary) didn’t work as well.

Anyway, thanks to your dogged effort, it works now.

Now, my next task is to modify the script to accept the selection from any front most application. But that should be relatively simple.

Thanks, once again.

Unfortunatelly, the text classes in AppleScript are a bit confusing, and most of time they don’t work as expected. “International text” is text with language and script info. Coercing “Unicode text” to “string” is still “Unicode text”. Though PD’s dictionary defines international text for both “title” and “contents”, seems that the “title” doesn’t accept Unicode as input (but “contents” will do).
The substring doesn’t affect the class of the text. It works for any text in AppleScript (except for C and Pascal strings, which are now deprecated terms).
As you’ve seen, every application defines its own. Safari and the Finder will pass you “Unicode text”, PD needs “International text” (or strings), ec. But just now AppleScript is changing quickly and very soon the “Unicode text” class will become the standard. :smiley: