Hi MacScripters,
I’ve been scripting off and on for a long time but that doesn’t mean I am anywhere near being an expert. Far from it. I usually just script enough to get me by for what I need at the moment (and enough to be dangerous).
Right now I am trying to get TextEdit to
¢ open a file,
¢ find all "." (periods) and remove them
¢ select all
¢ change the selected text to [b]UPPERCASE[/b]
I can get as far as the Select All and I am stumped on how to get it to go to the Edit>Transformations>Make Upper Case menu item.
This is what I have so far and I would appreciate any gentle guidance to get this over the goal line:
set newAdds to "test.txt"
tell application "TextEdit"
activate
tell application "TextEdit" to open file ((path to desktop) & newAdds as Unicode text)
tell application "System Events"
tell process "TextEdit"
keystroke "f" using {command down}
keystroke "."
keystroke tab
keystroke ""
tell window "Find"
click button "Replace All"
tell window "Find"
keystroke "w" using {command down}
tell application "System Events"
tell process "TextEdit"
keystroke "a" using {command down}
end tell
end tell
end tell
end tell
end tell
end tell
end tell
You can do this without TextEdit and System Events, using the “tr” shell command and the AS write to file subroutine … if you like i guess it’s pretty fast …
set newAdds to "test.txt"
set myFile to ((path to desktop) & newAdds as Unicode text) as alias
set MyTextFile to read myFile
set AppleScript's text item delimiters to "."
set NewText to every text item of MyTextFile
set AppleScript's text item delimiters to ""
set NewText to NewText as text
set MyUppercaseText to do shell script "tr 'abcdefghijklmnopqrstuvwxyzäöü' 'ABCDEFGHIJKLMNOPQRSTUVWXYZÄÖÜ' <<< " & quoted form of NewText
my write_to_file(MyUppercaseText, myFile, false)
on write_to_file(this_data, target_file, append_data)
try
set the target_file to the target_file as text
set the open_target_file to ¬
open for access file target_file with write permission
if append_data is false then ¬
set eof of the open_target_file to 0
write this_data to the open_target_file starting at eof
close access the open_target_file
return true
on error
try
close access file target_file
end try
return false
end try
end write_to_file
Hope it works
Hi,
as your text file is plain text, TextEdit is not needed
tr -d ‘.’ removes the periods
tr a-z A-Z converts to uppercase
set theFile to quoted form of POSIX path of (path to desktop) & "test.txt"
do shell script "cat " & theFile & " | tr -d '.' | tr a-z A-Z > " & theFile
If you really want to do the trick with TextEdit, you may use this version :
set newAdds to "test.txt"
if my parleAnglais() then
set wName to "Find"
set bName to "Replace All"
else
set wName to "Recherche"
set bName to "Tout remplacer"
end if
tell application "TextEdit"
activate
tell application "TextEdit" to open file ((path to desktop) & newAdds as Unicode text)
tell application "System Events"
tell process "TextEdit"
keystroke "f" using {command down}
keystroke "."
keystroke tab
keystroke ""
tell window wName
click button bName
tell window wName
keystroke "w" using {command down}
tell application "System Events"
tell process "TextEdit"
keystroke "a" using {command down}
end tell
end tell
end tell
end tell
end tell
end tell
end tell
set indexMenu to 5 (* Format *)
set indexMenuItem to 1 (* Font , adjust to your needs *)
set indexSubMenu to 5 (* Outline , adjust to your needs *)
my selectSubMenu("TextEdit", indexMenu, indexMenuItem, indexSubMenu)
--=====
on activateGUIscripting()
tell application "System Events"
if not (UI elements enabled) then set (UI elements enabled) to true (* to be sure than GUI scripting will be active *)
end tell
end activateGUIscripting
--=====
on parleAnglais()
local z
try
tell application "Numbers" to set z to localized string "Cancel"
on error
set z to "Cancel"
end try
return (z is not "Annuler")
end parleAnglais
--=====
(*
my selectSubMenu("Pages",6, 4, 26)
==== Uses GUIscripting ====
*)
on selectSubMenu(theApp, mt, mi, ms)
tell application theApp
activate
tell application "System Events" to tell process theApp to tell menu bar 1 to tell menu bar item mt to tell menu 1 to tell menu item mi to tell menu 1 to click menu item ms
end tell -- application theApp
end selectSubMenu
--=====
(*
useful to get the indexs of the triggered item
my select_SubMenu("Numbers", 6, 14, 3) (* Table > Footer rows > 2 *)
*)
on select_SubMenu(theApp, mt, mi, ms)
tell application theApp
activate
tell application "System Events" to tell process theApp to tell menu bar 1
get name of menu bar items
get name of menu bar item mt
tell menu bar item mt to tell menu 1
get name of menu items
get name of menu item mi
tell menu item mi to tell menu 1
get name of menu items
get name of menu item ms
-- "2"
click menu item ms
end tell -- menu item
end tell -- menu bar item .
end tell -- application System Events
end tell -- application theApp
end select_SubMenu
--=====
CAUTION:
at this time my imac running 10.6.3 is not here so I can’t get the menu item and submenu item indexes required to trigger the menu item UPPERCASE.
So, you will have to adjust the values of the two variables :
indexMenuItem
indexSubMenu
Yvan KOENIG (VALLAURIS, France) mercredi 21 avril 2010 22:49:24
Interesting, StefanK. This ended up wiping the text file clean. Hmmm…
I added this to the original script and it works pretty well. Probably not very elegant but it gets the job done. If you have a better (faster, more efficient) way of doing this, please, post it (I’m especially curious if this can be done without all these “tells”). Thanks.
tell application "System Events"
tell process "TextEdit"
keystroke "a" using {command down}
tell menu bar 1
tell menu bar item 4
tell menu 1
tell menu item 18
tell menu 1
click menu item 1
end tell
end tell
end tell
end tell
end tell
end tell
end tell
Hmm, I’ve tested the script before posting, worked fine