Does anyone know why the following is not getting the first bracket in the result:
set the_text to "
begin Python script
import re #p = re.compile(‘\d+’) #print p.findall(‘the rain 231 in spain 567 falls mainly’)
print re.findall(‘\d+’, ‘the rain 231 in spain 567 falls’)
"
set mod_text to ReplaceText(the_text, return, ASCII character 10)
set q to quoted form of mod_text
set python_return to (do shell script "python -c " & q)
set r to text items of python_return
return {python_return, r, text items of “[‘231’, ‘567’]”}
– set as_value to (run script python_return) – for lists
on ReplaceText(the_text, s_string, r_string)
set new_text to “”
set def_tid to AppleScript’s text item delimiters
set AppleScript’s text item delimiters to {s_string}
try
set text_items to (every text item of the_text)
set AppleScript’s text item delimiters to {r_string}
set new_text to text_items as string
set AppleScript’s text item delimiters to def_tid
on error
set AppleScript’s text item delimiters to def_tid
beep 2
end try
return new_text
end ReplaceText
This line:
set r to text items of python_return
is not catching the first square bracket. I get a result of:
What’s it supposed to return? You’ve not set TIDs beforehand, so it could return anything. If all you want is a list of characters then use ‘characters of…’, not ‘text items of…’.
AppleScript is buggy. Its handling of Unicode text is no exception.
while the dictionary entry for ‘do shell script’ states that the result is plain text:
do shell script: execute a shell script or command using the ‘sh’ shell
do shell script plain text – the command or shell script to execute. Examples are ‘ls’ or ‘/bin/ps -auxwww’
[administrator privileges boolean] – execute the command as the administrator
[password plain text] – use this administrator password to avoid a password dialog
[altering line endings boolean] – change all line endings to Mac-style and trim a trailing one (default true)
[Result: plain text] – the command output
The strange part is, when I get the class of the result:
class of python_return
→ result: string
I get string. I’m wondering if it’s possible that the result could be mixed unicode text and plain text.
It’s a bug in AppleScript’s Unicode text type. The bugs in AppleScript’s Unicode text type are different to the bugs in its string type, which is why you see the faulty result with one but not the other.
Don’t know how you’re getting that. Maybe there was an older version of AppleScript that returned a string instead of Unicode text, although I can’t recall such a thing. On AS 1.9.1, the Standard Additions dictionary wrongly lists the result type as string but it’s actually Unicode text:
class of (do shell script "echo 'hello world'") --> Unicode text