Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Easy AppleScript/TextEdit scripting problem...

Hi All:


I'm new to AppleScript, and we recently upgraded to Lion. We had a simple AppleScript we were using to strip quotation marks out of a .csv file using TextEdit, but the keystroke commands in Lion are different.


Here is the original script, which worked under Leopard/SnowLeopard:


tell application "TextEdit" to activate

tell application "System Events" to tell process "TextEdit"

keystroke "f" using command down

keystroke "\"" & tab

keystroke delete key

click button "Replace All" of window "Find"

keystroke "s" using command down

end tell


This would tell TextEdit to take the current file, replace all the quotation marks with nothing, then save the file again. Now, TextEdit uses a floating menu bar to do Find/Replace. I can get the script to go as far as selecting all the quotation marks:


tell application "TextEdit" to activate

tell application "System Events" to tell process "TextEdit"


keystroke "f" usingcommand down & option down


keystroketabusingshift down


keystroke "\""

?????

end tell


...but I can't figure out how to tell it to click the "All" then "Done" buttons to strip the quotes. Any assistance would be appreciated. Thanks!

20" iMac-OTHER, Mac OS X (10.7.3)

Posted on Feb 22, 2012 9:00 AM

Reply
Question marked as Best reply

Posted on Feb 22, 2012 9:35 AM

That is one of the problems with scripting the user interface - when the layout of the application changes, the script breaks. The following script seems to do what you want:

tell application "TextEdit" to activate
tell application "System Events" to tell process "TextEdit"
keystroke "f" using command down & option down
repeat 3 times -- back up to search field
keystroke tab using shift down
end repeat
keystroke quote
delay 0.5
tell front window
click button "All" of group 2 of scroll area 1
delay 0.5
click button "Done" of scroll area 1
end tell
end tell
3 replies
Question marked as Best reply

Feb 22, 2012 9:35 AM in response to scootermcbuttmonkey

That is one of the problems with scripting the user interface - when the layout of the application changes, the script breaks. The following script seems to do what you want:

tell application "TextEdit" to activate
tell application "System Events" to tell process "TextEdit"
keystroke "f" using command down & option down
repeat 3 times -- back up to search field
keystroke tab using shift down
end repeat
keystroke quote
delay 0.5
tell front window
click button "All" of group 2 of scroll area 1
delay 0.5
click button "Done" of scroll area 1
end tell
end tell

Feb 22, 2012 3:39 PM in response to scootermcbuttmonkey

If all you're doing is stripping quotation marks out of a CSV file, then you are making your life difficult by GUI scripting. TextEdit is scriptable, so something like this should work without breaking across OS revisions:


tell application "TextEdit"

set documentText to text of document 1

set {oldTID, my text item delimiters} to {my text item delimiters, "\""}

set textBits to text items of documentText

set my text item delimiters to ""

set text of document 1 to textBits as text

set my text item delimiters to oldTID

end tell


if you're wanting to do this to multiple documents rather than just one already-open document, you shouldn't even use TE; just open the document in applescript, modify it, and save it back to disk.

Easy AppleScript/TextEdit scripting problem...

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.