AppleScript, Unicode, and ecto
Posted July 22nd, 2006 @ 04:41pm by Erik J. Barzeski
I've spent a few minutes writing an AppleScript for ecto that I could use tomorrow to ease my live blogging of the final round of the British Open.
I get the time, put it in bold, and then prompt myself for text. I cut off everything but the first three paragraphs and move them down to the extended entry. Unfortunately, when I grab "paragraphs 1 through 5" of the top text, it includes a final carriage return. I don't want it, so I tried to remove the character by using this line:
set body text of currEntry to ( (characters 1 through ( (length of topText) - 1) of topText) as string)
This works beautifully when I run the script within Script Editor. Yet when I run the script from the Script menu, I get text that looks like this:
<&s&t&r&o&n&g&>&1&6&:&3&5&:&<&/&s&t&r&o&n&g&>
You can see the "<strong>" in there - the & are from the Unicode nature of Mac OS X's text system. Switching the call from "body text" to "entry body" has no effect.
How can I fix this script to work from the menu as it works from the entry body?
Posted 22 Jul 2006 at 4:51pm #
I uploaded an image of the script here. Sorry, I didn't have time to convert it to HTML for copy/paste. As text, it is:
set currTime to do shell script "date +%H:%M"
tell application "ecto"
set currEntry to front document
set dialogBox to (display dialog "New Text:" default answer "")
if button returned of dialogBox is "OK" then
set newEntryText to "<strong>" & currTime & " -</strong> " & text returned of dialogBox & return & return
set body text of currEntry to newEntryText & (body text of currEntry)
-- prepare the top text
set topText to (paragraphs 1 through 5 of body text of currEntry) as string
-- prepare the text to move down
set botText to (paragraphs 7 through (count of paragraphs of body text of currEntry) of body text of currEntry) as string
-- this works fine
set extended text of currEntry to (botText & return & return & extended text of currEntry)
-- the problem occurs here, but may have been set up by something earlier???
set entry body of currEntry to ((characters 1 through ((length of topText) - 1) of topText) as string)
else
-- do nothing
end if
end tell
Posted 22 Jul 2006 at 5:46pm #
Not totally sure if this is what's going on, but here's my theory:
1. The "characters 1 through x" part returns a list of characters, i.e. {"", ...}
2. The "as string" coercion is then piecing the list of characters back together with "&" as a text item delimiter, putting a copy of the delimiter between each entry in the list.
Try putting this statement before that line:
set Applescript's text item delimiters to ""
and see if that gets the correct behavior.
Posted 22 Jul 2006 at 6:09pm #
I never changed the text item delimiters, so your suggestion had no effect (and I took it back out).
However, my script is now mysteriously working. So who the heck knows what's going on… I did install the Satimage OSAX (to try out "extract string") but I'm not using it.
Posted 22 Jul 2006 at 8:08pm #
Does this make any difference?
set body text of currEntry to (text 1 through -2 of topText)
Also, you might look into using 'Unicode text' instead of 'string.
Posted 22 Jul 2006 at 8:21pm #
Bruce, that put an & where carriage returns were. I'm thinking that Brian's trick may be the one that works, actually, as it sets them for the script and then stays that way until you re-edit (right?).
Posted 23 Jul 2006 at 3:50pm #
Does 'topText' look right when it is first set?
If Brian's code helped, then you most likely have another script that isn't changing the delimiters back after using them (see below).
set ASTID to AppleScript's text item delimiters
set AppleScript's text item delimiters to {"something"}
-- do whatever
set AppleScript's text item delimiters to ASTID
Posted 23 Jul 2006 at 5:22pm #
I thought I should expand on that last statement… When run via the script menu, changes to AppleScript's text item delimiters will affect other scripts (that are also run via the script menu).
Posted 23 Jul 2006 at 5:42pm #
That's a little odd, but not entirely unexpected I suppose. I could totally see the delimiter being stored in the script itself (like declared properties are), but I suppose the script menu could be reusing some sort of context internally. Is this the system-wide Apple script menu, or a script menu implementation within the app itself?
Posted 23 Jul 2006 at 8:53pm #
It's the system-wide menu. I didn't know changes to the delimiters "stuck" as Bruce tells us they do. That's good to know.