newbie question: textEdit

I’ve never used appleScript before, but I know php, perl, python etc…

I would like to write some macros for textEdit. the first: I select several lines of text, press a button & the text is commented out thusly:

this

text

is now commented out

& another macro to uncomment it. How do I do that? How do I call an appleScript from textEdit, & what would the code look like? any advice/leads would be super appreciated – maybe a good tutorial on setting up macro-like scripts for use w/textEdit.

for some reason I’m finding it very hard to find info on applescript “basics.”

thanks folks,
Raphe

Model: macbook
Browser: Firefox 2.0.0.12
Operating System: Mac OS X (10.4)

There are many tutorials on this website. Go to the front page http://macscripter.net/ and then look at the sections in the upper right part of the front page. The unscripted section has many tutorials as well as some of the other section. There’s some tutorials called something like “applescript for beginners” that you can find in the unscripted section.

This applescript will do what you want. If the sentences start with # then it will remove it, and if they don’t start with # then it will add it. Note that in order to access this script from TextEdit (or any other application) you need to put the script in ~/Library/Scripts/. Then you need to activate the “script menu”. Launch the application /Applications/Applescript/Applescript Utility and check the box to “Show Script menu in menu bar”. After checking this box you will see the script menu at the top of your screen in the menu bar. Any scripts you put in ~/Library/Scripts/ will show up under that menu. To run the script just select it from the script menu.

-- this script will toggle "#" from the beginning of any selected paragraphs in TextEdit
-- to use the script just select the text and run the script
-- if you have problems then try increasing the value of "myDelay"

set myDelay to 0.2

-- get the selected text
tell application "TextEdit" to activate
delay myDelay
tell application "System Events" to keystroke "c" using command down
delay myDelay
set theText to the clipboard

-- parse the text
set textParas to paragraphs of theText
set moddedText to ""
repeat with aPara in textParas
	if aPara contains "# " then
		set thisText to characters 3 thru -1 of aPara as text
	else if aPara contains "#" then
		set thisText to characters 2 thru -1 of aPara as text
	else
		set thisText to "# " & aPara
	end if
	set moddedText to moddedText & thisText & return
end repeat
set moddedText to items 1 thru -2 of moddedText as text

-- put the modded text back
set the clipboard to moddedText
delay myDelay
tell application "System Events" to keystroke "v" using command down

Hi,

for this purpose I recommend to use TextWrangler (free).
It has a quite rich AppleScript dictionary

comment selected text:

tell application "TextWrangler"
	add prefix (get selection) prefix "#"
end tell

uncomment selected text:

tell application "TextWrangler"
	remove prefix (get selection) prefix "#"
end tell

Regarding the tutorials…
if you go to the front page http://macscripter.net/ and do a search in the “Search Articles” box the tutorials I mentioned will come right up. Search for “applescript tutorial for beginners” (include the quotes in the search box). You’ll see there’s 9 lessons, so start at the beginning and go through them. That’s how I learned anyway.

After you learn the basics go to the unscripted section to find other tutorials.

textWrangler is pretty nice – thanks for that :slight_smile:

& I’ve looked into the appleScript tutorials enough to know I like it (even thought it’s soooo long winded).

the last piece of the puzzle: hotkeys. I’ve used nedit in the past & it’s super easy to set up hotkeys. I trolled the documentation in textWrangler & couldn’t find any info on how to assign hotkeys for inbuilt stuffs, or appleScript actions. I also couldn’t find any docs on how to assign hotkeys to appleScript actions. Do you folks know if either is possible?

I’d go through the pain of installing nedit & run it from a server to get some hotkey access (or maybe get into vi again…).

Cheers,
Raphe

you can assign hotkeys only with third-party tools like QuicKeys, Butler. iKey, FastScripts, QuickSilver etc.